June 10, 2020, 7:47 p.m.

Handling arguments in django urls - path(), re_path() & url() functions

When developing a web application you may notice that some programmers uses path() where some uses url() in django urls.py . This may puzzles you to use whether path() or url(). If you try django's documentation you may still have doubts (don't worry documentations are for experienced developers not for newbies)

Today i will explain the use of re_path() & the difference between path() and url().

The first thing is that the url() was used till django1.11 version. After django1.11 the url() is replaced with path().
The reason behind this is to pass the arguments/url-parameteres in a more easy and meaningful way.
An example of path and url is shown below, you will understand why path() is more easy and meaningful in this article.
url(r'^blog/(?P[0-9]+)/$', blog_view)

The above code i.e the url() is written as :
path("blog/<int: blog_id>/", blog_view)

Note : Path always matches to the whole path. This means that
path('myaccount/login/')

is equivalent to
url(r"^myaccount/login/$")

So the main difference is that, we can not use regex or regular expression in path().




url() To path() Cheatsheet

Django provides an easy and meaningful way to pass parameters or arguments in urls. The cheatsheet of url() to path() is described below :
(Appreciation for my drawing is not required, i know it's amazing )

image not found
Convertors

Django comes with 5 in built converters, each of them is described below :
int:
int: matches all positive integers for example "189" and return an int value.
Equivalent regular expression to int is :
[0-9]+
str:
str: is defalut converter for path if we do not specify any other converter in our path. It matches all non empty strings excluding "/" i.e slash or the path seprator.
Example = "blog_id=45"
Equivalent regular expression to str is :
[^/]+
slug:
Matches any slug string consisting of ASCII letters, numbers,the hyphen and underscore characters etc.
slug returns a string (str) value.
Example: 'solution-hub'
Equivalent regular expression to slug is :
[-a-zA-Z0-9_]+

uuid:
uuid matches a formatted UUID. To prevent multiple URLs from mapping to the same web-page, dashes must be included & letters must be in lowercase.
uuid: returns a UUID instance (uuid.UUID).
Example: '07r167u-6265-447h-a56h8'
Equivalent regular expression :
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}

path:
path: matches any non-empty string, including the path separator i.e slash ‘/’. This allows developers to match against a complete URL path rather than just a segment of a URL path as with str.
Example: '/path/to/image'
path returns a string (str).
Equivalent regular expression :
'.+'


Passing arguments in URLs

I hope you understood the difference between url() & path() . You can use any of the 2 if you are not passing any argument/parameter in your url. On the other hand if you are passing the arguments then path() is the easy and more meaningful option rather than using url()

Now i am going to teach you that how can you pass arguments in urls more efficently.
If you use path() to pass arguments for example :
#in template
<a href="{% url 'blog' id=7 %}">Blog</a>
OR
<a href="{% url 'blog' id=blog_object.id %}">Blog</a>

Then your url will be something like this :
#in urls.py
path('blog/<int:id>/',views.ShowBlog,name='blog')

Now the function will be :
#in view.py
def ShowBlog(request,id):
   #Do anything here


The above example clearly shows that the parameters are the part of urls in these type of cases. But i am going to show you that how you can pass arguments in urls without disturbing the urls.
you can do something like this :
#in template
<a href="{% url 'blog'%}?id=7">Blog</a>
OR
<a href="{% url 'blog'%}?id={{blog_object.id}}">Blog</a>

Now our URL will be something like this :
#in urls.py
path('blog/',views.ShowBlog,name='blog')

Now the function will be :
#in view.py
def ShowBlog(request):
   id=request.GET.get('id')
   id=int(id)

This is how you can pass values (as string) using GET request and access the value in view's functions.


Tips If the path() doesn't satisfy your needs then you can use re_path() . By using this method you can use regex or regular expressions.
from django.urls import re_path

re_path(r'blog/(?P[0-9]+)/$', blog_view)


On the other hand the url() function is still available in django you can use that also.








Some recent posts



Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...

Programming vs coding | difference between programming and coding ...



View all...