How to Create Dynamic URLs in Django



Python


In this article, we show how to create dynamic URLs in Django.

We showed on another how to harcode URLs in Django. Hardcoding urls means manually writing out the full path to the page that you are creating a hyperlink to.

However, there is another way to create URLs in Django, and this is the dynamic way, by creating dynamic URLs.

In this article, we'll show how to create a dynamic URL, what's required, and all of that.

So let's say we have a website called, http://www.mywebsite.com

If you're running this on a local host, let's say you have, http://127.0.0.1:8000/ or localhost:8000/

All of these really are the same thing in that they are the host of the website.

But let's just go with, http://www.mywebsite.com

On this website you want to go to the following URL.

http://www.mywebsite.com/Blog/blogpost1.html

So this is the page we want to go to.

Hardcoding this URL produces the following anchor tag.



Or you could completely hardcode the entire path. This is shown below.



How can we do this now with a dynamic URL?

So above is the hardcoded URL. Now we need to do it dynamically.


urls.py File in the Project Root Directory

We first have to do a few things. We need to go to the urls.py file in the project root directory and add a namespace to the URL blog.

This is shown below.



I just put the entire urls.py file in the root directory.

you can see the url for the Blog app contains a namespace within the include function. This is what is necessary to create a dynamic URL. We need a namespace in the urls.py file in the project root directory. In this example, the namespace is 'blog'.


urls.py file in the App

Now we need to go to the urls.py file in the app we are referencing, in this case, the Blog app.

We need to check the URL name of the blogpost1 URL.

So we go to this page and should have the following, shown below.



So now looking in this file, we see that we have a url pattern for blogpost1 with a name equal to 'blogpost1'. This is the URL name.

So before you saw we had the namespace linking to, http://www.mywebsite.com/Blog/ page.

Now we have a URL with a name linking to the blogpost1 page in the Blog app.

So this is important, because we have the namespace and the URL name, which are the 2 components we need to create a dynamic URL.

So now we can create the dynamic URL, and we do this through the following code, shown below.



So now we've created the URL, http://www.mywebsite.com/Blog/blogpost1 dynamically.

All we have to do is have {% url %} and in between we specify in quotes (single or double) the, namespace:URL name

And of course if you need to add extra path to the dynamic URL, you can do so.

For example, the following code below adds the object id to the URL above, making for the following code.



This will now the following URL, http://www.mywebsite.com/Blog/blogpost1/1 if the object has an id of 1.

And this creates a dynamic URL in the Django framework.


Related Resources

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django



HTML Comment Box is loading comments...