How to Make Django Dynamic URLs Pattern | Django Tutorials

Django Dynamic URL Pattern
Django Django Tutorials

How to Make Django Dynamic URLs Pattern | Django Tutorials

How to Make Django Dynamic URLs Pattern | Django Tutorials

URL is the uniform resources locator. The websites have multiple URLs for different web pages, each web page has a unique URL. We can access any web page through its URL. We can create a URL for any webpage, providing that web page to your friends, they directly reach that specific web page. So now we are learning Django URL patterns and how we can create a Django Dynamic URLs Pattern. Because we need sometimes, static URL and sometimes dynamic URL. So, you will understand in this Django tutorial about static and dynamic URLs.

What is a dynamic URL and Why We need its

Actually, a dynamic URL means, you have to create it first time defining some rule and then it will create multiple URLs automatically when need. static URL is the URL you create, did not change anytime but dynamic URL change with time to time when a new web page is created.

We need it in different forms, some of them I want to show you an example of blogging system.  When a user creates a blog post, the user did not need to create a URL for that blog post. The URL is given automatically to that blog post. Sometimes you have analyzed when you click on any blog post you will see in the URL bar its id number. ID number is the unique one that is why we create ID number-based dynamic URL, then each has different any unique URL.

We need of  Django Dynamic URLs Pattern because we did not allow users to create a specif URL for their blog posts. And when a user creates his/her profile then a profile URL is created for that specific URL automatically according to the user’s id number. Think that there are numbers of users registered. They have a unique URL according to their ID number, the URL will be created automatically and will be a unique one. So we cannot create URLs manually for all users, so we need to create Django Dynamic URLs Pattern in this case.

How to Create Django Dynamic URLs Pattern

If you want to create a URL pattern, you need to create urlpatterns list first, then write all the URLs inside the list.  In the following example, we have defined two Django Dynamic URLs Pattern, the first one shows that when you create a number of blog posts, dynamically each blog post will have a unique id and linkable. When anyone clicks on any post in order to delete, it will delete easily going to its dynamic URL. Same as 2nd line of code, which will work of updating a post for a specific post.

urlpatterns = [
path(‘blog_post_del/<int:pk>’, views.blog_post_del, name=’blog_post_del’),
path(‘blog_post_update/<int:pk>’, views.blog_post_update, name=’blog_post_update’),
]

Path Function Arguments Explanation for Learning Django Dynamic URLs Pattern

First argument is showing path of the web page which is blog_post_del/<int:pk> and blog_post_update/<int:pk>. As you are seeing <int:pk> is the unique primary key of the blog post. As all the blog posts have a unique id, we can easily identify all the posts through their ID number when we want to delete or update any post.
You will any post when you want to delete or update as:
http://127.0.0.1:8000/blog_post_del/32 -> you are deleting a post having a 32 id number
http://127.0.0.1:8000/blog_post_update/32 -> you are updating post having 32 id number
Now here, you will be noticed that you have created a Dynamic URL because you did not need to create other URLs for all posts, as we have mentioned <int:pk> in the URL patterns, and Django will create the Dynamic URL of our project.
2nd argument is showing views for specific URL, that is mapping as views.blog_post_del and views.blog_post_update.
3rd argument is showing the name of URL which is used in the template while creating URL tag as blog_post_del and blog_post_update

Conclusion

We have discussed Django Dynamic URLs Pattern in which you have learned that how you can create dynamic URLs for the web pages for which you want to create. Also, we have discussed its importance and why need to create Django dynamic URL. To get more examples, explanations, more concepts please keep updated this article, as we update all Articles on JafriCode.

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django tutorials

What is CSRF (Cross Site Request Forgery) and Why use in Django Form

Django Form | Render Form Field Manually | Django Tutorials

Best Explanation | Django Template Inheritance with Examples

How to Add Django Website Templates in Django Project and Application

Leave your thought here

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

Recent Comments

  1. Push it to the limit cool Wolf! You are the best and you can do everything! It'll all work out very very very soon! https://www.samsung.com smkmkplobydlmcrjmzgvx 2396134 on Django Create superuser | Django Tutorial
  2. Thank you For your hard work over the years! For this, we give you the opportunity. https://google.com#1234567890 For more information, see the instructions. skfhjvkjsdjsrbhvbsrfhkis 9357930 on Django Create superuser | Django Tutorial
  3. Thank you For your hard work over the years! For this, we give you the opportunity. https://google.com#1234567890 For more information, see the instructions. skfhjvkjsdjsrbhvbsrfhkis 5948210 on Django Create superuser | Django Tutorial
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox