What are Django URLs and How to create them(Part VII)
In this tutorial, we are going to learn about Django URLs. How URLs with Django and how to create Django URLs for your own web application.
This tutorial is a part of our series on Creating Blog Website using Django.
Link to Previous tutorials:
What are Django URLs
A URL is a web address. For example, codespeedy.com is also a URL.
In Django, we use URLconf, which is a set of patterns that Django will try to match the requested URL to find the correct view.
Let us open site1/urls.py to understand more of it. It looks something like :
"""site1 URL Configuration [...] """ from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
It contains the Django Admin window URL, we have seen in the previous tutorial. It basically tells Django that for any URL ending with admin/ ; check for the respective view as registered under admin control.
Creating a Django URL
Now, in the file site1/urls.py, add a line
so that it now looks like,
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog1.urls')), ]
After this, everything starting with http://127.0.0.1:8000/ will now be redirected to blog1/urls.py
Further, let us configure blog1/urls.py. To this file add the following code :
from django.urls import path from . import views
urlpatterns = [ path('', views.post, name='post'), ]
In this, we are assigning a new view post to the address http://127.0.0.1:8000/ . The argument (name = ” ),is used to identify a view.
But we have not created a view yet, so the command will show an error; something like ‘web page not available‘ on the browser page.
Don’t worry we will have a look at creating a view in the upcoming tutorial.
For now, this is it! If you face any error, send us the query in the comment section below.
Next part of this Django series:
Have a look at some other posts: