我是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 %}
請發佈堆棧跟蹤。 – Sayse
讓我把它放下@Sayse –