Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Follow publication

How to Django — URLs and Templates

Emily Y Leung
Geek Culture
Published in
8 min readAug 21, 2021
Photo by AltumCode on Unsplash

URL Patterns

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('appname.urls')),
]
from django.urls import path
from . import views
app_name = "appname"urlpatterns = [
path('', views.home, name="home"),
]
from django.http import HttpResponse, JsonResponse# Create your views here.
def home(request):
return HttpResponse('<h1>Hello World</h1>')
from django.http import HttpResponse, JsonResponse# Create your views here.
def home(request):
return HttpResponse('<h1>Hello World</h1>')
def contact(request):
return HttpResponse('<h1>Hi my name is Jeremy.</h1>')
from django.urls import path
from . import views
app_name = "appname"urlpatterns = [
path('', views.home, name="home"),
path('contact/', views.contact, name="contact"),
]

Creating templates

Static template

sitename/
manage.py
sitename/
appname/
admin.py
apps.py
models.py
tests.py
views.py
__init__.py
migrations/
templates/
appname/

home.html
<!DOCTYPE html><html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Django Website</title> </head>
<body>
<h1>Hello World!</h1></body>
</html>
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# Create your views here.
def home(request):
return render(request, "appname/home.html")
def contact(request):
return HttpResponse('<h1>Hi my name is Jeremy.</h1>')

Dynamic Template

<!DOCTYPE html><html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Django Website</title> </head>
<body>
{% block content %}

{% endblock %}
</body>
</html>
{% extends "appname/base.html" %}{% block content %}
<h1>Hello World!</h1>
{% endblock content %}
{% extends "appname/base.html" %}{% block content %}
<h1>Hi my name is Jeremy.</h1>
{% endblock content %}

Variables in templates

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# Create your views here.
def home(request):
return render(request, "appname/home.html")
def contact(request):
return render(
request,
"appname/contact.html,
{"context":"Jeremy"}
)
def contact(request):
context = "Jeremy"
return render(
request,
"appname/contact.html,
{"context":context}
)
def contact(request):
context = {"name":"Jeremy"}
return render(
request,
"appname/contact.html,
{"context":context}
)
{% extends "appname/base.html" %}{% block content %}
<h1>Hi my name is {{ context }}.</h1>
{% endblock content %}
{% extends "appname/base.html" %}{% block content %}
<h1>Hi my name is {{ context.name }}.</h1>
{% endblock content %}
{% extends "appname/base.html" %}{% block content %}
<h1>Hi my name is {{ context.name|safe }}.</h1>
{% endblock content %}

Dynamic URLs

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# Create your views here.
def home(request):
return render(request, "appname/home.html")
def contact(request, name="Jeremy"):
name = name
return render(
request,
"appname/contact.html,
{"context":name}
)
from django.urls import path
from django.conf.urls import url
from . import views
app_name = "appname"urlpatterns = [
path('', views.home, name="home"),
url(r'^(?P<name>\b[A-Za-z]\w+\b)/$', views.contact, name="contact"),
]
{% extends "appname/base.html" %}{% block content %}
<h1>Hi my name is INPUT_NAME.</h1>
{% endblock content %}
http://localhost:8000/contact/Tom{% block content %}
<h1>Hi my name is Tom.</h1>
{% endblock content %}

Summary

Emily Y Leung
Emily Y Leung

Written by Emily Y Leung

Creative. Problem solver. Learning programming in public | emilyyleung.github.io

No responses yet

Write a response