What is Django URL Patterns | Complete Django Tutorials
May 21, 2021 2022-12-19 7:08What is Django URL Patterns | Complete Django Tutorials
What is Django URL Patterns | Complete Django Tutorials
What are Django URL Patterns | Django Tutorials
We are publishing a new tutorial while learning Django, If you are looking for a good and explained discussion and answer of questions what are Django URL patterns and their different style and use then read this article from start to end.
URL is the uniform resource locator, we used the different words while learning Django as URL, URL Pattern, URLconf, URL Dispatcher, etc. We will learn all these terms, you will be cleared everything if you confuse. So, today we have to discuss Django URL patterns. How you can create a URL pattern for views mapping.
What is URLconf
When finding its answer on the internet we found that “A URLconf is like a table of contents for your Django-powered Web site.” It means when we create different URLs in one tuple in the urls.py file, it is the URLconf because it is just like table content that is original data, used by someone. So All the URLs that we create in the tuple are the contents of the URL pattern called URLconf.
What is URL Dispatcher
Example
In the urls.py file, you have to define URL Django URL patterns which will map your views function. In the following example, I took a simple example of a URL pattern that we will discuss in detail for your information
urlpatterns = [
path(‘users/, views.users, name=’users’),
]
urlpattern is the name of a list, which is the Python data type in which data is stored and List items are ordered, changeable, and allow duplicate values to store in it.
In the next, there are three arguments in the path function that are:
- Pathname: It is the pathname, using that user hit the URL
- View function reference: It is the function you want to run when its URL hit
- Name attribute: It is the name of your path, that mostly used in Template while making URL in the URL tag
Syntax of Django URL Patterns
urlpatterns = [
path(‘users/, views.users, name=’users’),
path(‘posts/’, views.posts, name=’posts’),
path(‘post_detail/<int:id>/’, views.post_detail, name=’post_detal’),
path(‘post_edit/<int:id>/’, views.post_edit, name=’post_edit‘),
path(‘post_del/<int:id>/’, views.post_del, name=’post_del‘),
]
Importance of URL pattern
Without URL, you did not reach the web page. You have to must enter a unique address for any web page you want to display on the browser. So, that URL we have created in Django urls.py files. If you want to make URLs for your different web page, you have to map with views code, you must give the reference of the code of that view for which you are creating URL. When any URL a user hits in the browser, then specific views function or class will be executed because we have mapped with that URL.
Conclusion
Sure, you have cleared today topic of Django URL patterns, which is very simple and easy to understand. Now it is your work to do practise to get experience. If you did not do that, you can work properly, because there is no benefit of just reading an article, but you have to implement all these concepts in your project.
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.
After reading today’s topic, if you face any problem while reading, understanding please discuss it with us. If you have understood and have any relevant questions then please discuss with us in the following comment section
Recommended Django Tutorial for beginners
Complete Discussion on Django Forms Widget | Django Tutorials
ALL About Django Form Errors | Django Tutorials