Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

https://xxxx/category_check_view/?item_id=2

Above is a sample of URL pattern. How should i configured my URL in order to enable it to redirect to the right view? I seem to get it working for a url like this https://xxxx/category_check_view/2/ only so far.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

You can pass parameters to a view either in the url:

/category_check_view/2

Or via GET params:

/category_check_view/?item_id=2

GET params are not processed by the URL handler, but rather passed directly to the GET param dict accessible in a view at request.GET.

The Django (i.e. preferred) way to do handle URLs is the first one. So you would have a URL conf:

(r'^category_check_view/(d{4})$', 'proj.app.your_view'),

And a matching view:

def your_view(request, id):
    obj = Obj.objects.get(id=id)
    # ...

However, if you insist on passing the param via GET you would just do:

(r'^category_check_view$', 'proj.app.your_view'),

And:

def your_view(request):
    id = request.GET.get('item_id')
    obj = Obj.objects.get(id=id)
    # ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...