0

After clicking the upload button on a form I get a bullet point beneath the 'Job ID' field stating "This field is required." even though I have values enterred into the field. Here is 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."),
            },
            NON_FIELD_ERRORS: {
                'unique_together': "%(model_name)s's %(field_labels)s are not unique.",
            }
        }

models.py:

from __future__ import unicode_literals

from django.db import models
from django.utils import timezone

class Job(models.Model):
    user = models.ForeignKey('auth.User')
    original_file = models.FileField()
    jobID = models.CharField(max_length=50)
    rev = models.IntegerField()
    create_date = models.DateTimeField(default = timezone.now)

    def save(self):
        "Get last value of rev considering jobID and User and increment"
        "see https://stackoverflow.com/questions/1065089/auto-increment-a-value-in-django-with-respect-to-the-previous-one"
        last_rev = Job.objects.filter(user=self.user).filter(jobID=self.jobID).order_by('-rev')
        if last_rev:
            rev = last_rev[0].rev + 1
        else:
            rev = 1
        super(Job,self).save()

    def __unicode__(self):
        return self.jobID + " rev " + str(self.rev)

    class Meta:
        indexes = [
            models.Index(fields=['user','jobID']),
        ]
        unique_together = ("user","jobID","rev")
        ordering = ['-create_date']

views.py

from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import UploadFileForm
from .models import Job

# Create your views here.
def upload(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return render(request,'precheck/prestage.html')
    else:
            form = UploadFileForm()
    return render(request, 'precheck/upload.html',{'form': form})

def success(request):
    return render(request, 'precheck/prestage.html')

upload.html

{% extends 'base.html' %}

{% block content %}

<h2>Upload File</h2>

{% if error %}
{{ error }}
<br />
<br />
{% endif %}

<form method = "POST" action="{% url 'precheck:upload' %}">
    {% csrf_token %}

    {{ form.as_p }}

    <br />
    <input type="submit" class="btn btn-primary" value="Upload">

</form>

{% endblock %}

I tried manually building the form (ie not using a modelform) with the same results. Thanks for the help.

EDIT: Changing the form to include enctype="multipart/form-data" did the trick:

<form method = "POST" action="{% url 'precheck:upload' %}" enctype="multipart/form-data">
chaserchap
  • 49
  • 9
  • As shown in your post, `class Meta` is NOT indented underneath `class UploadFileForm`. Is this correct? – John Gordon Jul 06 '17 at 16:54
  • what hapens if you set blank=True? `jobID = models.CharField(max_length=50,blank=True)` – hansTheFranz Jul 06 '17 at 16:56
  • @John `class Meta` is supposed to be indented. It's correct in my code. – chaserchap Jul 06 '17 at 17:04
  • @hans I still get 'This field is required.' – chaserchap Jul 06 '17 at 17:05
  • There isn't any JavaScript intercepting your form submit, right? – FamousJameous Jul 06 '17 at 17:09
  • Please tell me if you add this two lines into your views.py under if form is valid what is the terminal telling you? `content_data = form.cleaned_data.get("JobID")` `print content_data`. With `print` statements you can usually narrow down the problem. – hansTheFranz Jul 06 '17 at 17:14
  • @Famous I'm using bootstrap around the html file, but nothing that should be intercepting the submit. – chaserchap Jul 06 '17 at 17:32
  • @hans It's not printing anything, which makes me think the form.is_valid is possibly throwing the error. Could it be checking for 'rev' (part of the model) and throwing an error because it isn't assigned yet? – chaserchap Jul 06 '17 at 17:34
  • @hans I added the following after the if form.is_valid block: `else: print form.errors.as_json()`. This printed `{"original_file": [{"message": "This field is required.","code":"required"}]}` in the terminal. – chaserchap Jul 06 '17 at 17:40

1 Answers1

0

The error is not for jobID, but for original_file. This is because you are not using the enctype="multipart/form-data" attribute on the form element.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895