简体   繁体   English

如何在 Django 应用程序中处理 URL 模式中的参数

[英]How to deal with params in URL schema in Django application

As we know most URL schemes base their URL syntax on this nine-part general format: <scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>正如我们所知,大多数 URL 方案的 URL 语法基于以下九部分通用格式: <scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>

I know I can get the query strings in Django request.GET dict.我知道我可以在 Django request.GET dict 中获取查询字符串。 But how to get parameters?但是如何获取参数呢? It seems HttpRequest in Django can not deal with params defined in URL. Django 中的 HttpRequest 似乎无法处理 URL 中定义的参数。

For example: http://www.example.com/hello;param1=aaaa;param2=bbbb例如: http : //www.example.com/hello;param1=aaaa;param2=bbbb

Is there any way to extract params like this in Django without writing own regex in url pattern.有什么方法可以在 Django 中提取这样的参数,而无需在 url 模式中编写自己的正则表达式。

Any help will be appreciated.任何帮助将不胜感激。

The path segment parameters you're talking about were defined in RFC 2396 .您正在谈论的路径段参数在RFC 2396中定义。 This RFC has been obsolete since 2005. The new standard, RFC 3986, does not define a specific way to define path segment parameters.此 RFC 自 2005 年起已过时。新标准 RFC 3986 未定义定义路径段参数的特定方法。 The relevant section states :相关部分指出

Aside from dot-segments in hierarchical paths, a path segment is considered opaque by the generic syntax.除了分层路径中的点段之外,路径段被通用语法认为是不透明的。 [...] For example, the semicolon (";") and equals ("=") reserved characters are often used to delimit parameters and parameter values applicable to that segment. [...] 例如,分号 (";") 和等号 ("=") 保留字符通常用于分隔适用于该段的参数和参数值。 [...] Parameter types may be defined by scheme-specific semantics, but in most cases the syntax of a parameter is specific to the implementation of the URI's dereferencing algorithm. [...] 参数类型可能由特定于方案的语义定义,但在大多数情况下,参数的语法特定于 URI 解引用算法的实现。

Django uses the new standard and does not define any specific way to parse path segment parameters. Django 使用新标准,并没有定义任何解析路径段参数的特定方式。 However, Python defines the urlparse() function, which parses the url according to RFC2396.但是,Python 定义了urlparse()函数,它根据 RFC2396 解析 url。 You can use this to extract the path parameters:您可以使用它来提取路径参数:

from urllib.parse import urlparse  # urlparse.urlparse on Python 2

params = urlparse(request.path).params

to capture params you've to define url patterns as per that.要捕获参数,您必须据此定义 url 模式。

lets assume your url is http://myurl.com/mydashboard/250?sort=-1&q=this so here 250 is param which you want.让我们假设您的网址是http://myurl.com/mydashboard/250?sort=-1&q=this所以这里 250 是您想要的参数。

To capture the same you've to define url like below要捕获相同的内容,您必须像下面这样定义 url

url(r'^mydashboard/(?P<weeknum>\d+)/$',
    mydashboard, name='mydashboard'),

and now the view definition of mydashboard will have another input param like below现在 mydashboard 的视图定义将有另一个输入参数,如下所示

def nagdashboard(request, weeknum):

And to get GET params or POST params you can use.并获得可以使用的 GET params 或 POST params。

request.GET.get('sort')

For further reading on patterns refer to here有关模式的进一步阅读,请参阅此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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