【问题标题】:Django - Website runs on localhost, returns NoReverseMatch on Ubuntu serverDjango - 网站在 localhost 上运行,在 Ubuntu 服务器上返回 NoReverseMatch
【发布时间】:2018-01-28 13:34:15
【问题描述】:

我正在使用 Django、Gunicorn 和 Nginx 在线运行一个项目。我目前正在尝试通过添加新的 URL 模式和页面供用户查看来更新网站。

但是,这会在在线时返回 NoReverseMatch 错误,但在本地,Django 不会返回任何错误。该错误仅在 HTML 文件调用我最近添加的命名空间时发生(即此 URL 不在网站的初始部署中)。

NoReverseMatch at /
Reverse for 'team' not found. 'team' is not a valid view function or pattern name.

虽然我已经在文件中明确指定了这个url和查看功能。

谢谢!

编辑:我正在使用这样的命名空间... .html 文件:

<a href='{% url "team" %}'>team</a>

urls.py:

url_patterns = [
   ...
   url(r'/team/$', views.team, name='team'
   ...
]

views.py:

def team(request):
    return render(request, 'team.html', {})

【问题讨论】:

  • 请详细说明您是如何使用名称和命名空间的。
  • 您是否在 Gunicorn 中重新加载了应用程序?
  • @LeLouch 编辑了我的问题以包含它。

标签: python django nginx ubuntu-16.04


【解决方案1】:

看起来像命名空间问题,只需清理 pyc 文件,这个Link 可以帮助你。

【讨论】:

  • ??你说“Just”x。后面是一个不引用x的链接。
【解决方案2】:

这应该可以工作,除非你的主 urls.py 中有另一个命名空间,例如

#main urls.py
url_patterns = [
   ...
   url(r'^club/', include('club.urls', namespace='club')),
   ...
]

#club urls.py
url_patterns = [
   ...
   url(r'/team/$', views.team, name='team'
   ...
]

在这种情况下,如果你这样做,你会遇到这样的错误

<a href='{% url "team" %}'>team</a> 

你必须这样做

<a href='{% url "club:team" %}'>team</a>

【讨论】:

    【解决方案3】:

    当你想渲染类似的东西时尝试使用 app_name: app_name = 'team_list'

    url_patterns = [ ... url(r'/team/$', views.team, name='team' ... ] 然后,在 a 标签中,试试这个:team

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2019-11-10
      相关资源
      最近更新 更多