2017-06-16 57 views
2

我正在製作一個包含多個應用程序並因此具有多個urls.py文件的Django項目。我正在嘗試將用戶帳戶的應用程序與商店,購物車和訂單的應用程序集成到一個項目中。具體來說,我想將賬戶/網頁鏈接回店Django - 鏈接到來自同一項目中不同應用程序的url

主要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> 

但我得到一個錯誤:

Reverse for 'shop' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Request Method: GET

Request URL: http://127.0.0.1:8000/account/

Django Version: 1.8.6

Exception Type: NoReverseMatch

Exception Value:
Reverse for 'shop' with arguments '()' and keyword arguments '{}' not >found. 0 pattern(s) tried: []

當我已經在帳戶名稱空間中時,如何鏈接回主要商店頁面(127.0.0.1:8000/)?對不起,如果我用任何錯誤的術語。

+0

嗯,謝謝你的輸入。但是,當我將「商店」更改爲「購物車」時,我仍然遇到同樣的錯誤,並且我確實在推車文件中包含購物車。此外,在這個網址方案下,是否有辦法鏈接到主商店頁面?來自django.conf.urls的 –

回答

2

您正在使用錯誤的網址名稱(shop)進行反向。看看shop/urls.py文件,看看^$路徑的實際名稱。由於已經定義了名稱空間,所以應該將其顛倒爲shop:<your url name here>

+0

從中導入url 。導入視圖 urlpatterns = [ url(r'^ $',views.product_list,name ='product_list'), ] –

+0

它應該是'product_list'然後,相反,而不是'shop',應該解決它。 – hurturk

+0

謝謝,對於錯別字 –

相關問題