gpt4 book ai didi

python - DetailView 中的 Django ModelForm

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:42 25 4
gpt4 key购买 nike

我有一个显示 Post 的 DetailView。我现在想添加为 Post 创建 Comment 的功能。为此,我需要 DetailView 中的 CommentForm,以便我可以在与 Post 位于同一页面上时创建评论。

这可能吗,还是我应该寻找另一种方法,例如“手动”进行表单处理?

class Comment(models.Model):
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
author_name = models.CharField(max_length=255)
parent_post = models.ForeignKey('Post',related_name='comments')

class PostDetailView(BlogMixin,DetailView):
""" A view for displaying a single post """
template_name = 'post.html'
model = Post
#Add some code for the CommentForm here?

class CommentForm(forms.ModelForm):
class Meta:
model = Comment
exclude = ("parent_post","created_at")

def create_view(request, **kwargs):
if request.method == "POST":
parent_fk = request.args['parent_fk'] #Im hoping to find how this will work soon
form = CommentForm(request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.parent_post = parent_fk
new_comment.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

** 替代方案 **

我一直在尝试应用解决方案 - A better alternative - 但我明白

Exception Value: __init__() takes exactly 1 argument (3 given)
Exception Location: .../sitepackages/django/core/handlers/base.py in get_response, line 112

目前还无法追踪它。

class PostView(BlogMixin,DetailView):
""" A view for displaying a single post """
template_name = 'post.html'
model = Post
def get_context_data(self, **kwargs):
context = super(PostView, self).get_context_data(**kwargs)
context['form'] = CommentForm()
return context

class PostDetailView(View):

def get(self, request, *args, **kwargs):
view = PostView.as_view()
return view(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
view = PostComment.as_view()
return view(request, *args, **kwargs)

class PostComment( SingleObjectMixin , FormView):
template_name = 'post.html'
form_class = CommentForm
model = Post

def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super(PostComment, self).post(request, *args, **kwargs)

def get_success_url(self):
return reverse('post-detail', kwargs={'pk': self.object.pk})
class BlogMixin(object):
"""
Basic mixin for all the views. Update the context with additional
information that is required across the whole site, typically
to render base.html properly
"""
def get_context_data(self, *args, **kwargs):
context = super(BlogMixin, self).get_context_data(*args, **kwargs)
blog = Blog.get_unique()
context.update({
'blog': blog,
'active_user': users.get_current_user(),
'is_admin': users.is_current_user_admin()
})
return context

url.py: url(r'^post/(?P[\d]+)/$',views.PostDetailView.,name="post-detail"),

最佳答案

如果您想使用第一种方法,您可以将 FK 设置为隐藏字段。在您看来,您可以在将评论提交到数据库之前保存FK。像这样:

if form.is_valid():
comment = form.save(commit=False)
comment.parent_post = parent_post
comment.save()

编辑:如果您想获取评论,则可以使用按帖子过滤来获取评论的查询集。

关于python - DetailView 中的 Django ModelForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com