简体   繁体   中英

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 .

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM