简体   繁体   English

如何在 django Detailview 中使用多个表单

[英]how to work with Multiple forms in django Detailview

I have a comment section in django blog and there are two forms one is for comment another is for reply to the comment but the comment form is working fine and reply form doesn't work!我在 django 博客中有一个评论部分,有两种形式,一种是评论,另一种是回复评论,但评论表格工作正常,回复表格不起作用! i was trying to do but getting error... IntegrityError at /page/9/ FOREIGN KEY constraint failed...我试图做,但得到错误... IntegrityError at /page/9/ FOREIGN KEY 约束失败...

appreciate to your help :) Thank you.感谢您的帮助:) 谢谢。

views.py
class PostDetailView(DetailView):
model = Post
template_name = "post_detail.html"
context_object_name = 'post'
form = CommentForm()

def get_object(self):
    obj = super().get_object()
    if self.request.user.is_authenticated:
        PostView.objects.get_or_create(
            user=self.request.user,
            post=obj
        )
    return obj

def get_context_data(self, **kwargs):
    category_count = get_category_count()
    most_recent = Post.objects.order_by('-timestamp')[:3]
    context = super().get_context_data(**kwargs)
    context['most_recent'] = most_recent
    context['page_request_var'] = "page"
    context['category_count'] = category_count
    context['form'] = self.form
    return context



def post(self, request, *args, **kwargs):
    form = CommentForm(request.POST)

    form = ReplyForm(request.POST)# how to work with this form like above from 

    if form.is_valid():
        post = self.get_object()
        form.instance.user = request.user
        form.instance.post = post
        form.save()
        return redirect(reverse("post-detail", kwargs={
            'pk': post.pk
        }))

  models.py

class Reply(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True)
    content = models.TextField()
    comment = models.ForeignKey('Comment', related_name='replies',default=False, null=True, 
                                     on_delete=models.CASCADE)

    def __str__(self):
         return self.content

class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True)
    content = models.TextField()
    post = models.ForeignKey('Post', related_name='comments', default=False, 
                              on_delete=models.CASCADE)

    def __str__(self):
         return self.content

You might find it easier if you did not struggle with trying to persuade a Class-based view to do what it was not intended to do, and instead used a plain old Function-based view.如果您不费力地试图说服基于类的视图做它不打算做的事情,而是使用普通的旧的基于函数的视图,您可能会发现它更容易。

Here is a two-form view.这是一个两种形式的视图。 The code has been refactored into what I regard as a better pattern, to validate both forms and redisplay if anything is wrong at the top, and then you just do the actual work to create and save the objects at the bottom.代码已被重构为我认为更好的模式,以验证两个表单并重新显示顶部是否有任何错误,然后您只需执行实际工作来创建和保存底部的对象。

def receive_uncoated( request): #Function based view

    # let's put form instantiation in one place not two, and reverse the usual test. This
    # makes for a much nicer layout with actions not sandwiched by "boilerplate" 
    # note any([ ]) forces invocation of both .is_valid() methods 
    # so errors in second form get shown even in presence of errors in first

    args = [request.POST, ] if request.method == "POST" else []
    batchform = CreateUncWaferBatchForm( *args )
    po_form =  CreateUncWaferPOForm(     *args, prefix='po')
    if request.method != "POST" or any(  
        [ not batchform.is_valid(), not po_form.is_valid() ]):

        return render(request, 'wafers/receive_uncoated.html',   # can get this out of the way at the top
            {'batchform': batchform,  
            'po_form': po_form, 
        })

    #POST, everything is valid, do the work

    # create and save some objects based on the validated forms ... 

    return redirect( 'wafers:ok' )   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM