1
我在index.html
意外NoReverseMatch錯誤使用包括當()中的URL模式
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$(?P<pk>[0-9]+)/$']
views.py
def rock_and_feat(request):
feats = Feat.objects.order_by('-created')[:3]
rocks = Rockinfo.objects.order_by('-rank')[:50]
context = RequestContext(request, {
'feats': feats, 'rocks': rocks
})
return render_to_response('template.html', context)
class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'
urls.py引用detail.html
時得到一個錯誤
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
的index.html
{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
{% for feat in feats %}
<div class="specialsticky">
<a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.image.url }}" alt="some text"></a>
<h1 class="mast-header">
<a href="#">{{feat.title}}</a>
</h1>
</div>
{% endfor %}
{% else %}
<p>No </p>
{% endif %}
</div>
{% endblock %}
detail.html
{% extends "index.html" %}
<iframe width="560" height="345" src="{{ feat.youtube_link }}" frameborder="0" allowfullscreen></iframe>
項目urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
應用在我在index.html
的圖片上添加<a href=
之前工作得很好。
無法弄清楚什麼是錯的。
對不起,但我不明白。正如你上面看到的'urls.py'中的正則表達式在它之前沒有一美元。你要求我做什麼改變?我編輯了上面的代碼爲項目添加'urls.py'。 – WutWut
當您使用include時,從正則表達式中移除美元符號,即'url(r'^',include('feature.urls',namespace =「feature」)),' – Alasdair