Moji.Michael
Moji.Michael

Reputation: 133

Django Url and Views cant pass parameters

I am using Django 2.1.4 I Want to Pass a parameter like question_id in the url but i just get 404 .

This is My Code :

urls.py :

urlpatterns = [
    path('index',views.index , name='index'),
    path('index/(?P<question_id>[0-9])',views.detail , name='detail'),
]

and this is my :

view.py

def index(request,):
    return HttpResponse("Welcome To My Page")

def detail(request, question_id):
    return HttpResponse("new Page" + str(question_id))

When I Enter http://127.0.0.1:8000/polls/index/12 in the url , i just get 404 .

Upvotes: 1

Views: 2326

Answers (2)

Salvioner
Salvioner

Reputation: 333

With Django 2.1.4 the path method can parse params in the URL with the following syntax:

urlpatterns = [
    path('index',views.index , name='index'),
    path('index/<int:question_id>', views.detail , name='detail'),
]

If you want to stick to the good old regex, you should probably change your 'detail' view to

path('index/(?P<question_id>[0-9]+/$)',views.detail , name='detail')

See also this article for more information.

Upvotes: 0

Van
Van

Reputation: 302

I think that's the old (prior to 2.0) notation. I use

path('profile/edit_avatar/<int:avatar_id>', views.edit_avatar, name='edit_avatar')

in my urls.py and

def edit_avatar(request, avatar_id=0):

in my views (with a default value, just in case)

See the Django tutorial, especially page 3.

Upvotes: 1

Related Questions