2

我创建了一个带有现有模型(书)实例的模型表单。我无法更新图书记录。添加新记录很好,但是当我尝试更新时,它似乎无法找到发布者(这是一个外键)。错误是“没有发布者与给定的查询匹配。”

模型.py

类发布者(模型。模型):

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

类书(模型。模型):

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

视图.py

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 请求 URL:http: //127.0.0.1 :8000/books/bookupd/

没有发布者与给定的查询匹配。

您看到此错误是因为您的 Django 设置文件中有 DEBUG = True 。将其更改为 False,Django 将显示标准 404 页面。

网址.py

从 django.conf.urls.defaults 导入 *

从 django.views.generic.simple 导入 direct_to_template

从 mysite10.books.views 导入 about_pages、books_by_publisher、authorcontact、bookcontact、booklisting、authorcontactupd

从 django.views.generic 导入 list_detail

从 mysite10.books.models 导入出版商,书籍

从 django.contrib 导入管理员

admin.autodiscover()

def get_books():

return Book.objects.all()

出版商信息 = {

'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 = 模式('',

(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+)/$'

视图.py

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})

网址.py

(r'^books/bookupd/(\d+)/$', 'mysite10.books.views.book_upd'),

4

1 回答 1

0

缺少一些信息,例如您的 urls.py 中的内容。你也可以发一下吗?您是否在数据库中检查了记录实际上没有更新?(错误可能是处理重定向的结果)


您的编辑不够:-您是否检查了数据库以查看记录是否已更新?- 请粘贴整个 urls.py,例如有趣的是看看 /contact/created 映射到什么,以防它成功,或者你是否有一些 publisher.get() 方法

此外,回溯还可以提供很多有用的信息来确定问题的根源。


即使您收到错误,您是否检查过数据库中的对象是否已更新?

您可以尝试删除“oreilly_books”部分(或至少是查询集部分)并尝试在没有它的情况下做同样的事情吗?

于 2009-03-19T09:53:41.440 回答