Home > OS >  Django - 'This field is required on form' load
Django - 'This field is required on form' load

Time:12-31

I have a Django view that shows two create forms.

Whenever the page loads all of the input fields display - 'This field is required".

enter image description here

Template code

{% block content %}


<form method="post" enctype="multipart/form-data">
  {% csrf_token %}
  
  {{ listing_create_form.as_p }}
  
  {{ listing_media_form.as_p }}

  <button type="submit">Submit Form</button>
</form>

{% endblock %}

views.py

@login_required
def createListing(request):
    listing_create_form = ListingCreateForm(request.POST or None, request.FILES)
    listing_media_form = ListingMediaForm(request.POST or None, request.FILES)
    if request.method == 'POST':
        if listing_create_form.is_valid() and listing_media_form.is_valid():
            listing_create_form.instance.created_by = request.user
            form = listing_create_form.save()
            form.save()
            new_listing_id = form.pk

            # loop over images to upload multiple
            for image_uploaded in request.FILES.getlist('image'):
                image_instance = ListingMedia.objects.create(listing=form, image=image_uploaded)
                image_instance.save()
            return redirect('boat_listings')
    context = {'listing_create_form': listing_create_form, 'listing_media_form': listing_media_form}
    return render(request, 'listings/listing_create_form.html', context)

forms.py

class ListingCreateForm(forms.ModelForm):
    class Meta:
        model = Listings
        widgets = {
            "featured_image": forms.FileInput(
                attrs={
                    "enctype": "multipart/form-data"
                }
            ),
        }
        fields = "__all__"
        exclude = ("created_by", "created_on", "last_modified",)

class ListingMediaForm(forms.ModelForm):
    class Meta:
        # image = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
        widgets = {
            "image": forms.ClearableFileInput(
                attrs={
                    "multiple": True
                }
            ),
        }
        model = ListingMedia
        fields = ['image']

Django template should render without field required message before user has inputted invalid inputs.

CodePudding user response:

it looks like when you initialise form instances, it gets data and tries to validate so that's what you are receiving error messages.

change your view:

@login_required
def createListing(request):
    if request.method == 'POST':
        listing_create_form = ListingCreateForm(request.POST, request.FILES)
        listing_media_form = ListingMediaForm(request.POST, request.FILES)
        if listing_create_form.is_valid() and listing_media_form.is_valid():
            listing_create_form.instance.created_by = request.user
            form = listing_create_form.save()
            form.save()
            new_listing_id = form.pk

            # loop over images to upload multiple
            for image_uploaded in request.FILES.getlist('image'):
                image_instance = ListingMedia.objects.create(listing=form, image=image_uploaded)
                image_instance.save()
            return redirect('boat_listings')
    else:
        listing_create_form = ListingCreateForm()
        listing_media_form = ListingMediaForm()
    context = {'listing_create_form': listing_create_form, 'listing_media_form': listing_media_form}
    return render(request, 'listings/listing_create_form.html', context)
  • Related