英文:
File upload using Django Forms
问题
I have a file field in MissFormModel model:
file = models.FileField(upload_to='uploads/')
My index view displays all MissFormModel instances:
def index(request):
context = {}
forms = MissFormModel.objects.all()
if forms.exists():
return render(request, 'app/index.html', {'forms': forms, 'context': context})
else:
return render(request, 'app/index.html', {'forms': None, 'context': context})
My index template:
{% for i in forms %}
<tr>
<th scope="row">{{ i.id }}</th>
<th>{{ i.sender }}</th>
<th>
{% if i.file %}
<a href="{{ i.file.url }}" class="btn btn-primary btn-sm" target="_blank">
Download
</a>
{% endif %}
</th>
</tr>
{% endfor %}
Now I am trying to implement file upload forms.
My forms.py looks like:
class SendNewRequest(ModelForm):
class Meta:
model = MissFormModel
fields = ("sender", "file")
labels = {
'sender': '',
'file': '',
}
widgets = {}
My view for file upload:
def send_new_request(request):
form = SendNewRequest()
if request.method == 'POST':
form = SendNewRequest(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('index')
return render(request, 'forms/new_request.html', {'form': form})
My new_request.html:
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
But when I've created new forms with attachments, my index page displays nothing. The same situation in the admin page. Could you explain what I am doing wrong?
英文:
I have a file field in MissFormModel model:
file = models.FileField(upload_to ='uploads/')
My index view display all MissFormModel instances:
def index(request):
context ={}
forms = MissFormModel.objects.all()
if forms.exists():
return render(request, 'app/index.html', {'forms':forms,'context':context})
else:
return render(request, 'app/index.html', {'forms':None,'context':context})
My index template:
{% for i in forms %}
<tr>
<th scope="row">{{i.id}}</th>
<th>{{i.sender}}</th>
<th>
{% if i.file %}
<a href="{{ i.file.url }}" class="btn btn-primary btn-sm" target="_blank">
Download
</a>
{% endif %}
</th>
</tr>
{% endfor %}
Now I am trying to implement file upload forms.
My forms.py looks like:
class SendNewRequest(ModelForm):
class Meta:
model = MissFormModel
fields = ("sender", "file")
labels = {
'sender':'',
'file':'',
}
widgets = {}
My view to file upload:
def send_new_request(request):
form = SendNewRequest()
if request.method == 'POST':
form = SendNewRequest(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('index')
return render(request, 'forms/new_request.html', {'form':form})
My new_request.html:
<form action="" method="POST" encytpe="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
But when I've created new forms with attachment at my index page i see nothing. The same situation in admin page. Could you explain what i am doing wrong?
答案1
得分: 1
尝试这样做
您已经初始化了空表单,然后尝试保存它。
def send_new_request(request):
if request.method == 'POST':
form = SendNewRequest(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('index')
form = SendNewRequest()
return render(request, 'forms/new_request.html', {'form': form})
英文:
try this
You have intialzied the empty form and then you are trying to save it.
def send_new_request(request):
if request.method == 'POST':
form = SendNewRequest(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('index')
form = SendNewRequest()
return render(request, 'forms/new_request.html', {'form':form})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论