我有一个Django项目,其中应加载以显示刚刚添加的专辑的页面没有。它应该使用引用:http://127.0.0.1:8000/music/1/转到该相册的主键(自动生成),但会出错:
未找到“create_song”的反向。 'create_song'不是有效的视图函数或模式名称。
同样问题的变体,我假设,当我点击VIEW DETAILS BUTTON(如下面的index.html中所示)时,它不会加载:
<div class="caption">
<h2>{{ album.album_title }}</h2>
<h4>{{ album.artist }}</h4>
<!-- View Details -->
<a href="{% url 'music:detail' album.id %}" class="btn btn-primary btn-sm" role="button">View Details</a>
<!-- Delete Album -->
<form action="#" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="album_id" value="{{ album.id }}" />
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
各部分的代码如下:
音乐/模板/音乐/ album_form.html
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'music/form_template.html' %} <!-- how form fields and inputs are laid out-->
<div class="form-group>
<div class="col-sum-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
音乐/ views.py
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Album
#=============HOME PAGE===================
class IndexView(generic.ListView):
#specify template being used
template_name='music/index.html' #when we get a list of all albums, plug them into this template
context_object_name='all_albums' #if you don't use this variable it is automatically just object_list (which is used in index.html)
#make a query set
def get_queryset(self):
return Album.objects.all()
#=============DETAILS VIEW=============== details about one object
class DetailView(generic.DetailView):
#what model are we looking at /for
model=Album
template_name='music/detail.html'
#===============For the add album form
class AlbumCreate(CreateView):
model=Album
fields=['artist','album_title','genre','album_logo']
音乐/ urls.py
from django.contrib import admin
from django.urls import include, path
from . import views #the dot means look at the current directory - look for a module called views
app_name='music'
urlpatterns = [
#this is matching /music/
path('', views.IndexView.as_view(), name='index'),
#when you use a detail view it expects a primary key
path("<pk>/", views.DetailView.as_view(), name="detail"),
#/music/album/add - dont need to specify pk
path('album/add/', views.AlbumCreate.as_view(), name="album-add"),
]
音乐/ models.py
from django.db import models
from django.urls import reverse
# Create your models here.
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
def get_absolute_url(self):
return reverse('music:detail',kwargs={'pk':self.pk})
#music detail takes the primary key ..
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
更新:还包括details.py
{% extends 'music/base.html' %}
{% block title %}{{ album }}{% endblock %}
{% block albums_active %}active{% endblock %}
{% block body %}
<div class="container-fluid songs-container">
<div class="row">
<!-- Left Album Info -->
<div class="col-sm-4 col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<a href="{% url 'music:detail' album.id %}">
{% if album.album_logo %}
<img src="{{ album.album_logo.url }}" class="img-responsive">
{% else %}
<h3>No image to display</h3>
{% endif %}
</a>
<h1>{{ album.album_title }} <small>{{ album.genre }}</small></h1>
<h2>{{ album.artist }}</h2>
</div>
</div>
</div>
<!-- Right Song Info -->
<div class="col-sm-8 col-md-9">
<ul class="nav nav-pills" style="margin-bottom: 10px;">
<li role="presentation" class="active"><a href="{% url 'music:detail' album.id %}">View All</a></li>
<li role="presentation"><a href="{% url 'music:create_song' album.id %}">Add New Song</a></li>
</ul>
<div class="panel panel-default">
<div class="panel-body">
<h3>All Songs</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Audio File</th>
<th>Favorite</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for song in album.song_set.all %}
<tr>
<td>{{ song.song_title }}</td>
<td>
<a target="_blank" href="{{ song.audio_file.url }}">
<button type="button" class="btn btn-success btn-xs">
<span class="glyphicon glyphicon-play"></span> Play
</button>
</a>
</td>
<td>
<a href="{% url 'music:favorite' song.id %}" class="btn-favorite"><span class="glyphicon glyphicon-star {% if song.is_favorite %}active{% endif %}"></span></a>
</td>
<td>
<form action="{% url 'music:delete_song' album.id song.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="song_id" value="{{ song.id }}" />
<button type="submit" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-remove"></span> Delete
</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
{% endblock %}