0
HTML5를 사용하여 Django Form에 2 개의 파일을 업로드하려는 경우 (다중 파일 업로드를 지원하므로). 내가 직면하고있는 문제는 업로드를위한 첫 번째 목표입니다. 저장하면 두 번 저장되므로 (아래 for 루프 당) 두 개의 파일이 있다는 것을 알고 있습니다. 사전을 사용하여 이름을 반복하는 것으로 생각했지만 this keyword can't be an expression
이라는 오류가 표시됩니다. 어쩌면 이것은 단순한 것이지만, 더 필요한 것이 있으면 제공 할 수 있습니다. 참고로 파일 업로드에 forms.py를 사용하지 않고 일반 HTML <input
태그 만 사용했습니다. 감사.Django 폼에서 여러 파일을 모델로 저장
#page.html
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form_a.as_p }}
<input type="file" name="img" multiple>
<input type="submit" value="Submit" />
</form>
#models.py
def contact(request):
if request.method == 'POST':
form_a = RequestForm(request.POST, request.FILES)
if form_a.is_valid():
#assign form data to variables
saved_first_name = form_a.cleaned_data['First_Name']
saved_last_name = form_a.cleaned_data['Last_Name']
saved_department = form_a.cleaned_data['Department']
saved_attachments = request.FILES.getlist('img')
#create a dictionary representing the two Attachment Fields
tel = {'keyword1': 'Attachment_2', 'keyword1': 'Attachment_1'}
for a_file in saved_attachments:
#for every attachment that was uploaded, add each one to an Attachment Field
instance = Model(
Attachment_1=a_file,
Attachment_2=a_file
)
instance.save()
all_together_now = Model(First_Name=saved_first_name, Last_Name=saved_last_name,
Department=saved_department, Attachment_1=???, Attachment_2=???)
#save the entire form
all_together_now.save()
else:
#just return an empty form
form_a = RequestForm()
return render(request, 'vendor_db/contact.html', {'form_a': form_a})