Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Jeffery
Courses Plus Student 26,985 Pointsurl template tag - how does it know where 'views.hello_world' is located?
In the video, in the learning_site/templates/layout.html page, how does the url tag 'views.hello_world' know to use the view in the learning_site stub folder.
The templates folder is in the outer directory, the same as the courses app and the learning_site stub folder - if it was a relative path presumably it would be 'learning_site.views.hello_world'
Basically I am asking how does it make the connection, if that makes sense?
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe module lookup is relative lo the project base directory, in this case, .../learning_site
. So the reference views.hello_world
would first look for the file .../learning_site/views.py
.
Note that the url of the form {% url 'views.hello_world' %}
has been deprecated in Django 1.8 and later. The preferred pattern now uses a url named reference. See docs
Edit: The search order for a view is dependent on the urls
defined in the urls.py
files. The resolution goes as follows:
-
manage.py
defines which thesettings.py
file to use:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'learning_site.settings')
- In the
settings.py
file, the "top" URL file is defined byROOT_URLCONF
:
ROOT_URLCONF = 'learning_site.urls'
- Starting in the file
learning_site.urls.py
, all URLs are defined:
urlpatterns = [
url(r'^courses/', include('courses.urls', namespace='courses')),
url(r'^admin/', include(admin.site.urls)),
url(r'^suggest/$', views.suggestion_view, name='suggestion'),
url(r'^$', views.hello_world, name='home'),
]
This defines the view.hello_world
in learning_site
as at the top level. The views defined in courses.urls
will have the namespace courses.'
prefix. This makes the views within courses
all have the courses.views.
prefix. The include
function inserts the app urls recursively.
It is the Django convention to place all of the views for each app in their app-specific views.py
file. The hierarchy of the modules using the "app.views.view" unique for each view.
Daniel Jeffery
Courses Plus Student 26,985 PointsBut the views.py file is in the learning_site stub rather than the learning_site base directory, (the outermost directory) isn't it?
I suppose what you are saying is that the two can be thought of as being synonymous in this case - which I guess makes sense. I would have thought {% url 'learning_site.views.hello_world' %} would have made more sense.
I guess either way it is deprecated, and not the preferred way, so more of an academic question.
For the record, I find it a little unusual that they went with that convention to have two files of the same name - not sure why they did that!
Chris Freeman
Treehouse Moderator 68,457 PointsI would like to be sure we are speaking about the same things. Can you post a hierarchical listing of your files from the top level learning_site directory? In Linux it's $ ls -R
Daniel Jeffery
Courses Plus Student 26,985 PointsSo this question is coming from the Django Basics course The badge is "Final Details" and the specific video is "url Tag".
The section I am referring to is around the 01:00 - 03:00 mark.
The files in the top level learning_site directory are:
assets courses db.sqlite3 learning_site manage.py templates
In the video we have a layout.html template which is in the templates directory - /learning_site/templates/layout.html
This file has these two links in it:
<a href="{% url 'views.hello_world' %}">Home</a>
<a href="{% url 'courses.views.course_list' %}">Courses</a>
The second link I understand, because it is a absolute path from the outermost learning_site directory. i.e /
The first link to me would make more sense being learning_site.views.hello_world, which would be consistent with the previous absolute path.
(Note: I just tried to see if learning_site.views.hello_world would work - and it did. So my conclusion is that because the 'stub folder' learning_site as Kenneth called it, has the same name as the outer learning_site, it realises that when you do views.hello_world that it should use the file in /learning_site/templates/views.py rather than looking for /views.py.
Once again - this is confusing to write and probably to read, because the outermost learning_site, and the stub learning_site have the same name!
Seems like a convention that is bound for confusion!
Hopefully that made sense and helps someone out!
Chris Freeman
Treehouse Moderator 68,457 PointsThanks for the additional info. I've updated my answer.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsWhich video are you referring to?