Mohamed Benkedadra
Mohamed Benkedadra

Reputation: 2084

regex in django 2.0 re_path

I'm fairly new to python and Django, so please excuse me if this seems like too simple a question.

I've been trying to use this in CreateView, but it is not working:

re_path(r'^<str:pk>/$', indexView.as_view(), name='index'),

Can anyone tell me why, and how to fix this?

Upvotes: 6

Views: 11472

Answers (2)

Astik Anand
Astik Anand

Reputation: 13057

You are doing wrong, you are using re_path which expects regex, you should use path here in this case. And also you should use slug type and not str.

path('<slug:pk>/', indexView.as_view() ,name = 'index'),

But if you still want to use, re_path you have to use regex.

re_path(r'^(?P<slug>\w+)/$', indexView.as_view() ,name = 'index'),

You can follow the django docs here.

Upvotes: 9

Alasdair
Alasdair

Reputation: 309089

You are mixing up the regular expression (re_path()) and converter (path()) syntax. Assuming your pk is an integer you should use either:

path('<int:pk>/', indexView.as_view(), name='index'),

or

re_path(r'^(?P<pk>[0-9]+)/$', indexView.as_view(), name='index'),

Upvotes: 4

Related Questions