urls.py
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+)/$']
它应该加载单个图像的页面,但它不会。不知道为什么。
答案 0 :(得分:1)
您可能需要重新排列网址,以便首先使用不太具体的网址
url(r'^(?P<album_id>\d+)$', views.album_details, name='album_details'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
您还会注意到我修改了第一个正则表达式以包含$
字符以表示匹配结束