DEV Community

Cover image for First Django App For Beginners Part2 (Views & Urls)
Abdul Azeez
Abdul Azeez

Posted on • Updated on

First Django App For Beginners Part2 (Views & Urls)

Welcome Back to part 2 of this tutorial I hope we all have a great understanding on Django.

We will continue from where we stopped in the last tutorial if you are just joining please checkout part1 of this tutorial part1 of this tutorial
Let's get started!

What is views in Django?

A view is simple python function that takes a request and return a http response, the response can either be in html, 404, xml, etc.

What is Urls in Django?
A urls is a web address e.g(www.example.com) that uses your view to know what to show the user on a web page

Note: Before you can start with your views you have to create a urls.py file in your application folder like this below

App Forlder Structure:

app/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
    urls.py
Enter fullscreen mode Exit fullscreen mode

Writing our first view
app/views.py

from django.shortcuts import render
from django.http import HttpResponse
def home(request):
    return HttpResponse("<h1>A simple views!</h1>")
Enter fullscreen mode Exit fullscreen mode

Setting up our urls
first we have to configure our urls.py in our project folder
project/urls.py

from django.contrib import admin
from django.urls import path, include # so we can include our app urls


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls')), #here

]
Enter fullscreen mode Exit fullscreen mode

Then
app/urls.py

from django.urls import path
from . import views # importing the view

urlpatterns = [
    path('', views.home, name="home"),
]
Enter fullscreen mode Exit fullscreen mode

A simple view and url config

We will Look at templates and static directory/files in the next tutorial

Top comments (0)