urls.py多個Django的URL參數不工作
urlpatterns = [
url(r'^$', views.gallery, name='home'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
url(r'^(?P<album_id>\d+)/', views.album_details, name='album_details'),
]
views.py
def details(request, pic_id):
picture = get_object_or_404(Picture, pk=pic_id)
print("accessed details %s" %picture)
context = {
"picture": picture
}
return render(request, "picture_details.html", context)
gallery_details.html
{% for picture in pictures %}
<div class="img">
<a href="{% url 'gallery:details' picture.id %}">
<img src="{{ picture.picture_thumbnail.url }}" />
</a>
<div class="desc">{{ picture.description|truncatewords:5 }}</div>
</div>
{% endfor %}
當我嘗試運行此我得到一個異常值:
Reverse for 'details' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<album_id>\\d+)/(?P<pic_id>\\d+)/$']
它應該加載頁面的單個圖像,但它沒有。不知道爲什麼。
你期望它匹配你的'urlpatterns'中的第二個模式嗎?你有沒有嘗試從該模式中刪除最後的斜槓'/'? – schwobaseggl
好吧,我從'urlpatterns'中刪除了'/',現在如果我手動訪問網站http://127.0.0.1:8000/gallery/2/1,它就可以工作。但仍然,url http://127.0.0.1:8000/gallery/1/不起作用 –
你是否從'details'和'album_details'中刪除了它? – schwobaseggl