2016-02-14 94 views
0

我是django的新手;雖然在這裏已經解決了同樣的錯誤,但我實際上無法找到我的代碼出錯的地方。NoReverseMatch at/Gyobera/

這是我的錯誤:

NoReverseMatch at /Gyobera/ 
Reverse for 'classification' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$'] 
Request Method: GET 
Request URL: http://127.0.0.1:8000/Gyobera/ 
Django Version: 1.8.2 
Exception Type: NoReverseMatch 
Exception Value:  
Reverse for 'classification' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$'] 
Exception Location: C:\Python34\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496 
Python Executable: C:\Python34\python.exe 
Python Version: 3.4.2 
Python Path:  
['C:\\Users\\Scott Businge\\Ewange', 
'C:\\Python34\\lib\\site-packages\\pip-7.1.0-py3.4.egg', 
'C:\\Users\\Scott Businge\\Ewange', 
'C:\\WINDOWS\\SYSTEM32\\python34.zip', 
'C:\\Python34\\DLLs', 
'C:\\Python34\\lib', 
'C:\\Python34', 
'C:\\Users\\Scott Businge\\AppData\\Roaming\\Python\\Python34\\site-packages', 
'C:\\Python34\\lib\\site-packages'] 
Server time: Mon, 15 Feb 2016 08:48:43 +0300 

鏈接不能點擊顯示詳細的頁面,每次我點擊它表明從索引頁,我將在這裏發表下錯誤。我只使用該項目的URL(Ewange)

的index.html

 <h2><strong>Main Classifications</strong></h2> 
      {% if classifications %} 
      <ul> 
       {% for classification in classifications %} 
       <!-- Following line changed to add an HTML hyperlink --> 
       <li><a href="{% url 'classification' classification.url %}">{{ classification.name }}</a></li> 
       {% endfor %} 
      </ul> 
     {% else %} 
      <strong>There are no classifications present.</strong> 
     {% endif %} 
{% endblock %} 

urls.py

url(r'^classification/(?P<classification_name_url>\w+)/$', views.classification, 
          name='classification'), 

views.py

def index(request): 
    context = RequestContext(request) 
    classification_list = Classification.objects.order_by('-likes')[:6] 
    context_dict = {'classifications': classification_list} 
    for classification in classification_list: 
     classification.url = classification.name.replace(' ', '_') 
    return render_to_response('index.html', context_dict, context) 

def classification(request, classification_name_url): 
    context = RequestContext(request) 
    classification_name = classification_name_url.replace('_', ' ') 
    context_dict = {'classification_name': classification_name, 'classification_name_url': classification_name_url} 
    try: 
     classification = Classification.objects.get(name=classification_name) 
     context_dict['classification'] = classification 
     lists = List.objects.filter(classification=classification) 
     context_dict['lists'] = lists 
    except Classification.DoesNotExist: 
     return render_to_response('gyobera/classification.html', context_dict, context) 

classification.html

<h1>{{ classification_name }}</h1> 
{% if classification %} 
    {% if lists %} 
    <ul> 
     {% for list in lists %} 
     <li><a href="{{ list.title }}">{{ list.title }}</a></li> 
     {% endfor %} 
    </ul> 
    {% else %} 
     <strong>No lists currently in classification.</strong> 
    {% endif %} 
{% else %} 
    The specified classification {{ classification_name }} does not exist! 
{% endif %} 

setings.py

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'Gyobera', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'Ewange.urls' 

當我運行的服務器,我仍然得到 「NoReverseMatchError」 這裏是urls.py,views.py和鏈接的當前狀態index.html的。 我一直在問的是,修正後classification.list_set,做ü必須改變觀點和分別urls.py的context_dict和URL匹配..

NoReverseMatch at /Gyobera/ 
Reverse for 'classification' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$'] 
Request Method: GET 
Request URL: http://127.0.0.1:8000/Gyobera/ 
Django Version: 1.8.2 
Exception Type: NoReverseMatch 
Exception Value:  
Reverse for 'classification' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$'] 
Exception Location: C:\Python34\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496 

urls.py

url(r'^classification/(?P<classification_name_url>\w+)/$', views.classification, 
          name='classification'), 

views.py

def classification(request, classification_name_url): 
    context = RequestContext(request) 
    classification_name = classification_name_url.replace('_', ' ') 
    context_dict = {'classification_name': classification_name, 'classification_name_url': classification_name_url} 
    try: 
     classification = Classification.objects.get(name=classification_name) 
     context_dict['classification'] = classification 
     lists = List.objects.filter(classification=classification) 
     context_dict['lists'] = lists 
    except Classification.DoesNotExist: 
     return render_to_response('gyobera/classification.html', context_dict, context) 

的index.html

{% if classifications %} 
    <ul> 
     {% for classification in classifications %} 
     <!-- Following line changed to add an HTML hyperlink --> 
     <li><a href="{% url 'classification' classification.list_set.url %}"></a></li> 
     {% endfor %} 
    </ul> 
{% else %} 
+2

請發佈堆棧跟蹤。 – Sayse

+0

讓我把它放下@Sayse –

回答

0

您會看到錯誤,因爲您正在查看的分類沒有網址,並且沒有網址,因爲您從未將更改保存到視圖中的模型。但我並不認爲每次有人加載索引頁面時都要保存對模型所做的更改。

def index(request): 
    context = RequestContext(request) 
    classification_list = Classification.objects.order_by('-likes')[:6] 
    for classification in classification_list: 
     classification.url = classification.name.replace(' ', '_') 
     classification.save() 

    context_dict = {'classifications': classification_list} 
    return render_to_response('index.html', context_dict, context) 
-2
​​

分類沒有一個URL字段,所以在「分類」的網址沒有參數。這就是爲什麼錯誤說with arguments '()' and keyword arguments '{},即:沒有參數。

實際關係:一個分類有很多列表,每個列表都有一個url字段。因此,單個分類通過List模型具有多個URL。

訪問單個分類的所有列表,您可以使用:

classification.list_set #list_set is the default, when you don't use related_name 

這可以通過這種分類的列表給出多個列表,所以無論循環,或者選擇一個,是這樣的:

<li><a href="{% url 'classification' classification.list_set.first().url %}"> 
+0

它顯示相同的@Aviah。讓我發佈堆棧跟蹤 –

+0

讓我們嘗試調試它,我懷疑它是與名稱/網址的東西。而不是'{%url'分類'classification.url%}'從表中嘗試一個值,比如'{%url'分類'foobaz%}'。另外,你可以發佈模型嗎? –

+0

是的,模型在這裏。 –