Reputation: 121
I see a lot of similar questions but can't found any answer. I need just to simply upload or process file using Django FileField. Here is what I have:
forms.py
class UploadForm(forms.Form):
packet_file = forms.FileField()
base.html
<form action="process" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
views.py
def index(request):
form = UploadForm() # An unbound form
return render(request, 'base.html', {'form': form})
def process(request):
if request.method == 'POST': # If the form has been submitted...
form = UploadForm(request.POST, request.FILES) # A form bound to the POST data
if form.is_valid():
So the index() handles initial rendering of the empty form. When I fill it and it goes to process() it always returns
This field is required.
Next to the FileField form field.
What's wrong here? Am I missing any general Django settings like MEDIA files storage or so? Is it's even necessary at this phase when I can't validate form?
Thanks a lot TC
Upvotes: 2
Views: 1080
Reputation: 121
OK - it's my own stupidity as usual. I have two different applications and each one has its own sub-directory for templates. I thought Django uses that one within related directory first and when it's not found it goes to the project level.
But it doesn't work this way. So my I had two base.html templates for printing simple form, but the only the first one is being always called and it doesn't have enctype="multipart/form-data"
So that's it. Going back to study basic Django principles ;-)
Upvotes: 2