我的问题是,为什么即使加载文件,表单也会显示文件字段没有文件选择(如何更正?)
我有以下形式(相关位是FileInput小部件):
class StudentForm(forms.ModelForm):
class Meta:
model = Student
exclude = ['id',]
widgets = {
'user' : forms.HiddenInput(),
'first_name' : forms.TextInput(attrs={'class' : 'form-control'}),
'last_name' : forms.TextInput(attrs={'class' : 'form-control'}),
'email' : forms.EmailInput(attrs={'class' : 'form-control'}),
'majors' : forms.TextInput(attrs={'class' : 'form-control'}),
'university' : forms.TextInput(attrs={'class' : 'form-control'}),
'grad_year' : forms.TextInput(attrs={'class' : 'form-control',
'type' : 'number'}),
'resume' : forms.FileInput(attrs={'class' : 'form-control'}),
'transcript' : forms.FileInput(attrs={'class' : 'form-control'}),
'git_link' : forms.URLInput(attrs={'class' : 'form-control'}),
'linkedin_link' : forms.URLInput(attrs={'class' : 'form-control'}),
}
我保存/显示的视图是:
student = Student.objects.get(user=request.user)
form = StudentForm(instance=student)
if request.method == 'POST':
form = StudentForm(request.POST, request.FILES, instance=student)
if form.is_valid():
form.save()
messages.success(request, 'Profile successfully updated.')
variables['form'] = form
return render (request, 'app/profile.html', variables)
我正在通过HTML呈现它:
<form method='post' action="{% url 'add-profile' %}" enctype="multipart/form-data" data-confirm="Please confirm you want to save the current data. This will overright all previous data.">
{% csrf_token %}
<table class="table">
{{form.as_table}}
</table>
<button type="submit" class="btn btn-primary" >Update Profile</button>
</form>
我的问题是,为什么即使加载文件,表单也会显示文件字段没有文件选择(如何更正?)
感谢您的帮助
答案 0 :(得分:0)
我的解决方案(我不喜欢)一直使用{{form.field}}
,而网址字段包括{{form.field.url}}
。然后,我可以应用CSS格式来删除标准按钮,并在没有“无文件选择”文本的情况下应用我自己的格式。
可怕