Render Your First Django View
Now we have created our Django Aapplication, it is time to create a view. Views are the parts of the code that will be rendered on your web browser.
Follow this tutorial from the very beginning, if you are missing previous part of the Django Project.: Python Django Tutorial for Beginners
Before going through the code these are the steps to consider while creating new views:
- Views are either method based or class based
- views.py consists all our application views
- Each methods represent a unique view
- We either directly write the html code in the method or import a template which is a html file within that method
- A route must be defined in order to see the view from your browser. The path where your method resides is actually determined by the routes.
Method(or Classes), Template and Routes are three important things to consider while creating views.
Here we will be using Method based views and for now we will simply print some texts on the home page of our application.
Define a Home Method
We need to import HttpResponse and render from django library. Our home method looks like the code shown below:
from django.http import HttpResponse from django.shortcuts import render # Create your views here. def home(request): return HttpResponse("<h1><b>Welcome</b> to the first <i>Django View</i></h1>")
Here we simply defined a method home, which is a view that renders the HTML snippet we mentioned within the HttpResponse method. Instead of raw html caode we can also include the template name and include a html page. Which shall be done later in upcoming blog.
Define your View in the URL
Here you need to understand about how Application URLs are maintained by Django Project. We can have multiple applications within a project and following cases can be implemented regarding the routes:
- Django Project can include urls of all different applications under its urls.py file
- Each project can maintain their own routes within their project strucutre and the Django Project urls.py will list those application urls in it only.
So both ways can be implemented. For now we will be defining a route for our newly created method based view in Django Project urls.py. Our code looks like this:
from django.contrib import admin from django.urls import path from django.conf.urls import url from restaurants.views import home urlpatterns = [ path('admin/', admin.site.urls), url(r'^ The first thing we need to do is import that home method we just defined in the urls.py. My application name is restaurants and I imported it as shown in the code The first route that is defined under urlpatterns is for django administrtion. You will be asked for your login credentials if you go to this url: http://127.0.0.1:8000/admin/ A detailed about Django Administration can be found here: Walk through of Django Administration Second route which has a regex r'^$' is defined for home. This denotes for an empty string. Therefore if the route is http://127.0.0.1:8000 it calls the home method and renders the view defined in that method. With this your first Django View, a method based view is ready. , home),
The first thing we need to do is import that home method we just defined in the urls.py. My application name is restaurants and I imported it as shown in the code
The first route that is defined under urlpatterns is for django administrtion. You will be asked for your login credentials if you go to this url: http://127.0.0.1:8000/admin/
A detailed about Django Administration can be found here: Walk through of Django Administration
Second route which has a regex r’^$’ is defined for home. This denotes for an empty string. Therefore if the route is http://127.0.0.1:8000 it calls the home method and renders the view defined in that method.
With this your first Django View, a method based view is ready.