[英]How to see if a view function exists based on a url
I am trying to give django a url and resolve it to a view. 我试图给Django一个URL并将其解析为一个视图。 For example: 例如:
>>> reverse('login')
'/login/'
>>> reverse('/login/')
Traceback (most recent call last): File "", line 1, in File "/Users/david/Desktop/V/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "/Users/david/Desktop/V/lib/python3.6/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix raise NoReverseMatch(msg) 追溯(最近一次通话最近):文件“ /Users/david/Desktop/V/lib/python3.6/site-packages/django/urls/base.py”中第90行的第1行反向返回iri_to_uri(resolver._reverse_with_prefix(view,prefix,* args,** kwargs))文件“ /Users/david/Desktop/V/lib/python3.6/site-packages/django/urls/resolvers.py”,第622行,在_reverse_with_prefix中引发NoReverseMatch(msg)
Is there another way to do this? 还有另一种方法吗? Or a django method that I can pass it a url (these will be 'dirty' urls, such as " https://example.com/login/?next=/myaccount/ "), and it will resolve it to the correct view? 或django方法,我可以为其传递一个url(这些将是“肮脏”的url,例如“ https://example.com/login/?next=/myaccount/ ”),它将把它解析为正确的视图?
The opposite of redirect
is resolve
[Django-doc] in the django.urls
module: redirect
的反义词是django.urls
模块中的resolve
[Django-doc] :
The
resolve()
function can be used for resolving URL paths to the corresponding view functions.resolve()
函数可用于解析指向相应视图函数的URL路径。 It has the following signature: 它具有以下签名:
resolve(path, urlconf=None)
path
is the URL path you want to resolve.path
是您要解析的URL路径。 As withreverse()
, you don't need to worry about theurlconf
parameter. 与reverse()
,您不必担心urlconf
参数。 The function returns aResolverMatch
object that allows you to access various metadata about the resolved URL. 该函数返回一个ResolverMatch
对象,该对象使您可以访问有关已解析URL的各种元数据。
For example: 例如:
>>> from django.urls import resolve
>>> resolve('/admin/')
ResolverMatch(func=django.contrib.admin.sites.index, args=(), kwargs={}, url_name=index, app_names=['admin'], namespaces=['admin'])
This thus returns a ResolverMatch
that contains the "view" function that will be called together with positional and named parameters, and information about the namespace, name of the app(s), etc. 因此,这将返回一个ResolverMatch
,其中包含将与位置和命名参数一起调用的“ view”函数,以及有关名称空间,应用程序名称等的信息。
For URLs that can not be resolved, it will raise a django.urls.exceptions.Resolver404
error. 对于无法解析的URL,将引发django.urls.exceptions.Resolver404
错误。
Note that the querystring is not part of the path
. 请注意,querystring不是path
一部分。 You can extract the path with: 您可以使用以下方法提取路径:
>>> from urllib.parse import urlparse
>>> urlparse('/login/?next=a')
ParseResult(scheme='', netloc='', path='/login/', params='', query='next=a', fragment='')
So only the path='...'
part (can be obtained by fetching the .path
attribute) will determine the view function, the querystring is passed to the view as a dictionary-like object with request.GET
. 因此,只有path='...'
部分(可以通过获取.path
属性来获取)将确定视图函数,将querystring作为带有request.GET
的类似于字典的对象传递给视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.