How to define a url that does not belong to any app?

42 views
Skip to first unread message

Kubilay Yazoğlu

unread,
Nov 25, 2017, 6:00:25 PM11/25/17
to Django users
Hi there. I have only one week of experience in Django, some things aren't clear to me yet.

I want to create a page where users can access by clicking on a button in homepage.
Since this page contains plain text and there is no functionality in it, it does not have be in any of the apps. But since I had to write its view function in somewhere, I've written it in the same view file of homepage (That's my thought. Correct me if I'm wrong)

First question is, where to define its view function? As I said I've defined it in the same view file of homepage. Not sure if that's the right way.
Secondly, how to define its url? I tried the following code but it does not work. I get this error when I click on the button in homepage: The current URL, learnmore.html, didn't match any of these.

url(r'^learnmore/$', learnmore_view)

Bernd Wechner

unread,
Nov 25, 2017, 7:17:33 PM11/25/17
to django...@googlegroups.com, Kubilay Yazoğlu
I'm curious too ;-). I suspect you can do pretty much anything at the project level that you can do at the app level but am not a pro in that space by any measure. This might be a useful read:

https://stackoverflow.com/questions/4879036/django-projects-vs-apps

Regards,

Bernd.

Kubilay Yazoğlu wrote:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d6750ede-da18-406a-a1e5-2336827b6ae3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Message has been deleted

Jason

unread,
Nov 25, 2017, 7:32:37 PM11/25/17
to Django users
What you can do is populate a path in your DIRS list in TEMPLATES in your settings file to the directory where your template is located.  Then, you can use TemplateView from django.views.generic to pass in a HTML file to be served up by Django for that URL.

Something like this:

urls.py

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
url(r'^', include('grid.urls')),
    url(r'^learn-more$', TemplateView.as_view(template_name='index.html'))
url(r'^silk/', include('silk.urls', namespace='silk'))
]

url_patterns = [
    
]

settings.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

What you're doing here is telling django to look in this location for templates.  Then, you're defining a URL that takes in the template name as a parameter to return when the URL his hit.

Kubilay Yazoğlu

unread,
Nov 26, 2017, 1:13:28 PM11/26/17
to Django users
SOLVED. Looks like I've done a silly mistake. Thanks for your answers though.

 Mistake was to write href="learnmore.html" in home page html. I changed this to href="{% url 'learnmore' %}, learnmore is the name of the view function of learnmore page.

26 Kasım 2017 Pazar 02:00:25 UTC+3 tarihinde Kubilay Yazoğlu yazdı:
Reply all
Reply to author
Forward
0 new messages