0

I actually had an idea to make an app where a bulk of files upload can be possible at once, but I got some difficulties on how to implement that on the model and for now I'm in need of help.

And I'm currently trying to solve this error.

I get these errors:

Internal Server Error: /upload
Traceback (most recent call last):
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/records/views.py", line 123, in send_files
    form.save()
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/forms/models.py", line 460, in save
    raise ValueError(
ValueError: The GoCustomerRegistration could not be created because the data didn't validate.
[25/Aug/2021 13:02:31] "POST /upload HTTP/1.1" 500 70627
Internal Server Error: /upload
Traceback (most recent call last):
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/records/views.py", line 123, in send_files
    form.save()
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/forms/models.py", line 460, in save
    raise ValueError(
ValueError: The GoCustomerRegistration could not be created because the data didn't validate.
[25/Aug/2021 13:06:28] "POST /upload HTTP/1.1" 500 70627
Internal Server Error: /upload
Traceback (most recent call last):
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/records/views.py", line 123, in send_files
    form.save()
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/forms/models.py", line 460, in save
    raise ValueError(
ValueError: The GoCustomerRegistration could not be created because the data didn't validate.
[25/Aug/2021 13:06:30] "POST /upload HTTP/1.1" 500 70627
[25/Aug/2021 13:06:34] "GET /upload HTTP/1.1" 200 1906
Internal Server Error: /upload
Traceback (most recent call last):
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/records/views.py", line 123, in send_files
    form.save()
  File "/home/yokwejuste/PycharmProjects/DjangoProjects/Internship/go_immigration/venv/lib/python3.9/site-packages/django/forms/models.py", line 460, in save
    raise ValueError(
ValueError: The GoCustomerRegistration could not be created because the data didn't validate.
[25/Aug/2021 13:06:58] "POST /upload HTTP/1.1" 500 70641

Models.py:

class GoCustomerRegistration(models.Model):
    name = models.CharField(max_length=300, verbose_name='Full name')
    type = models.CharField(max_length=20, verbose_name='Customer Type')
    destination = models.CharField(max_length=30, null=False, verbose_name='Destination')
    time_of_submission = models.DateTimeField(auto_now_add=True, null=False)
    registered_by = models.ForeignKey(GoUser, on_delete=models.CASCADE)
    age = models.IntegerField(verbose_name="Age", null=False)
    photo = models.ImageField(max_length=10000, verbose_name='Customer Picture',
                          default='profile1.png', upload_to='customers/profiles/')
    documents = models.FileField(max_length=1000, verbose_name='Documents',
                             null=False, default=None,
                             upload_to=f'customers/files/%Y/%m/%d')

    class Meta:
        ordering = ["time_of_submission"]
    def __str__(self):
        return '%s --- %s' % (self.name, self.documents)

Views.py:

def send_files(request):
    context = {}
    if request.POST:
        form = GoCustomerRegistrationForm(request.POST)
        if form.is_valid:
            form.save()
            return redirect('preview')
        else:
            HttpResponse(f'Invalid data { request.user.username }')
        context['document_form'] = form

    else:
        form = GoCustomerRegistrationForm
        context['document_form'] = form

    return render(request, 'files.html', context)

urls.py:

path('upload', send_files, name='upload'),

forms.py:

class GoCustomerRegistrationForm(forms.ModelForm):
    class Meta:
        model = GoCustomerRegistration
        fields = ('name', 'type', 'destination', 'age', 'photo', 'documents')
        TYPE_CHOICES = (
            ('', 'Select a customer type'),
            ('student', 'STUDENT'),
            ('worker', 'WORKER'),
            ('tourist', 'TOURIST'),)
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control form-control-user'}),
            'age': forms.TextInput(attrs={'class': 'form-control form-control-user'}),
            'type': forms.Select(choices=TYPE_CHOICES, attrs={'class': 'form-control form-control-user'}),
            'destination': forms.TextInput(attrs={'class': 'form-control form-control-user'}),
            'photo': forms.ClearableFileInput(
            attrs={'multiple': True, 'class': 'btn btn-primary d - block btn - user w - 100'}),
            'documents': forms.ClearableFileInput(
            attrs={'multiple': True, 'class': 'btn btn-primary d - block btn - user w - 100'}),
        }
karel
  • 5,489
  • 46
  • 45
  • 50
  • Welcome to SO. In your **views.py** it should be `form.is_valid()` not `form.is_valid` and try to ask one question at a time. SO is the first place to look for an answer but the last place to post one(your last resort...) try your best to solve it yourself first by reading doc or... – Avishka Dambawinna Aug 27 '21 at 15:53

1 Answers1

0

If you want assign multiple file to a model, you should make a separated model and make a FK between them. E.g for photo:

class GoCustomerRegistration(models.Model):
    ...


class CustomerPhoto(models.Model):
    photo = models.ImageField(upload_to='customers/profiles/')

Now each customer can has multiple photos

And now for uploading multiple image in template, you can use InlineFormSet. Docs in https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#inline-formsets

Amin
  • 2,605
  • 2
  • 7
  • 15
  • What about the ValueError I'm getting at each time I want to submit using this code above, added to the fact that I wish to a bulk upload...? – yokwejuste Aug 26 '21 at 23:58