Django Server
Intro
In this lesson we are going to step away from the Pokedex exercise for a second and concentrate on the Django Server. The Django server is a development server that allows you to run your Django web application locally for testing and development purposes. It provides a convenient way to preview your application and debug any issues before deploying it to a production environment. This guide covers several important topics related to the Django server:
- What does
python manage.py runserverdo? - How do urlpatterns and paths work?
- How do URL paths link to Django views?
- What is a request?
- How to pass parameters through URL patterns?
What does python manage.py runserver do?
The
python manage.py runservercommand is used to start the Django development server. It launches a lightweight web server that listens for incoming requests and serves your Django application locally. By running this command in your project's root directory, you can access your application via a local URL (typicallyhttp://127.0.0.1:8000/) in your web browser.
How do urlpatterns and paths work?
In Django, the
urlpatternsvariable is a list of URL patterns defined in your project'surls.pymodule. Each URL pattern consists of apath()function call, which maps a URL pattern to a specific view function or view class.The
path()function takes two required arguments:routeandview. Therouteargument specifies the URL pattern, while theviewargument identifies the view function or class that handles the request.
# pokedex_proj/urls.py
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('squares/', square_area_view, name='square'),
# path('circles/', circle_area_view, name='circle'),
# path('triangles/', triangle_area_view, name='triangle'),
]
In the example above, three URL patterns are defined. The first pattern maps the URL
squares/to thesquare_area_viewfunction, and the second pattern maps the URL/circles/to thecircle_area_viewfunction and so on.It's important to note that each one of these URL paths are prefaced by
http://127.0.0.1:8000/, this means that if we wanted to send a request to thesquares/path we would have to send a request tohttp://127.0.0.1:8000/squares/
How do URL paths link to Django views?
URL paths in Django are associated with view functions or view classes, we will learn about Class Based Views later on in the course. When a request is made to a specific URL, Django's URL resolver determines the corresponding view function or class based on the defined URL patterns.
For example, if a user requests the URL
squares/, Django's URL resolver matches it to thesquare_area_viewfunction specified in theurlpatternslist. Currently this view function doesn't exist, so lets create it right above oururlpatterns. (This is not where views should be created but for simplicities sake this is where we will create them today)
# pokedex_proj/urls.py
from django.http import HttpResponse
def square_area_view(request):
area_of_a_square = 2 ** 2
return HttpResponse(area_of_a_square)
urlpatterns = [
path("..."),
]
The
square_area_viewfunction is responsible for processing the request and generating an appropriate response. It can interact with models, perform calculations, and render templates to construct the response.Lets try this out by running our Django Server with
python manage.py runserverand then opening our browser with http://127.0.0.1:8000/squares/ where we should see the number 4 appear on our screen.
What is a request?
In Django, a request represents an HTTP request made by a client (e.g., a web browser) to the Django server. The request contains information such as the URL, HTTP method (e.g., GET, POST), headers, and any submitted data.
When a request is received by the Django server, it is passed to the appropriate view function or class based on the URL pattern. The view then processes the request and returns an HTTP response, which is sent back to the client.
Create a
circle_area_viewfunctional view that will take in a request and return the area of a circle with a radius of 2. Lets printrequestwithin this view and take a look at what our Django Server prints to our terminal every time a request is processed.
# pokedex_proj/urls
import math
def circle_area_view(request):
print(request)
area_of_a_circle = math.pi * (2 ** 2)
return HttpResponse(area_of_a_circle)
In the example above, the
circle_area_viewfunction is responsible for handling requests to thecircles/URL. It currently returns theareaof a circle but aviewcould potentially return just about anything. Test is by traveling to http://127.0.0.1:8000/circles/. Look at the Django Terminal to see the output of our print statement and experiment with the following request headers:
- request.method = returns the requests method
- request.body = returns the body of the request within a binary string
- request.headers = returns almost all information about the origin of the request
We don't expect you to understand these, we expect you to know they exist and familiarize yourself with these topics on your own time.
How to pass parameters through URL patterns?
URL patterns in Django can include parameters that are captured and passed to the corresponding view function or class. Parameters are specified within angle brackets
< >in the URL pattern definition and by default can only be of typeinteger(int)or typestring(str).
path('triangles/height/<int:height>/base/<int:base>/', triangle_area_view, name='triangle'),
In the example above, the URL pattern
triangles/height/<int:height>/base/<int:base>/captures integer parameters (heightandbase) and and passes it to thetriangle_area_viewfunction.
# pokedex_proj/urls
def triangle_area_view(request, height, base):
print(f"Height: {height}\nBase: {base}")
area_of_a_triangle = (height * base)/2
return HttpResponse(area_of_a_triangle)
The
triangle_area_viewfunction can access the captured parameters as arguments and use them to display the details of a specific triangles area. Lets test it with the following:
- http://127.0.0.1:8000/triangles/height/5/base/4/
- http://127.0.0.1:8000/triangles/height/20/base/6/
- http://127.0.0.1:8000/triangles/height/2/base/30/
Conclusion
The Django server is a crucial tool for local development and testing of Django applications. By using
python manage.py runserver, you can start the development server and access your application through a local URL. Understanding the concepts of urlpatterns, paths, views, requests, and parameter passing allows you to define the URL structure, link it to appropriate views, process requests, and create dynamic and interactive web applications with Django.