In this Django Tutorial we are going to learn about Django Url Tag, django url tag returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates.
So first we need to create a new view function for our polls and after that we are going to link the urls in our navbar.html using django url tag. so this is our views.py file.
mysite/polls/views.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from django.shortcuts import render # Create your views here. def Index(request): context = {} return render(request, 'index.html', context) def Polls(request): return render(request, 'polls_list.html') |
Now create a new html file at name of polls_list.html for our newly created view.
mysite/templates/polls_list.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{% extends 'base.html' %} {% block title %} Polls List {% endblock %} {% block body %} <div class="container"> <h1>This is Polls List Page</h1> </div> {% endblock %} |
And now this is our urls.py, you need map your newly view function. you can see in this code we have name parameter in our paths, we can use this name with url tag to create absolute path reference.
1 2 3 4 5 6 |
from django.urls import path from .views import Index, Polls urlpatterns = [ path('', Index, name = 'index'), path('polls/', Polls, name = 'polls_list') ] |
Now open your navbar.html file and in the href section you need to add this code, you can see that we have used django url template tag for adding a path reference for our urls.
1 2 3 4 5 |
<a class="navbar-brand" href="{% url 'index' %}">GeeksCoders</a> <a class="nav-link active" href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a> <a class="nav-link" href="{% url 'polls_list' %}">Polls</a> |
Now this is our complete navbar.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<nav class="navbar navbar-expand-lg navbar navbar-dark bg-dark"> <a class="navbar-brand" href="{% url 'index' %}">GeeksCoders</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a> <a class="nav-link" href="{% url 'polls_list' %}">Polls</a> </div> </div> </nav> |
This is our rest of files for our django project.
mysite/templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{% extends 'base.html' %} {% block title %} Home {% endblock %} {% block body %} <div class="container"> <h1>Welcome to GeeksCoders.com</h1> <p>Tutorial Number 11 - Django URL Tag</p> </div> {% endblock %} |
mysite/static/css/style.css
1 2 3 4 5 6 7 8 9 10 11 |
h1 { color:green } p { color:red } |
mysite/polls/admin.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from django.contrib import admin from .models import Question # Register your models here. #first way #admin.site.register(Question) #second way @admin.register(Question) class QuestionModel(admin.ModelAdmin): list_filter = ('text', 'pub_date') list_display = ('text', 'pub_date') date_hierarchy = ('pub_date') ordering = ('pub_date', 'text') |
mysite/polls/models.py
1 2 3 4 5 6 7 8 9 10 11 |
from django.db import models # Create your models here. class Question(models.Model): text = models.CharField(max_length=100) pub_date = models.DateTimeField('date published') def __str__(self): return self.text |
mysite/urls.py
1 2 3 4 5 6 7 |
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('polls.urls')) ] |
Run your project and click on the Polls from navbar and this will be the result.