URLを投稿してリンクモデルに保存したい。しかし、私はこのエラーが発生しました:
Category matching query does not exist
私のviews.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 シェルでクエリを実行すると、次のようになります。
>>> 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