eh .. Ich weiß nicht, wie man die Frage beschreiben, weshalb ich keine richtige Lösung finden konnte. Ich werde es so genau wie möglich erklären. Ich möchte das Bild in einem Artikel nur hochladen, wenn ich möchte, aber wenn ich nicht möchte, erinnere mich das Formular "Dieses Feld ist erforderlich". Wie repariere ich es? Hier sind meine verwandten Codes:Django 1.8.6 wie Bild optional hochladen
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})
in 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>
Wenn ich zum Hochladen haben, wie lade ich ein erstes Bild hoch? Und wenn ich mehrere Bilder hochladen möchte, hilft das FilerImageField?