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 trialArcee Palabrica
8,100 PointsClarification on views.py and urls.py
Can anyone help me pound this into my brain?
So the way I understand this is that when a client sends a request (e.g. http://127.0.0.1:8000/courses/
) the first file that will be looked into is in /learning_site/urls.py
... it'll then look into the available patterns in the urls.py
, gets a match in the regex r'^courses/
then takes us to our /courses/urls.py
through include('courses.urls')
where it finds another regex in the urlpatterns
that finally takes us to /courses/views.py
where course_list()
returns the HttpResponse
showing the list of courses in the form of HTML. Did I understand it right or I'm messing it up?
Also can anyone expound how include('courses.urls')
works?
1 Answer
Anthony Albertorio
22,624 PointsThink about the concept of pluggable apps. The reason we have them inside our main project is to increase our modularity. Each app should be stand alone. So, each app has it has its own views.py and urls.py which make them stand alone. You don't want to worry about someone else's code when adding the app to your page. It should just work.
When we include our app inside of our main project, we let Django know that "hey, this app has its own way of handling things. Refer to that." You tell Django that with the include().
That way you don't have to worry about the internals of the pluggable app that you are using which was written by someone else. You can just add their urls.py to your main urls.py file and forget it.
Hope this helps.