正在尝试创建用于更新和删除相册实例的模型表单 - 数据库元素,遵循Bucky Roberts:Django初学者教程。
index.html如下
{% extends 'music/base.html' %}
{% block title %}Playspa{% endblock %}
{% block body %}
<form id="form_uord" method="post" >
<ol>
{% for album in object_list %} <!-- object_list is the default
where the data returned by the view is stored in the html script -->
<li>
<a href="{% url 'music:detail' album.id %}">{{ album.album_title}}</a>
       
<!--<a href="{% url 'music:album-update' album.id %}" > update </a>-->
<button id="update_button" type="button" value="update" class="btn btn-primary" onclick="upd()"> update</button>
   
<!--<a href="{% url 'music:album-delete' album.id %}" >delete</a>-->
<button id="delete_button" type="button" value="delete" class="btn btn-danger" onclick="del()">delete</button></br></br>
</li>
{% endfor %}
</ol>
</form>
<script>
function upd()
{
var f= document.getElementById("form_uord");
f.action="{% url 'music:album-update' album.id %}";
var but=document.getElementById("update_button");
but.type="submit";
but.submit();
}
function del()
{
var f= document.getElementById("form_uord");
f.action="{% url 'music:album-delete' album.id %}";
var but=document.getElementById("update_button");
but.type="submit";
but.submit();
}
</script>
{% endblock %}
&#13;
以下是app urls.py文件中的网址
url(r'^$', views.IndexView.as_view(), 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'),
以下是views.py文件
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Album
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views.generic import View
from .forms import UserForm
class IndexView(generic.ListView):
template_name= 'music/index.html'
#context_object_name= 'all_albums' this var is set when the default
#object_list name has to be changed for list returned by
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model= Album
template_name= 'music/detail.html'
class AlbumCreate(CreateView):
model=Album
fields=['artist','album_title','genre','album_logo']
class AlbumUpdate(UpdateView):
model=Album
fields=['artist','album_title','genre','album_logo']
class AlbumDelete(DeleteView):
model=Album
success_url= reverse_lazy('music:index')
class UserFormView(View):
form_class= UserForm
template_name= 'music/registration_form.html'
def get(self, request): #display blank form for registration
form= self.form_class(None)
return render(request, self.template_name, {'form':form})
def post(self, request):
form= self.form_class(request.POST)
if form.is_valid():
user= form.save(commit=False)
username= form.cleaned_data['username']
password= form.cleaned_data['password']
user.set_password(password)
user.save()
#returns user obj if login creds are correct
user= authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('music:index')
return render(request, self.template_name, {'form':form})
终端同时阅读以下内容。
Internal Server Error: /music/
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/lib64/python2.7/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/usr/lib64/python2.7/site-packages/django/template/response.py", line 84, in rendered_content
content = template.render(context, self._request)
File "/usr/lib64/python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/defaulttags.py", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/usr/lib64/python2.7/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/usr/lib64/python2.7/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
NoReverseMatch: Reverse for 'album-update' with arguments '('',)' not found. 1 pattern(s) tried: [u'music/album/(?P<pk>[0-9]+)/$']
[31/May/2018 06:09:23] "GET /music/ HTTP/1.1" 500 161922
我陷入困境,因为更改更新和删除触发器从超链接到按钮会破坏代码并成为新手我自己无法得到帮助。
答案 0 :(得分:1)
这里有很多错误,但是直接的错误是你的JS脚本中没有定义let value = doubleToInteger(data:"ENTER DOUBLE VALUE")
func doubleToInteger(data:Double)-> Int {
let doubleToString = "\(data)"
let stringToInteger = (doubleToString as NSString).integerValue
return stringToInteger
}
;仅在之前的循环中定义。您应该在循环中执行此操作,而不是在JS中使用album
标记,并在单击时将其传递给函数。