Django的新手。 使用基于类的通用视图“创建”新歌曲并将其添加到数据库中。
似乎是错误的url查找。非常感谢您的帮助。
urls.py(查看最后一个URL-发生错误的地方)
from django.conf.urls import url
import views
app_name= 'music' # required for namespace
urlpatterns= [
#/music/
url(r'^$', views.index, name='index'),
#/music/register
url(r'^register/$', views.UserFormView.as_view(), name='register'),
# /music/71/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
#/music/album/add/
url(r'album/add/$', views.AlbumCreate.as_view(), name='album-add'),
#/music/album/2/
url(r'album/(?P<pk>[0-9]+)/$',views.AlbumUpdate.as_view(), name='album-update'),
#/music/album/2/delete/
url(r'album/(?P<pk>[0-9]+)/delete/$',views.AlbumDelete.as_view(), name='album-delete'),
#/music/login/
url(r'^login/$',views.LoginView.as_view(), name='login'),
#/music/logout/
url(r'^logout/$',views.logout_user,name='logout'),
#/music/songs/all/
url(r'^songs/(?P<filter_by>[a-zA_Z]+)/$', views.songs, name='songs'),
#/music/71/add/
url(r'^(?P<pk>[0-9]+)/add/$', views.SongCreate.as_view(), name='song-add'),
]
尝试转到/ music // add /网址以创建新歌曲。错误显示“详细” URL上没有提供参数。
views.py
class DetailView(LoginRequiredMixin, generic.DetailView):
login_url= 'music:login'
redirect_field_name= 'go_to'
model= Album
template_name= 'music/detail.html'
class SongCreate(LoginRequiredMixin, CreateView):
login_url='music:login'
redirect_field_name='go_to'
model=Song
fields=['song_title','audio_file']
def form_valid(self, form):
temp= form.save(commit=False)
temp.album= self.request.album
temp.save()
return super(SongCreate, self).form_valid(form)
转到URL上的错误:http://127.0.0.1:8000/music/17/add/是
Request Method: GET
Request URL: http://127.0.0.1:8000/music/17/add/
Django Version: 1.11.12
Exception Type: NoReverseMatch
Exception Value: Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: [u'music/(?P<pk>[0-9]+)/$']
Exception Location: /usr/lib64/python2.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable: /usr/bin/python
Python Version: 2.7.15
根项目根目录中template / music中的模板是在成功完成URL / music // add /后加载的模板,它是 song_create.html ,这是基于通用类的视图所必需的。
{% extends 'music/album_thumbnail.html' %}
{% block title %}Add a Song{% endblock %}
{% load staticfiles %}
{% block right_to_thumbnail %}
<ul class="nav nav-pills" >
<li role="presentation" >
<a href="{% url 'music:detail' album.id %}">This Album</a>
</li>
<li role="presentation" class="active">
<a href="#">Add a Song</a>
</li>
</ul>
<div class="panel panel-default">
<div class="panel-body">
<h3> Add a new Song here</h3>
{% if error_messege %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form class="form-horizontal" role="form" acion="" enctype="multipart/form-data">
{% csrf_token %}
<input type="hidden" name="album" value="{{ album }}">
{% include 'music/form_template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
{%endblock%}
form-template.html文件如下
{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2-col-sm-10">
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">
{{field.label_tag}}
</label>
<div class="col-sm-10">{{ field }}</div>
</div>
{% endfor %}
答案 0 :(得分:0)
尝试:
url(r'^(?P<pk>[0-9]+)/add/$', views.SongCreate.as_view(success_url=reverse('success-url')), name='song-add'),
答案 1 :(得分:0)
您没有在CreateView中进行任何操作来从URL获取相册并将其传递给模板。您需要覆盖get_context_data
:
def get_context_data(self, *args, **kwargs):
kwargs['album'] = Album.objects.get(pk=self.kwargs['pk'])
return super().get_context_data(*args, **kwargs)
您还需要修改form_valid
,因为没有self.request.album
这样的东西。
def form_valid(self, form):
temp = form.save(commit=False)
temp.album = Album.objects.get(pk=self.kwargs['pk'])
temp.save()
return super(SongCreate, self).form_valid(form)