我实际上有一个想法,使一个应用程序,其中大量的文件上传可以一次,但我有一些困难,如何在模型上实现,现在我需要帮助。
我正在尝试解决这个错误。
我得到这些错误:
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'}),
}
如果您想为一个模型分配多个文件,您应该创建一个分离的模型,并在它们之间创建一个FK。例如:
class GoCustomerRegistration(models.Model):
...
class CustomerPhoto(models.Model):
photo = models.ImageField(upload_to='customers/profiles/')
现在每个顾客可以有多张照片
现在要在模板中上传多个图像,可以使用InlineFormSet
。文档在https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#inline-formsets