开发者

Django url: I want to match this param, how to write the regex?

开发者 https://www.devze.com 2023-02-03 15:56 出处:网络
url scheme sample: http://domain/tests/?_=1111111&data=3333333&status=22222222 In view, I ne开发者_如何学Goed data and status, any approach is welcome! All the params are integers.Your data

url scheme sample:

http://domain/tests/?_=1111111&data=3333333&status=22222222

In view, I ne开发者_如何学Goed data and status, any approach is welcome! All the params are integers.


Your data passed as GET parameters does not need to be matched in urls file.

urlpatterns += patterns(
    ('^tests/$', 'app.views.test'),
)

You will have that data in your view as: request.GET.get('_', None), ...

Your can write a form which will help you to validate and clean up the data and use it like this in your view:

form = some_form(data=request.GET)
if not form.is_valid():
    raise InvalidRequest()
data = form.cleaned_data


If that's the url the params are accessible through the request.GET dictionary in the view method.

def my_view(request):
    print request.GET['data']

Of course if you need to validate whether the elements are in the dictionary('data' in request.GET) and convert them to int prior using them like that. int(request.GET['data'])

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号