【发布时间】:2017-11-19 17:08:51
【问题描述】:
我正在制作一个包含多个应用程序的 Django 项目,因此有多个 urls.py 文件。我正在尝试将用户帐户的应用程序放入包含商店、购物车和订单应用程序的项目中。具体来说,我想将帐户/页面链接回商店
主 urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('account.urls')),
url(r'^cart/', include('cart.urls', namespace='cart')),
url(r'^orders/', include('orders.urls', namespace='orders')),
url(r'^', include('shop.urls', namespace='shop')),
]
帐户/的Urls.py:
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login',name='logout_then_login'),
url(r'^register/$', views.register, name='register'),
url(r'^$', views.dashboard, name='dashboard'),
]
这是我用于帐户页面的模板
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/base.css" %}" rel="stylesheet">
</head>
<body>
<div id="header">
<span class="logo">Rachel's Stuff</span>
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == "dashboard" %}class="selected"{% endif %}>
<a href="{% url "dashboard" %}">My dashboard</a>
</li>
<li {% if section == "images" %}class="selected"{% endif %}>
<a href="{% url 'shop' %}">Home</a>
</li>
<li {% if section == "people" %}class="selected"{% endif %}>
<a href="#">People</a>
</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
<a href="{% url "logout" %}">Logout</a>
{% else %}
<a href="{% url "login" %}">Log-in</a>
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
在这里,我想从 127.0.0.1:8000/account/ 链接回http://127.0.0.1:8000,默认为主店面:
<li {% if section == "images" %}class="selected"{% endif %}>
<a href="{% url 'shop' %}">Home</a>
</li>
但我得到一个错误:
未找到带有参数“()”和关键字参数“{}”的“shop”的反向操作。尝试了 0 个模式:[]
请求方法:GET
请求网址:http://127.0.0.1:8000/account/
Django 版本:1.8.6
异常类型:NoReverseMatch
异常值:
未找到带有参数“()”和关键字参数“{}”的“shop”的反向操作。尝试了 0 个模式:[]
当我已经在帐户命名空间中时,如何链接回主商店页面 (127.0.0.1:8000/)?对不起,如果我用错了任何术语。
【问题讨论】:
-
嗯,感谢您的意见。不过,当我将“shop”更改为“cart”时,我仍然会遇到同样的错误,而且我在 urls 文件中确实有 cart/。另外,在这个 url 方案下,是否有办法链接回主商店页面?