我创建了一个带有实例的modelForm到现有模型(Book)。我无法更新图书记录。添加新记录很好但是当我尝试更新时,它似乎无法找到发布者(这是一个外键)。错误是“No Publisher与给定查询匹配。”
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Meta:
ordering = ["name"]
类作者(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField(blank=True, verbose_name='e-mail')
objects = models.Manager()
sel_objects=AuthorManager()
def __unicode__(self):
return self.first_name+' '+ self.last_name
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField(blank=True, null=True)
num_pages = models.IntegerField(blank=True, null=True)
类BookForm(ModelForm):
class Meta:
model = Book
def authorcontactupd(request,id):
if request.method == 'POST':
a=Author.objects.get(pk=int(id))
form = AuthorForm(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('/contact/created')
else:
a=Author.objects.get(pk=int(id))
form = AuthorForm(instance=a)
return render_to_response('author_form.html', {'form': form})
找不到页面(404) 请求方法:POST 请求网址:http://127.0.0.1:8000/books/bookupd/
没有发布商匹配给定的查询。
您看到此错误,因为您的Django设置文件中有DEBUG = True。将其更改为False,Django将显示标准的404页面。
来自django.conf.urls.defaults import *
来自django.views.generic.simple import direct_to_template
来自mysite10.books.views的导入about_pages,books_by_publisher,authorcontact,bookcontact,booklisting,authorcontactupd
来自django.views.generic import list_detail
来自mysite10.books.models import Publisher,Book
来自django.contrib import admin
admin.autodiscover()
def get_books():
return Book.objects.all()
publisher_info = {
'queryset': Publisher.objects.all(),
'template_name':'books/publisher_publisher_list_page.html',
'template_object_name': 'publisher',
'extra_context': {'book_list': Book.objects.all},
}
book_info = {
'queryset': Book.objects.order_by('-publication_date'),
'template_name':'books/publisher_publisher_list_page.html',
'template_object_name': 'book',
'extra_context': {'publisher_list': Publisher.objects.all},
}
oreilly_books = {
'queryset': Book.objects.filter(publisher__name="O'Reilly"),
'template_name':'books/publisher_publisher_list_page.html',
'template_object_name': 'book',
'extra_context': {'publisher_list': Publisher.objects.all},
}
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^polls/', include('mysite10.polls.urls')),
(r'^search-form/$', 'mysite10.views.search_form'),
(r'^search/$', 'mysite10.views.search'),
(r'^contact/$', 'mysite10.contact.views.contact'),
(r'^contact/thanks2/(\d+)$', 'mysite10.contact.views.thanks2'),
(r'^contact/thanks/$', 'mysite10.contact.views.thanks'),
(r'^publishers/$', list_detail.object_list, publisher_info),
(r'^books/$', list_detail.object_list, book_info),
(r'^books/oreilly/$', list_detail.object_list, oreilly_books),
(r'^books/(\w+)/$', books_by_publisher),
(r'^author/$', authorcontact),
(r'^authorupd/(\d+)/$', authorcontactupd),
(r'^contact/created/$', 'mysite10.books.views.created'),
(r'^bookform/$', bookcontact),
(r'^contact/bookscreated/$', 'mysite10.books.views.books_created'),
(r'^booklist/$', 'mysite10.books.views.booklisting'),
(r'^books/bookupd/(\d+)$', 'mysite10.books.views.book_upd'),
)
我终于得到了以下代码。 urls.py中的错误,因为在$之前缺少正斜杠。 修改为(r'^ books / bookupd /(\ d +)/ $'
def book_upd(request,id):
if request.method == 'POST':
a=Book.objects.get(pk=int(id))
form = BookForm(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('/contact/bookscreated')
else:
a=Book.objects.get(pk=int(id))
form = BookForm(instance=a)
return render_to_response('book_form.html', {'form': form})
(r'^ books / bookupd /(\ d +)/ $','mysite10.books.views.book_upd'),
答案 0 :(得分:0)
有一些缺失的信息,比如你在urls.py中的内容。你也可以发布它吗?您是否在数据库中检查了记录实际上没有更新? (错误可能是处理重定向的结果)
您的修改不够: - 您是否检查了数据库以查看记录是否已更新? - 请粘贴整个urls.py,例如,看看/ contact / created映射到哪个成功,或者是否有一些publisher.get()方法很有趣
此外,回溯还可以提供有关问题根源的大量有用信息。
您是否检查了对象是否在数据库中更新,即使您收到错误?
您可以尝试删除“oreilly_books”部分(或至少是查询集部分)并尝试不使用它吗?