def post(request):
if request.method == 'POST':
form = NoteText(request.POST or None, request.FILES or None)
if form.is_valid():
data1 = form.cleaned_data
# data2 = subject.cleaned_data
title = data1['title']
content = data1['content']
category = data1['category']
subject_name = data1['subject_name']
photo = data1['photo']
# subject_name = data2['subject']
if not Note.objects.all().filter(title=title):
note = Note(title=title,
content=content,
created=timezone.now(),
category=category,
subject_name=subject_name,
photo = photo)
note.save()
return HttpResponseRedirect('/notebook/')
else:
return HttpResponse('The title has been used')
else:
form=NoteText()
# subject=SubjectText()
return render(request,'notebook/post.html',{'form':form})
HTML中的:post.html:
<form action="{% url 'post' %}" method="post" enctype="multipart/form-data">
<table>
{{ form.as_table }}
</table>
{% csrf_token %}
detail.html:
<div>
{% if note.photo %}
<img src="{{ note.photo.url }}" />
{% endif %}
</div>
如果我必须上传,如何上传初始图片? 如果我想上传多个图像,FilerImageField会有帮助吗?