您好我收到此错误:/ sss / edit /中的KeyError 'PK' 我知道keyError意味着dict中没有键。但我不明白为什么它会给我。它看起来很简单,但我得到了这个错误。这是我的代码
class PostUpdateView(UpdateView):
model = Post
form_class = PostForm
template_name = 'main/edit.html'
def form_valid(self, form):
self.object = form.save(commit=False)
# Any manual settings go here
self.object.save()
return HttpResponseRedirect(self.object.get_absolute_url())
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
post = Post.objects.get(pk=kwargs['pk'])
if request.user.has_perm('main.change_post') and post.created_by == request.user:
return super(PostUpdateView, self).dispatch(request, *args, **kwargs)
else:
return http.HttpForbidden()
我从post=Post.objects.get(pk=kwargs['pk'])
我也有这个模型
class Post(models.Model):
pub_date = models.DateTimeField(auto_now_add = True)
先谢谢
答案 0 :(得分:1)
如果您在urls.py中定义了pk
,则只能访问url(r'^sss/edit/(?P<pk>\d+)$', PostUpdateView.as_view())
:
<Button></Button>