我是Python Django的新手,我正在与网址模式作斗争,我创建的网站有三个超文本标记语言页面和它们各自的三个视图,我附上了我的代码和项目结构的图像请帮助我找到解决方案:项目结构:- enter image description here
所以我的想法是,当我单击index.html时,页面将重定向到图像id为‘detail.html /id’的音乐,现在我想重定向到图像上的其他html页面,在detail.html中单击带有'song_title‘属性的picturedetail.html
代码:-
从django.db导入模型类相册(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;类图片(models.Model):album=models.ForeignKey(相册,on_delete=models.CASCADE) file_type=models.FileField()
对于album.picture_set.all中的图片,{%扩展'music/base.html‘%} {% block TITY%}专辑详细信息{% ENDBLOCK%} {% block BODY%}{ %} {{picture.song_title}} {% endfor%}
{% endblock %}
从django.conf.urls导入url。导入视图
app_name=‘音乐’;urlpatterns =[ url(r'^$',views.IndexView.as_view(),name=‘索引’),#/ PictureDetail /id/ url(r'^(?P0-9+)$',views.DetailView.as_view(),name='detail'),#for picturedetail view url(r'^(?P0-9+)/(?PA-Za-z+)',views.PicturedetailView.as_view(),name='picturedetail'),]
4.view.py:-
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
发布于 2018-07-06 12:25:34
url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),
这里有两个参数,pk和song_title。尝试以下操作:
{% url 'music:picturedetail' pk=picture.pk song_title=picture.song_title %}
https://stackoverflow.com/questions/51202613
复制相似问题