我正试图通过博客文章的PK,但我遇到了困难。如果可能的话,我想传递PK而不将其放在URL中。 我试图让我的用户编辑他们的帖子,这些帖子显示在如下列表中:
我需要确保当用户在帖子下按下编辑时,它允许他们编辑该特定帖子。当我尝试获取PK时,我相信我出错的地方是get_object函数。任何有关这方面的帮助将非常感激!
查看
class EditPost(UpdateView):
model = ProjectPost
form_class = ProjectPostForm
template_name = 'howdidu/edit_post.html'
def get_object(self):
return ProjectPost.objects.get(pk=self.request.Get.get('pk'))
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.project.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
模型
class ProjectPost(models.Model):
project = models.ForeignKey(UserProject)
title = models.CharField(max_length=100)
post_overview = models.CharField(max_length=1000)
date_created = models.DateTimeField(auto_now_add=True)
post_views = models.IntegerField(default=0)
post_likes = models.IntegerField(default=0)
URL
url(r'^edit_post/$', login_required(views.EditPost.as_view()), name='edit_post'),
用于编辑帖子模板的链接
<a href="{% url 'edit_post' %}">Edit post</a>