【问题标题】:Django - link to url from different app in same projectDjango - 链接到同一项目中不同应用程序的 url
【发布时间】: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 方案下,是否有办法链接回主商店页面?

标签: python django


【解决方案1】:

您使用了错误的 url 名称 (shop) 来反转。查看shop/urls.py 文件并查看^$ 路径的实际名称。由于已经定义了命名空间,因此应将其反转为 shop:&lt;your url name here&gt;

【讨论】:

  • 从 django.conf.urls 导入 url 从 .导入视图 urlpatterns = [ url(r'^$', views.product_list, name='product_list'), ]
  • 它应该是product_list 然后,将其反转而不是shop,应该解决它。
  • 谢谢,错别字抱歉
  • 实际上,我遇到了同样的错误。 >未找到带有参数“()”和关键字参数“{}”的“product_list”的反向操作。尝试了 0 个模式:[] 编辑:shop:product_list 有效
  • 不好意思,还有命名空间,试试shop:product_list
猜你喜欢
  • 1970-01-01
  • 2017-07-18
  • 2015-01-18
  • 1970-01-01
  • 2018-04-18
  • 2010-09-12
  • 2019-06-25
  • 1970-01-01
  • 2012-12-24
相关资源
最近更新 更多