chaserchap
chaserchap

Reputation: 49

Django ModelForm error message not showing in template

I have created some custom error message following the documentation (as best as I can find) but I'm not getting any errors, let alone my custom errors. Here's my code:

forms.py

class UploadFileForm(forms.ModelForm):
    class Meta:
        model = Job
        fields = ['jobID','original_file']
        labels = {
            'jobID': _('Job ID'),
            'original_file': _('File'),
        }
        error_messages = {
            'jobID': {
                'max_length': _("Job ID is limited to 50 characters."),
                'required': _("Please provide a Job ID."),
                'invalid': _("Job ID must be a combination of letters, numbers, - and _.")
            },
            'original_file': {
                'required': _("Please provide a file."),
                'validators': _("Please ensure you are selecting a zipped (.zip) GDB."),
            },
        }
        help_texts = {
            'original_file': _('Please provide a zipped (.zip) GDB.'),
        }

upload.html

    <form method = "POST" action="{% url 'precheck:upload' %}" enctype="multipart/form-data" name="uploadForm">
        {% csrf_token %}

        {% for field in form %}
        <div>
            <strong>{{ field.errors }}</strong>
            {{ field.label_tag }} {{ field }}
            {% if field.help_text %}
                <p class ="help-text">{{ field.help_text }}</p>
            {% endif %}
        </div>
        {% endfor %}

        <br />
        <button type="button" id="uploadButton" data-loading-text="Loading..." class="btn btn-primary" autocomplete="off" style="margin: auto 20%; ">Upload</button>

    </form>

views.py

def upload(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES, user = request.user)
        if form.is_valid():
            form.save()
            request.session['jobID'] = request.POST['jobID']
            #job = Job.objects.filter(user_id = request.user.id).filter(jobID = request.POST['jobID']).latest()
            # initialize(job)
            return render(request,'precheck/run_precheck.html')
    form = UploadFileForm()
    historyList = Job.objects.filter(user_id = request.user.id)[:10]
    return render(request, 'precheck/upload.html',{'form': form, 'history': historyList})

I've included everything I think is relevant, let me know if you need anything more.

Upvotes: 0

Views: 789

Answers (1)

dirkgroten
dirkgroten

Reputation: 20672

The problem is that if the form is not valid, you're resetting the form to the initial form:

form = UploadFileForm()
historyList = Job.objects.filter(user_id = request.user.id)[:10]
return render(request, 'precheck/upload.html',{'form': form, 'history': historyList})

Your flow should render the bound form (with its errors) so it should be:

if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES, user = request.user)
    if form.is_valid():
         # do stuff for valid form
         return redirect
elif request.method == 'GET':
     form = UploadFileForm()
# flow common for GET and invalid form
return render(request, template, {'form': form})

Upvotes: 3

Related Questions