Najmul Hoque Sharif
Najmul Hoque Sharif

Reputation: 67

need to understand the ur to path converter in Django

i am learn to Django . i have stuking the below URL . i use path but i went to convert it to my path URL pattern .

This is my actual Path patrn

path('', views.home,name='home'),
path('topics/<int:id>/', views.board_topic, name='topics'), 
path('topics/<int:id>/new/', views.new_topic,name='new_topic'), 
path('signup/', accounts_views.signup, name='signup'),

i went to convert below this comented url to the below path

path('topics/<int:id>/topic_id', views.topic_posts, name='topic_posts'),


# url(r'^boards/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/$', views.topic_posts, name='topic_posts'),

This is Good Formate ? if not then which is best url to path formate ?

Thank You

Upvotes: 1

Views: 1357

Answers (2)

immanski njoro
immanski njoro

Reputation: 186

'url' is the old version of 'path' used in versions < Django 2.0. This is what it would look like using the more recent 'path' instead. path('boards/<int:pk>/topics/<int:topic_pk>/', views.topic_posts, name='topic_posts'),

Upvotes: 1

Bogdan Veliscu
Bogdan Veliscu

Reputation: 681

Try this:

path('boards/<int:pk>/topics/<int:topic_pk>/', views.topic_posts, name='topic_posts'),

If you need different types you can choose from this list or create custom converters. The following path converters are available by default:

  • str - Matches any non-empty string, excluding the path separator, '/'.
  • int - Matches zero or any positive integer. Returns an int.
  • slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
  • uuid - Matches a formatted UUID.
  • path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str.

Upvotes: 1

Related Questions