我想发布一个网址并保存到链接模型。但我得到了这个错误:
Category matching query does not exist
我的意见.py
def category_detail(request, category_name):
if request.POST:
form = CreateLinkForm(request.POST)
user = request.user
if form.is_valid():
link = form.save(commit=False)
link.user = user
link.category = Category.objects.get(name=category_name)
link.save()
return HttpResponseRedirect(reverse('category_detail', args=[category_name]))
else:
form = CreateLinkForm()
links = Link.objects.filter(category__name=category_name)
return render_to_response("category_detail.html", {"links":links, "form":form, "category_name":category_name}, context_instance=RequestContext(request))
当我在终端 python manage.py shell 查询时,我得到了这个:
>>> from link.models import Category
>>> Category.objects.all()
[<Category: <django.db.models.fields.related.ManyRelatedManager object at 0x22dbfd0> Programlama>, <Category: <django.db.models.fields.related.ManyRelatedManager object at 0x22de110> Foto>, <Category: <django.db.models.fields.related.ManyRelatedManager object at 0x22dbfd0> Sanat>]
>>> Category.objects.get(name="Foto")
<Category: <django.db.models.fields.related.ManyRelatedManager object at 0x22dbed0> Foto>
链接型号:
class Link(models.Model):
user = models.ForeignKey(User)
posted_at = models.DateTimeField(auto_now_add=True)
url = models.URLField()
title = models.CharField(max_length=255, null=True, blank=True)
category = models.ForeignKey(Category)
def __unicode__(self):
return "%s %s %s" %(self.url, self.title, self.category)
创建链接表格:
class CreateLinkForm(ModelForm):
class Meta:
model = Link
exclude = ["user", "posted_at", "category"]
那我怎么了?为什么我得到这个错误Category matching query does not exist