loading...
Cover image for Django Tutorial #2: URLs, Models, Views and Templates

Django Tutorial #2: URLs, Models, Views and Templates

ericnanhu profile image Eric Hu Originally published at techjblog.com ・3 min read

In this section, we’ll talk about URLs, models, views and templates, as well as their relationships with each other.

URLs

img

As the name suggests, the urls.py handles the URLs. It reads information from a URL and returns a view. Do not confuse this view with the view in Laravel. They are two different things, and we’ll talk about this later on.

Django_Blog/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls'))
]

Line 1 to line 4 import the necessary packages.

Line 7 is for the admin panel which is included in Django. We’ll talk about this later in this tutorial.

Line 8 means if the URL starts with www.yourdomain.com/blog/, Django will go to /blog/urls.py and reads information from the rest of the URL. This functionality helps us manage the URLs for different apps.

blog/urls.py
from django.urls import path
from blog import views

urlpatterns = [
    path('', views.home, name='home'),
]

Line 5, if nothing follows the URL www.yourdomain.com/blog/, the home view is called.

Views

Views act like the controllers in Laravel. It is the place where you organize the logic and the behaviour of your app. Views receive requests from URLs, retrieve data from the database through models, and finally return a template.

In Django, all the views are defined in the views.py. You can define the views in classes or functions. In this tutorial, we’ll use functions since it is easier to understand.

blog/views.py
from django.http import HttpResponse


def home(request):
    return HttpResponse("Hello, world. You're at the home page.")

Start the server,

python manage.py runserver

Open your browser and go to http://127.0.0.1:8000/blog/

img

Of course, views can do a lot more than just return a string. Its primary purpose is to retrieve data from the database and manipulate them in the way you wanted and finally pass them onto a template.

Templates

Templates are what you see in your browser, they are like the views in Laravel. Which is why they are all HTML files. As an example, we create a template named home.html.

Create a new blog folder inside templates, and then create a home.html inside blog.

templates/blog/home.html
<!DOCTYPE html>
<html>
<body>

    <h1>My First Heading</h1>

    <p>My first paragraph.</p>

</body>
</html>

Now we can make our home view return the home.html template instead of a string.

blog/views.py
from django.shortcuts import render


def home(request):

    return render(request, 'blog/home.html')

Refresh the browser, you should see an HTML page instead of just a string.

img

Models

Remember I said views are in charge of retrieving data from the database? Well, that’s where models come in.

Each database table has a corresponding “Model”, which is defined in class, and that model will handle all the interactions with the table.

However, before we create models and templates, we need to design our database structure.

Summary

img

In summary, when you type in the URL in the browser, the router reads the information and returns the corresponding view, the view would retrieve the data from the database through the model and put them in the corresponding template, and finally, the template is returned to the browser.

Next Post: Django Tutorial #3: Database

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Posted on by:

Discussion

pic
Editor guide