Instagram
youtube
Facebook
Twitter

Django Dynamic URLs

We have covered all the basic details related to Django URLs in our Basic Django tutorial. In this tutorial, we’ll cover mainly two topics Static URLs and Dynamic URLs. 

Static URLs

A static URL is a type of URL that does not change. In technical terms, we can say that these URL does not contain URL parameters. It is very difficult to maintain these types of URLs if the content of our website grows rapidly. Because we have to hard-code the URL of every single page. A static URL looks like https://www.example.com/about.

Dynamic URLs

A Dynamic URL is a type of URL that can change. These type of URLs contains special characters like ? , = , & , + etc. the website that fetches content from a database generally uses dynamic URLs. one drawback of using dynamic URLs is one web page can map to many different URLs. A dynamic URL looks like https://www.example.com/about-my-code/detail?id=23.

Dynamic URL Path Converters

Before creating dynamic URLs in Django first we have to know what type of path converters we can use in Django. So, here is the list of all path converters we can use in Django.

  • int – it matches zero or positive integer type values.

Example - path("contact/<int:id>/", views.contactinside, name='home')

  • str – it matches string type values (excluding the path separator(‘/’))

Example - path("contact/<str:id>/", views.contactinside, name='home')

  • slug – it matches slug string type values, i.e. a string consisting of alphabets, digits, hyphens and underscore.

Example - path("contact/<slug:id>/", views.contactinside, name='home')

  • path – it matches string type values (including the path separator(‘/’)).

Example - path("contact/<path:id>/", views.contactinside, name='home')

  • UUID – it matches a UUID(universal unique identifier).

Example - path("contact/<uuid:id>/", views.contactinside, name='home')

Creating Dynamic URL in Django

To create a Dynamic URL first open the app’s url.py and use any type of path converters that are described above. We’ll create our URLs using two types of path converters.

  • Using str path converter - follow the steps given below to create a dynamic URL using this type of path converter. First open your app’s url.py file and add the following expression.

-my_app/urls.py

urlpatterns = [
    path("", views.contact, name='home'),
    path("contact/", views.contact, name='home'),
    path('upload/', views.Image),
    path("contact/<str:id>/", views.contactinside, name='home'),
    ]

 

Here, i have used angular brackets after contact/. Inside angular brackets, i have passed the type of path converter i.e, str and an id to render this in views.

Now, open your views.py and use the given expression to add view.

-my_app/views.py

def contactinside(request, id) :
    return HttpResponse(id + " hello welcome to this page")

Here we have created a function and named it the same as we used in our url.py file to render views. We have passed the request and id in our function. After this, we returned an HTTP response and passed a string with an id inside this.
Now, let us check whether our Dynamic URL is working or not.

Here, we have passed codersdaily23 as id and it is perfectly working.

 

 

  • Using int path converter - please follow all the steps given below to create a dynamic URL using slug. First, open your app’s url.py file and add the following expression.

-my_app/urls.py

urlpatterns = [
    path("", views.contact, name='home'),
    path("contact/", views.contact, name='home'),
    path('upload/', views.Image),
    path("contact/<int:id>/", views.contactinside, name='home'),
    ]

 

Here, I have used angular brackets after contact/. Inside angular brackets, i have passed the type of path converter i.e, int and an id to render this in views.

Now, open your views.py and use the given expression to add view.

-my_app/views.py

def contactinside(request, id) :
    return HttpResponse(id)

Here we have created a function and named it the same as we used in our url.py file to render views. We have passed the request and id in our function. After this, we returned an HTTP response and passed id inside this.
Now, let us check whether our Dynamic URL is working or not.



Here, we have passed 45454545 as id and it is perfectly working.