Django useful URL Patterns

In this tutorial we discuss some important url which make easy to maintain url in django.


Using path function:

In Django, the path () function is used to configure URLs. In its basic form, the path() function has a very simple syntax. An example of the basic path() function would be:


path(‘mypage/’, views.myview)

Another way to define it:


from django.urls import path

from .import views

urlpatterns = [

path(‘’, views.index, name=’index’)

]


Define using regex:


1- By using key

(?P<pk>\d+)

url(r'^questions/(?P<pk>\d+)/$', views.question_details, name='question_details'),

Example: /questions/0/


2- By using slug

(?P<slug>[-\w]+)-(?P<pk>\d+)

url(r'^blog/(?P<slug>[-\w]+)-(?P<pk>\d+)/$', views.blog_post, name='blog_post'),

Examples: /blog/hello-world-124/

/blog/a-0/


3- By using username

(?P<username>[\w.@+-]+)

url(r'^profile/(?P<username>[\w.@+-]+)/$', views.user_profile),

Examples: /profile/naveen/

/profile/@naveen/


4- By using year

(?P<year>[0-9]{4})

url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)

Examples: /articles/2019/


5-By using year and month

(?P<year>[0-9]{4})/(?P<month>[0-9]{2})

url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),

Example: /articles/2019/06/


6- By using year, month and date

(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})

url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail)

Example: /articles/2019/12/12/


If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion  you can send mail at contact@codersarts.com.

Please write your suggestion in comment section below if you find anything incorrect in this blog post 


#DjangoProject #Django #DjangoAssignementhelp #Djangostuff #Djangoprojectstuff #python #Djangoframework #HowtoaddURLinDjango #dateurlinDjango #howweusedateandyearinDjangoURL

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram