简体   繁体   English

如何查看是否存在基于URL的视图函数

[英]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 with reverse() , you don't need to worry about the urlconf parameter. reverse() ,您不必担心urlconf参数。 The function returns a ResolverMatch 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.

相关问题 测试以查看URL是否存在 - Test to see if URL exists 如何检查基于函数的视图是从另一个重定向函数调用还是直接从 url 调用? - How can i check if the function based view was called from another redirect function or directly from the url? 如何在 Django 中使用基于函数的视图查看帖子? - How to see posts using functions based view in django? 如何查看 Tkinter 中是否存在小部件? - How to see if a widget exists in Tkinter? 如何在基于函数的视图中转换基于类的详细视图? - How to convert class based detail view in function based view? 如何在Django中将基于函数的视图转换为基于类的视图 - How to convert a Function based view into a Class based View in django 如何将基于类的视图变量转换为基于函数的视图? - how to turn class based view variable to function based view? 如何在 Django 中将基于 function 的视图集成到基于 class 的视图中? - How to integrate a function based view into a class based view in Django? django:如何将基于 class 的视图与基于 function 的视图合并 - django: how to merge a class based view with a function based view 如何在django中获取视图函数的url路径 - How to get the url path of a view function in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM