topic-5
topic-5
When you have installed Django and created your first Django web
application, and the browser requests the URL, this is basically what
happens:
1. Django receives the URL, checks the urls.py file, and calls the view
that matches the URL.
2. The view, located in views.py, checks for relevant models.
3. The models are imported from the models.py file.
4. The view then sends the data to a specified template in the
template folder.
5. The template contains HTML and Django tags, and with the data it
returns finished HTML content back to the browser.
My First Project (my_tennis_club)
Django views are Python functions that take http requests and return
http response, like HTML documents.
A web page that uses Django is full of views with different tasks and
missions.
Views are usually put in a file called views.py located on your app's
folder.
There is a views.py in your members folder that looks like this:
Views
my_tennis_club/members/views.py:
def members(request):
my_tennis_club/my_tennis_club/urls.py:
urlpatterns = [
path('', include('members.urls')),
path('admin/', admin.site.urls),
]
Templates
In the Django Intro page, the result should be in HTML, and it should be created
in a template, so
Create a templates folder inside the members folder, and create a HTML file
named myfirst.html.
• <!DOCTYPE html>
• <html>
• <body>
• <h1>Hello World!</h1>
• </body>
• </html>
Modify the View
Open the views.py file and replace the members view my_tennis_club/members/views.py:
def members(request):
template = loader.get_template('myfirst.html')
return HttpResponse(template.render())
Up until now , output has been static data from Python or HTML
templates.
Now we will see how Django allows us to work with data, without
having to change or upload files in the process.
In Django, data is created in objects, called Models, and is actually
tables in a database.
Create Table (Model)
To create a model, navigate to the models.py file in the /members/ folder.
Open it, and add a Member table by creating a Member class, and describe the
table fields in it:
my_tennis_club/members/models.py:
• After creating Models, with the fields and data we want in them, it is
time to display the data in a web page.
• Start by creating an HTML file named all_members.html and place it
in the /templates/ folder:
my_tennis_club/members/templates/all_members.html:
<!DOCTYPE html>
<html>
<body>
<h1>Members</h1>
<ul>
{% for x in mymembers %}
<li>{{ x.firstname }} {{ x.lastname }}</li>
{% endfor %}
</ul>
</body>
</html>
Modify View
Next we need to make the model data available in the
template. This is done in the view.
In the view we have to import the Member model, and send it to the template like this:
my_tennis_club/members/views.py:
from django.http import HttpResponse
from django.template import loader
from .models import Member
def members(request):
mymembers = Member.objects.all().values()
template = loader.get_template('all_members.html')
context = {
'mymembers': mymembers,
}
return HttpResponse(template.render(context, request))
Run Example »
The members view does the following:
• Creates a mymembers object with all the values of the Member
model.
• Loads the all_members.html template.
• Creates an object containing the mymembers object.
• Sends the object to the template.
• Outputs the HTML that is rendered by the template.
Details Template
The next step in our web page will be to add a Details page, where
we can list more details about a specific member.
<!DOCTYPE html>
<html>
<body>
<h1>{{ mymember.firstname }} {{ mymember.lastname }}</h1>
<p>Phone: {{ mymember.phone }}</p>
<p>Member since: {{ mymember.joined_date }}</p>
<p>Back to <a href="/members">Members</a></p>
</body>
</html>
Add Link in all-members Template
The list in all_members.html should be clickable, and take you to
the details page with the ID of the member you clicked on:
my_tennis_club/members/templates/all_members.html:
<html><body>
<h1>Members</h1>
<ul>
{% for x in mymembers %}
{% endfor %}
</ul>
• </body></html>
________________________________________
Create new View
Then create a new view in the views.py file, that will deal
with incoming requests to the /details/ url:
my_tennis_club/members/views.py:
from django.http import HttpResponse
from django.template import loader
from .models import Member
def members(request):
mymembers = Member.objects.all().values()
template = loader.get_template('all_members.html')
context = {
'mymembers': mymembers,
}
return HttpResponse(template.render(context, request))
def details(request, id):
mymember = Member.objects.get(id=id)
template = loader.get_template('details.html')
context = {
'mymember': mymember,
}
return HttpResponse(template.render(context, request))
The details view does the following:
• Gets the id as an argument.
• Uses the id to locate the correct record in the Member table.
• loads the details.html template.
• Creates an object containing the member.
• Sends the object to the template.
• Outputs the HTML that is rendered by the template.
Add URLs
Now we need to make sure that the /details/ url points to
the correct view, with id as a parameter.
Open the urls.py file and add the details view to the
urlpatterns list:
my_tennis_club/members/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('members/', views.members, name='members'),
path('members/details/<int:id>', views.details, name='details'),
]