Django 2.0版
views.py
from django.views import generic
from .models import Album
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/details.html'
details.html
{% extends 'music/base.html' %}
{% block body %}
<!-- {{album}} -->
<img src="{{album.album_logo}}">
<h1>{{album.album_title}}</h1>
<h3>{{album.artist}}</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}" method="post">
{% csrf_token %}
{% for song in album.song_set.all %}
<input type="radio" id="song{{ forloop.counter }}" name="song" value="{{ song.id }}">
<label for="song{{ forloop.counter }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="http://i.imgur.com/b9b13Rd.png" />
{% endif %}
</label><br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
{% endblock %}
index.html
{% extends 'music/base.html' %}
{% block body %}
{% if all_albums %}
<h3>Here are all my Albums:</h3>
<ul>
{% for album in all_albums %}
<li><a href="{% url 'music:details' album.id %}">{{ album.album_title }}</a></li>
{% endfor %}
</ul>
{% else %}
<h3>You don't have any albums</h3>
{% endif %}
{% endblock %}
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Viberr</title>
{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type="text/css">
<link rel="stylesheet" type="text/css" href="{% static 'music/style.css' %}" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Header -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'music:index' %}">University of Calicut</a>
</div>
<!-- Items -->
<div class="collaps navbar-collaps" id="topNavBar">
<ul class="nav navbar-nav">
<li class="active">
<a href="{% url 'music:index' %}">
<span class="glyphicon glyphicon-cd" aria-hidden="true"></span>
Albums
</a>
</li>
<li class="">
<a href="{% url 'music:index' %}">
<span class="glyphicon glyphicon-music" aria-hidden="true"></span>
Songs
</a>
</li>
</ul>
<form class="navbar-form navbar-left" role="search" method="GET" action="#">
<div>
<input type="text" class="form-control" name="q" value="">
<button type="submit" class="btn btn-default">Search</button>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="#">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
Add Album
</a>
</li>
<li class="">
<a href="#">
<span class="glyphicon glyphicon-off" aria-hidden="true"></span>
Logout
</a>
</li>
</ul>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
urls.py(音乐)
from django.conf.urls import url
from .import views
from django.urls import path
app_name = 'music'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),
]
models.py
from django.db import models
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title=models.CharField(max_length=500)
genre=models.CharField(max_length=100)
album_logo=models.CharField(max_length=1000)
def __str__(self):
return self.album_title + ' - ' + self.artist
class Song(models.Model):
album = models.ForeignKey(Album, on_delete=models.CASCADE)
file_type = models.CharField(max_length=10)
song_title = models.CharField(max_length=250)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.song_title
我附加了主页和链接页面的图像, 请帮我解决这个问题
在上面的代码中,第二页抛出了一个错误
django.urls.exceptions.NoReverseMatch:找不到“收藏夹”的反向条目。 “收藏夹”不是有效的视图函数或模式名称**
请有人帮我解决错误之处!预先感谢!
答案 0 :(得分:0)
我在这里看不到任何名称为收藏夹的url,这也是错误告诉您的信息:
app_name = 'music'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),
]
您可能复制粘贴了details.html,或者您忘记了在应用程序“音乐”中指定“收藏夹” URL
编辑: 正如您已经澄清的那样,您无法通过onw解决问题: 这样更改details.html:
<form action="{% url 'music:favorite' album.id %}" method="post">
替换为
<form action="#" method="post">
然后您应该在网站上至少要加载
。EDIT2:
观看之后,您在评论中链接的youtube视频: 所有答案都在视频及其评论中! 您没有更新details.html。请密切注意分钟1:50。 顺便说一句,我替换表单动作的解决方案也可以使用,但是输出将与您期望的稍有不同... 我希望这会帮助您入门;)