Django formset is not valid- why not?

后端 未结 1 523
生来不讨喜
生来不讨喜 2021-01-25 19:24

I am trying to use a form to allow users to upload images to projects stored in a database in my Django project, however, I\'m currently getting console output that\'s telling m

相关标签:
1条回答
  • 2021-01-25 20:02

    Your formset does not contain any data. You need to pass request.POST and request.FILES as the first and second argument (or as the data and files keyword arguments), but only if it is an actual form submission.

    If there is no data passed into a form or formset, it is considered unbound, and will return False without checking for errors.

    The usual pattern is to pass them when request.method == 'POST', and then validate the formset:

    def upload_budget_pdfs(request, project_id):
        ...
        if request.method == 'POST':
            drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix='drawings', queryset=...)
            if drawing_formset.is_valid():
                # save formset
                return HttpResponseRedirect(...)
        else:
            drawing_formset = DrawingUploadFormset(prefix='drawings', queryset=...)
        return render(...)  # Render formset
    

    This will show a blank form on GET, a filled-in form with error messages on an unsuccessful submission, and it will save the formset and redirect on a successful submission.

    0 讨论(0)
提交回复
热议问题