Django URL模式(~~/?item_id = 2) [英] Django URL pattern (~~/?item_id=2)

查看:36
本文介绍了Django URL模式(~~/?item_id = 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以上是URL模式的示例.我应该如何配置我的URL,以使其能够重定向到正确的视图?到目前为止,我似乎只能为 https://xxxx/category_check_view/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.

推荐答案

您可以在url中将参数传递给视图:

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

/category_check_view/2

或通过 GET 参数:

/category_check_view/?item_id=2

URL处理程序不处理

GET 参数,而是将其直接传递给在 request.GET GET 参数dict.>.

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.

第一个处理URL的Django(即首选)方法.因此,您将拥有一个URL conf:

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'),

和一个匹配的视图:

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

但是,如果您坚持要通过 GET 传递参数,则可以这样做:

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

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

并且:

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

这篇关于Django URL模式(~~/?item_id = 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆