这是我的from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to="documents/")
uploaded_at = models.DateTimeField(auto_now_add=True)
views.py
这是我的 class UploadFileForm(forms.Form):
file = forms.FileField()
def index(request):
documents = Document
template = loader.get_template('documents/index.html')
context = {
'documents' : documents
}
return HttpResponse(template.render(context, request))
def model_form_upload(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('documents:index')
else:
form = DocumentForm()
return render(request, 'documents/model_form_upload.html', {
'form': form
})
forms.py
我的class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('description', 'document')
my upload_template.html
和<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
<p><a href="{% url 'documents:index' %}">Return to Home</a></p>
应该在文件的末尾显示
{{1}}
我认为我只需要在views.py中进行一些更改,但是我就没有想法了。任何答案将不胜感激。