Django网址错误。调试返回NoReverseMatch

时间:2019-07-03 06:52:25

标签: python django django-urls

我正在尝试建立一个到管理页面(localhost:8000/admin)的链接,但是当尝试这样链接时: HTML:

{% if user.is_staff %}
  <a class="nav-item nav-link" href="{% url 'admin' %}">Admin</a>
{% endif %}

代码:

urlpatterns = [
  path('admin/', admin.site.urls, name='admin'),
]

我收到一个错误,说NoReverseMatch at /

跟踪:

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\core\handlers\base.py" in _get_response
  145.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\core\handlers\base.py" in _get_response
  143.                 response = response.render()

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render
  171.                     return self._render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\defaulttags.py" in render
  309.                 return nodelist.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\template\defaulttags.py" in render
  443.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\urls\base.py" in reverse
  90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "C:\Users\krish\Envs\myproject\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  668.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name.

为什么会这样?它可以与其他链接一起使用,但不适用于管理员。

Python 3.7.3
Django 2.3

0 个答案:

没有答案