简体   繁体   中英

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! i was trying to do but getting error... IntegrityError at /page/9/ FOREIGN KEY constraint failed...

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' )   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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