Django3 多个文件提交到数据库
Django3 multiple files submit to database
我实际上有一个想法,可以制作一个可以同时上传大量文件的应用程序,但是我在如何在模型上实现它方面遇到了一些困难,现在我需要帮助。
我目前正在尝试解决这个错误。
我收到这些错误:
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
中的文档
我实际上有一个想法,可以制作一个可以同时上传大量文件的应用程序,但是我在如何在模型上实现它方面遇到了一些困难,现在我需要帮助。
我目前正在尝试解决这个错误。
我收到这些错误:
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