I've created a form with an image upload in Django. It works on my local machine but something must be misconfigured on the server? When I submit the form it just reloads a blank form and nothing is entered into the database. My settings file:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
My View
def add_book_view(request):
if request.method == 'POST':
form = AddBookForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('books:book_list')
else:
form = AddBookForm()
return render(request, 'settings/add_book.html', {'form' : form})
My form
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-font">
Title:
</div>
<input class="settings-form-input" type="text" name="title" maxlength="256"
required="" id="id_title">
<div class="form-font">
Author(s):
</div>
<input class="settings-form-input" type="text" name="authors" maxlength="256"
required="" id="id_authors">
<div class="form-font">
Genre:
</div>
<div class="settings-form-input">
<select name="genre" id="id_genre">
<option value="0" selected="">None</option>
<option value="1">Action-Adventure</option>
<option value="2">Autobigraphy</option>
<option value="3">Biography</option>
<option value="4">Classic</option>
<option value="5">Drama</option>
<option value="6">Fantasy</option>
<option value="7">Fiction</option>
<option value="8">Historical</option>
<option value="9">Mystery</option>
<option value="10">Non-Fiction</option>
<option value="11">Romance</option>
<option value="12">Science-Fiction</option>
</select>
</div>
<br/>
<div class="form-font">
Book Cover:
</div>
<input type="file" name="book_cover" accept="image/*" required="" id="id_book_cover">
<br/><br/>
<div class="form-font">
Description:
</div>
<textarea class="settings-form-textarea" name="description" required=""
id="id_description"></textarea>
<input type="hidden" name="added_by" id="id_added_by" value="{{ user.first_name}}
{{ user.last_name }}">
<div class="form-button-container">
<input type="submit" class="form-button" value="Add Book">
</div>
</form>
I have pillow in my virtual environment
I set my static and media urls on the WEB section. Any ideas what I'm doing wrong? Thanks.