URL and Rendering HTML in Django


URL
URL's can be considered as routes in the context of web application.
example:
httP://www.mazhar.com/
,http://www.mazhar.com/about
,http://www.mazhar.com/contact
URL's are defined inside
urls.py
file. Each URL is mapped with specific view function which is defined inviews.py
file.
View
Business logic is written in
view function
,view functions
return appropriate response. If needed it interact withmodel
to get data from database.view function acts as a glue between
model
andtemplate
.In your
view function
, you typically use therender
function to combine a template with data and return it as the HTTP response.The
render
function takes three arguments:The request object
The path to the template file
A dictionary containing data to be inserted into the template placeholders
Create Django App
Create Django app named 01_basic_project
Add URL's
move to basic_project folder using cd command in
urls.py
add following code
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.homepage),
path('about/', views.about)
]
Add functions in views.py
You have to create
view.py
file it is not there in basic_project folder by default. Add following code
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello World! I'm Home.")
def about(request):
return HttpResponse("My About Page.")
Return HTML response frow view functions
from django.shortcuts import render
def homepage(request):
return render(request, 'home.html')
def about(request):
return render(request, 'about.html')
Create
templates
folder on the same level ofmanage.py
file. inside that folder createhome.html
andabout.html file
.Go to setting.py file and write
'DIRS' : ['templates']
.home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<p>Check out my <a href="/about">About</a> page.</p>
</body>
</html>
Add CSS template
Create
static folder
on the same level of manage.py file.Inside
static folder
createcss folder
andjs folder
.Create
style.css
insidecss folder
andmain.js
insidejs folder
.
Add
import os
at the top of settings.py file and add following setting.Add
{% load static %}
at the top ofhome.html
.Link CSS
<link rel="stylesheet" href="{% static 'css/style.css' %}">
Link JS
<script src="{% static 'js/main.js' %}" defer></script>
home.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script src="{% static 'js/main.js' %}" defer></script>
</head>
<body>
<h1>Home</h1>
<p>Check out my <a href="/about">About</a> page.</p>
</body>
</html>
Subscribe to my newsletter
Read articles from Mazhar Solkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
