eli5 Django
Marcelino Veloso III,Imagine the request-response pattern.
A client, usually a browser, makes a request to an http server. The server responds with data from a database, templated with html
, css
and js
.
The request
- When
runserver
starts and we click on http://127.0.0.1:8000/, the browser initiates anHttpRequest
to the server. - Django matches the endpoint of this url to a developer-defined
UrlPattern
route. - What is the endpoint? Well, it's the tip of the url. In the url above, it would be
/
: this is the tip, the endpoint. - The router's matching process goes something like this for url http://127.0.0.1:8000
/
:- Does endpoint of http://127.0.0.1:8000
/
match/about
? No. - Does endpoint of http://127.0.0.1:8000
/
match/contact-us
? No. - Does endpoint of http://127.0.0.1:8000
/
match/
? Yes
- Does endpoint of http://127.0.0.1:8000
The response
- The endpoint
/
is typically the homepage of the website. - It's up to the developer to construct how this
View
looks like, typically by combininghtml
,css
, andjs
with data. - So the
UrlRoute
represents a pre-constructed homepageView
- This
View
, based on http://127.0.0.1:8000/, will then be rendered as aHttpResponse
for the browser.
Summary
We started with a requesting url and retrieved a viewable response.
- The url's endpoint made an
HttpRequest
to the server. - The server looked for
UrlPatterns
, replying with: "Yes, I have aView
representing request to/
" - This
View
is packaged into anHttpResponse
for rendering in the browser. - Since the
View
will likely contain other urls... we can expect more request-response cycles.
This is a naive understanding of the flow but should cover how clicks become views.
For a more comprehensive approach, the constantly updated books of Will Vincent are likely your best bet in understanding Django better. He uses class-based views for his examples so this might be appreciated further by diving into the more functional approach used by Django Girls.