django反向匹配问题

时间:2017-01-13 03:08:37

标签: python django

我一直想弄清楚为什么我遇到这个问题。一切似乎都很好,我没有其他网址逆转的问题。当我手动输入URL时,页面显示正常。

这是导致问题的模板标记:{% url 'schedule:calendar' %}

这是urls.py:

app_name = 'schedule'
urlpatterns = [
    url(r'^$', login_required(views.CalendarView.as_view(), login_url='/login/'), name='calendar'),
]

我不认为这是必要的,但我的观点文件中只包含此内容:

class CalendarView(TemplateView):
    template_name = 'schedule/index.html'

    def get_context_data(self, **kwargs):
        context = super(CalendarView, self).get_context_data(**kwargs)
        context['template'] = 'calendar_view'
        return context

我根本没有看到命名冲突,但是当我将{% url 'schedule:calendar' %}放在我的模板中的任何地方时,它会抛出错误,即使这是模板页面中的唯一内容。

这是错误:

Environment:


Request Method: GET Request URL: http://127.0.0.1:8000/schedule/

Django Version: 1.10.3 Python Version: 3.5.2 Installed Applications: ['footables',  'floppyforms',  'djangobower',  'custom_user',  'bootstrap3_datetime',  'widget_tweaks',  'schedule.apps.ScheduleConfig',  'customers.apps.CustomersConfig',  'employees.apps.EmployeesConfig',  'jobs.apps.JobsConfig',  'frontend.apps.FrontendConfig',  'django.contrib.admin',  'django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.messages',  'django.contrib.staticfiles',  'rest_framework',  'mptt'] Installed Middleware: ['django.middleware.security.SecurityMiddleware',  'django.contrib.sessions.middleware.SessionMiddleware',  'django.middleware.common.CommonMiddleware',  'django.middleware.csrf.CsrfViewMiddleware',  'django.contrib.auth.middleware.AuthenticationMiddleware',  'django.contrib.messages.middleware.MessageMiddleware',  'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error: In template /home/poduck/PycharmProjects/mobileautorepair/schedule/templates/schedule/index.html, error at line 0    Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []   1 : {% extends 'frontend/base.html' %}{% load staticfiles %}{% block style %}    2 :  <link rel='stylesheet' href='{% static 'fullcalendar/dist/fullcalendar.min.css' %}'/>{% endblock %}    3 : {% block title %}Schedule{% endblock %}    4 : {% block body %}    5 :    <div class="container">    6 :         <div id="calendar"></div>    7 :     </div>    8 : {% endblock %}    9 : {% block script %}    10 :   <script src="{% static 'fullcalendar/dist/fullcalendar.min.js' %}"></script>


Traceback:

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  215.                 response = response.render()

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/response.py" in render
  109.             self.content = self.rendered_content

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/response.py" in rendered_content
  86.         content = template.render(context, self._request)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in render
  208.                     return self._render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  174.         return compiled_parent._render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  439.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/urls/base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix,
*args, **kwargs)))

File "/home/poduck/virtualpython3/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  392.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /schedule/ Exception Value: Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:0)

  

您可以发布模式的调试信息吗?订单(只需点击   错误的URL和Django将显示在&#34; debug on&#34;它适合你)? - 阿尔乔姆   Bernatskyi

感谢Artem Bernatskyi的评论,我明白了。我有一个数据API,我在该api的urls.py文件中有一个命名空间冲突。它中也有app_name='schedule'。我重命名了,现在一切正常。

对于遇到相同问题的任何人,如果您只是搜索所有代码以查找要反向匹配的名称,则可能会最快。确保它们不会出现在另一个urls.py文件中。

谢谢Artem。