捕获 request.GET 中的 URL 参数 [英] Capturing URL parameters in request.GET

查看:30
本文介绍了捕获 request.GET 中的 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在定义正则表达式以捕获 URL 中的参数,如教程中所述.如何从 URL 访问参数作为 HttpRequest 对象的一部分?

I am currently defining regular expressions in order to capture parameters in a URL, as described in the tutorial. How do I access parameters from the URL as part the HttpRequest object?

我的 HttpRequest.GET 当前返回一个空的 QueryDict 对象.

My HttpRequest.GET currently returns an empty QueryDict object.

我想在没有库的情况下学习如何做到这一点,这样我就可以更好地了解 Django.

I'd like to learn how to do this without a library, so I can get to know Django better.

推荐答案

当 URL 类似于 domain/search/?q=haha 时,您将使用 request.GET.get('q', '').

When a URL is like domain/search/?q=haha, you would use request.GET.get('q', '').

q 是你想要的参数,如果没有找到 q'' 是默认值.

q is the parameter you want, and '' is the default value if q isn't found.

但是,如果您只是在配置 URLconf**,那么您从 regex 捕获的数据将作为参数(或命名参数)传递给函数.

However, if you are instead just configuring your URLconf**, then your captures from the regex are passed to the function as arguments (or named arguments).

例如:

(r'^user/(?P<username>w{0,50})/$', views.profile_page,),

然后在你的 views.py 中你会有

Then in your views.py you would have

def profile_page(request, username):
    # Rest of the method

这篇关于捕获 request.GET 中的 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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