0
雖然增加後到我的博客,我收到以下錯誤Post matching query does not exist.
我最近改變了所有的蛞蝓領域,但是這沒有關係,我相信Django的崗匹配查詢不存在
URL.py
url(r'^$', views.PostListView.as_view(),name='home'),
url(r'^(?P<slug>[\w-]+)/$', views.detail, name='detail'),
url(r'^editPost/(?P<slug>[\w-]+)/$', views.edit_post, name='edit_post'),
url(r'^deletePost/(?P<slug>[\w-]+)/$', views.delete_post, name='delete_post'),
url(r'^addPost/$', views.add_post, name='add_post'),
views.py
def detail(request, slug):
post = Post.objects.get(slug=slug)
comments=post.comment_set.all()
forms=CommentForm
if request.method == 'POST':
form=CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.title = post
print comment
comment.save()
else:
print form.errors
else:
form = PostForm()
return render(request, "blog_detail.html", {'forms':forms,'post': post,'comments':comments})
def add_post(request):
forms = PostForm
if request.method == 'POST':
form = PostForm(request.POST)
print form
if form.is_valid():
uncommit = form.save(commit=False)
title= form.cleaned_data['title']
description=form.cleaned_data['description']
form.save()
return redirect("home")
else:
print form.errors
else:
form = PostForm()
return render(request, "add_post.html", {'forms': forms})
當我點擊add選項時,會生成以下錯誤。模板add_post
add_post.html
{% block blog_post %}
<form method="POST" action="{% url 'add_post' %}">{% csrf_token %}
{{ forms|crispy }}
<input type="submit">
</form>
{% endblock %}
回溯
Internal Server Error: /addPost/
Traceback (most recent call last):
File "/home/amogh/PycharmProjects/interview/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/amogh/PycharmProjects/interview/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/amogh/PycharmProjects/interview/main_app/blog/views.py", line 40, in detail
post = Post.objects.get(slug=slug)
File "/home/amogh/PycharmProjects/interview/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/amogh/PycharmProjects/interview/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 387, in get
self.model._meta.object_name
DoesNotExist: Post matching query does not exist.
我真的不知道什麼是錯誤的,因爲查詢不存在...任何幫助表示讚賞...預先感謝
檢查是否hace Post對象與給定的slu 012 – Gocht
你可以用你的stacktrace編輯問題嗎?另外,在你的django shell中,你可以執行'Post.objects.get(slug = slug)'並且成功獲得一個Post對象嗎? – karthikr