这是我的views.py
from django.shortcuts import render
# Create your views here.
from forms import DocumentForm
from models import Document
def SaveDocument(request):
saved = False
if request.method == "POST":
#Get the posted form
MyDocumentForm = DocumentForm(request.POST, request.FILES)
if MyDocumentForm.is_valid():
print 'It enters here'
document = Document()
document.name = MyDocumentForm.cleaned_data["name"]
document.document = MyDocumentForm.cleaned_data["document"]
print document.document
document.save()
saved = True
else:
print 'Fails'
else:
MyDocumentForm = DocumentForm()
return render(request, 'saved.html', locals())
profile.html
<html>
<body>
<form name = "form" enctype = "multipart/form-data"
action = "{% url "SaveDocument" %}" method = "POST" >{% csrf_token %}
<div style = "max-width:470px;">
<center>
<input type = "text" style = "margin-left:20%;"
placeholder = "Name" name = "name" />
</center>
</div>
<br>
<div style = "max-width:470px;">
<center>
<input type = "file" style = "margin-left:20%;"
placeholder = "document" name = "document" />
</center>
</div>
<br>
<div style = "max-width:470px;">
<center>
<button style = "border:0px;background-color:#4285F4; margin-top:8%;
height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
<strong>Login</strong>
</button>
</center>
</div>
</form>
</body>
</html>
代码工作得很好,但似乎没有将文件保存到磁盘。打印文档显示有文档。
我可能知道我做错了什么吗?我正在按照这个例子进行微小的修改
Need a minimal Django file upload example
在此行document.document = MyDocumentForm.cleaned_data["document"]