0

Django form doesn't validate, have no idea why. Originally I supposed it was because I added one more hidden input filed to the form(is is actually allowed? found nothing related in docs ) but I deleted it and nothing changed.

the form:

class TicketForm(forms.ModelForm):
class Meta:
    model = Ticket
    fields = ('upload','ownership','price','comment','ticket_type')
    widgets = {
        'upload': forms.FileInput(
            attrs={'id': 'fileinput', 'required': True, 'name':'pdf', 'placeholder': 'Say something...'}
        ),
    }

model:

class Ticket(models.Model):
    upload = models.FileField(upload_to='media/tickets/')
    ownership = models.ForeignKey(UserProfile)
    event = models.ForeignKey(Event)
    comment = models.CharField(max_length=2000)
    approved = models.BooleanField(default=False)
    sold = models.BooleanField(default=False)
    price = models.IntegerField(blank=False, default=0)
    TICKET_CHOICES = (
        ('DG', 'Digital'),
        ('RL', 'Real'),
    )  
    ticket_type = models.CharField(
        max_length=2,
        choices=TICKET_CHOICES,
        default='Digital',
    )

in template:

    <form method='POST' action="/upload_ticket/" class="pdf_form">
{% csrf_token %}
            {{ form.as_p }}

      <!-- <input type="hidden" id="pk_input" value="{{ selected.id}}" name="event_id"></input> -->
      <input type="submit" id='submit_hidden'>Save</input>
  </form>

In views if I remove if form.is_valid() the error is obvious The Ticket could not be created because the data didn't validate.

The file to be loaded is pdf file.

Do you have any ideas why it doesn't validate?

nimasmi
  • 3,978
  • 1
  • 25
  • 41
  • What errors does the form show? – Daniel Roseman Apr 19 '17 at 15:11
  • @DanielRoseman traceback isn't very helpful. only that error line ticket = form.save(commit=False) –  Apr 19 '17 at 15:12
  • @DanielRoseman generally it's 'ValueError at /upload_ticket/' but why should it be, it's file input and pdf –  Apr 19 '17 at 15:14
  • Possible duplicate of [Django Image Uploading - Uploads do not work](http://stackoverflow.com/questions/41274700/django-image-uploading-uploads-do-not-work) – Anonymous Apr 20 '17 at 09:56

0 Answers0