URL In Django – Free Django Tutorials

What is a URL?

A URL is simply a web address. You can see URL which is visible in your browser’s address bar every time you visit a website[like whenever we run our django-project we can run the server at 127.0.0.1:8000,which is URL].
Today every page on the internet needs its own URL.So because every page have it’s own URL it knows what it should show to a user who opens that URL.For Django, we have something called URLconf (URL configuration).Whenevar user request for a specific URL django will try to match the requested URL with the set of patterns to find the correct view.That set of patterns are contained in URLconf.


How do URLs work in Django?

Open up the blog/urls.py which is created in this post writing-your-first-blog-app-in-django in your editor of choice[prefer pycharm] and check what it contans.

from django.contrib import admin
from django.urls import path.url

urlpatterns = [
    url('admin/', admin.site.urls),
]

As you can see from the code above django has already put something here for us.

path(‘admin/’, admin.site.urls),

The above line means  for every URL that starts with admin/, django will find a corresponding view.To know more about admin url’s you can visit https://docs.djangoproject.com/en/2.0/ref/contrib/admin/


Regex in URL

Django uses regex(regular expressions) for matching URLs to views. Regex has a lot of rules that form a search pattern.
In django we will need only a limited subset of the rules to express the pattern we are looking for, namely:

  • ^ which will used for the beginning of the text
  • $ which will used for the end of the text
  • \d which will used for a digit
  • + which will used to indicate that the previous item should be repeated at least once
  • () which will used to capture part of the pattern

Now imagine you have a website with the address like http://www.blog.com/post/13579/, where 13579 is the number of your post.

Writing separate views for each and every post number would be really annoying. With the help of regular expressions, we can create a pattern that will match the URL and extract the number for us:
^post/(\d+)/$.

Let’s break down the above expression by piece to see what we are doing here:

  • ^post/ is tells django to take anything that has post/ at the beginning of the URL (right after ^)
  • (\d+) means that there will be a number (it can have one or more digits) and that we want the number captured and extracted
  • / tells django that another / character should follow after a number.
  • $ then indicates the end of the URL and tell us that only strings ending with the / will match this pattern

Write Your first Django URL!

We want http://127.0.0.1:8000/ to be the home page of our blog app and to display a list of posts.

We also want to keep the blog/urls.py file clean, so we will import URLs from our articles application which we will create in this post  Writing your first blog app in django-part2” to the main blog/urls.py file.
So for this purpose go ahead and add a line that will import blog.urls.

Note that we are using the include function here so you will need to add that import.

After changes your urls.py file should look like this:[where r stands for regular expression]

from django.contrib import admin
from django.urls import path
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'', include('articles.urls')),
    
]

Django will now redirect everything that comes into ‘http://127.0.0.1:8000/‘ to blog.urls and looks for further instructions there.

To know more about django url’s visit https://docs.djangoproject.com/en/2.0/topics/http/urls/.


This is all about django url’s,regex and how to write django in url in your project.

Leave a Comment

Your email address will not be published. Required fields are marked *