参数的Python NoReverseMatch
我是Python Django新手,我正在努力解决URL模式问题,我创建的网站有三个Html页面和它们各自的三个视图,我正在链接代码和项目结构的图像请帮助我找到解决方案: 项目结构:- 所以,我的想法是,当我点击index.html时,页面会重定向到detail.html,图像id为'music/id',现在我想重定向到image上的其他html页面,点击detail.html到picturedeail.html,属性为'song\u title' 代码:-参数的Python NoReverseMatch,python,django,Python,Django,我是Python Django新手,我正在努力解决URL模式问题,我创建的网站有三个Html页面和它们各自的三个视图,我正在链接代码和项目结构的图像请帮助我找到解决方案: 项目结构:- 所以,我的想法是,当我点击index.html时,页面会重定向到detail.html,图像id为'music/id',现在我想重定向到image上的其他html页面,点击detail.html到picturedeail.html,属性为'song\u title' 代码:- Models.py:- from
from django.db import models
class Album(models.Model):
art_type=models.CharField(max_length=255)
album_title=models.CharField(max_length=255)
art_method=models.CharField(max_length=255)
album_logo=models.FileField()
def __str__(self):
return self.album_title;
class Picture(models.Model):
album=models.ForeignKey(Album,on_delete=models.CASCADE)
file_type=models.FileField()
song_title=models.CharField(max_length=245)
{% extends 'music/base.html' %}
{% block title %}Album Details{% endblock %}
{% block body %}
<ul>
{% for picture in album.picture_set.all %}
<div class="col-sm-4 col-lg-2">
<div class="thumbnail">
<a href="{% url 'music:picturedetail' picture.song_title %}">
<img src="{{ picture.file_type.url }}" class="img-responsive">
</a>
<div class="caption">
<h6>{{picture.song_title}}</h6>
</div>
</div>
</div>
{% endfor %}
</ul>
我得到的错误是:-
NoReverseMatch at /music/1
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Request Method: GET
Request URL: http://127.0.0.1:8000/music/1
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Exception Location: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.6-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.1
Python Path:
['D:\\Django\\website',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-2.0.6-py3.6.egg',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\pytz-2018.4-py3.6.egg']
Server time: Fri, 6 Jul 2018 03:00:51 +0000
NoReverseMatch at/music/1
找不到参数为“('River',)”的“PicturedDetail”的反向。尝试了1个模式:[“音乐/(?P[0-9]+)/(?P[A-Za-z]+)”
请求方法:获取
请求URL:http://127.0.0.1:8000/music/1
Django版本:2.0.6
异常类型:NoReverseMatch
异常值:
找不到参数为“('River',)”的“PicturedDetail”的反向。尝试了1个模式:[“音乐/(?P[0-9]+)/(?P[A-Za-z]+)”
异常位置:C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site packages\django-2.0.6-py3.6.egg\django\url\resolvers.py in _reverse_,带前缀,第636行
Python可执行文件:C:\Users\dell\AppData\Local\Programs\Python\Python36-32\Python.exe
Python版本:3.6.1
Python路径:
['D:\\Django\\website',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\Python36.zip',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site packages',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site packages\\django-2.0.6-py3.6.egg',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site packages\\pytz-2018.4-py3.6.egg']
服务器时间:2018年7月6日星期五03:00:51+0000
谢谢你的解决方案它对我有效非常感谢你的解决方案它对我有效非常感谢
from django.views import generic
from .models import Album
from .models import Picture
class IndexView(generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_albums'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model=Album
template_name = 'music/detail.html '
class PicturedetailView(generic.DetailView):
model =Picture
template_name = 'music/picturedetail.html'
NoReverseMatch at /music/1
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Request Method: GET
Request URL: http://127.0.0.1:8000/music/1
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Exception Location: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.6-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.1
Python Path:
['D:\\Django\\website',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-2.0.6-py3.6.egg',
'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\pytz-2018.4-py3.6.egg']
Server time: Fri, 6 Jul 2018 03:00:51 +0000
url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),
{% url 'music:picturedetail' pk=picture.pk song_title=picture.song_title %}