Ich versuche, eine Datei auf den Server mit Django hochladen, aber die form.is_valid
ist immer Rückkehr Flase. Dann habe ich den Formularwert wie folgt ausgegeben.(Django) Upload-Datei immer fehlgeschlagen
Druckform in views.py
<tr><th><label for="id_country">country:</label></th><td><input type="text" name="country" value="DE" required id="id_country" maxlength="200" /></td></tr>
<tr><th><label for="id_email">email:</label></th><td><input type="email" name="email" value="[email protected]" required id="id_email" /></td></tr>
<tr><th><label for="id_filetoupload">fileToUpload:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="file" name="filetoupload" required id="id_filetoupload" /></td>
</tr>
form.py
from django import forms
class UserInfo(forms.Form):
country = forms.CharField(label='country', max_length=200)
email = forms.EmailField(label='email')
filetoupload = forms.FileField(label='fileToUpload')
urls.py
from django.conf.urls import url
from loadfileweb.views import receiveinfo
urlpatterns = [
url(r'^home/', receiveinfo),
url(r'^home/submit', receiveinfo)
]
views.py
def receiveinfo(request):
if request.method == 'POST':
form = UserInfo(request.POST, request.FILES)
print "form info"
print form
print form.is_valid()
if form.is_valid():
# process the data in form.cleaned_data as required
country = form.cleaned_data['country']
email = form.cleaned_data['email']
print email
print "------------ get email -----------"
# redirect to a new URL:
return HttpResponse('/thanks/')
else:
print "----- form isn't valid -------"
return render(request, 'home.html', {'form': form})
else:
print "--------- request.method != 'POST'------------"
form = UserInfo()
return render(request, 'home.html', {'form': form})
home.html
<form class='UserInfo' action="submit" method="post" enctype="multipart/form-data">{% csrf_token %}
E-mail: <input type="email" name="email" autofocus><br>
{{ form.email.errors}}<br>
Country: <input list="countrycode" name="country">
{{ form.country.errors}}<br>
<datalist id="countrycode">
<option value="DE">
<option value="ES">
<option value="FR">
<option value="IT">
<option value="UK">
<option value="JP">
<option value="CA">
<option value="USA">
</datalist>
<br>
Select a file to upload:
<input type="file" name="fileToUpload" id="fileToUpload" ><br>
{{ form.filetoupload.errors}}<br>
<input type="submit"><br>
</form>
ich auf Google und viele Foren und noch mit ihm haben Problem gesucht. Hoffe auf deine Hilfe, Danke!
Können Sie den Ausdruck von form.errors nach .is_valid() zurückgeben False? –
Ja, das Ausgabeergebnis ist '
- filetupload
- Dieses Feld wird benötigt.
'. Aber ich habe definitiv eine Datei zum Hochladen ausgewählt, ich weiß nicht warum. –