'MultiValueDictKeyError at upload/
Good day people, a part of my project has a form action that takes user input and saves it in the database. the user has an option to either select the category from the select field or enter a new category if the category does not exit in the select field but I get "MultiValueDictKeyError at upload/" below is the template and the corresponding view to the form action: upload.html
{%extends 'base.html'%}
{%block content%}
<div class="container" id="containers">
<div class="row" style="width: 40%;">
<form action="upload" method="post" enctype="multipart/form-data">
{% csrf_token %}
<ul class="collection">
<li class="collection-item">
<div class="input-field" >
<input placeholder="Enter the product name here..." id="first_name" type="text" class="validate" name="product_name">
<label for="first_name">Product name</label>
</div>
</li>
<li class="collection-item">
<div class="input-field" >
<select class="browser-default" name="the_category">
<option value="" disabled selected>Category</option>
{% for category in categories %}
<option value="{{category.name}}">{{category.name}}</option>
{% endfor%}
</select>
</div>
</li>
<li class="collection-item">
<div class="input-field" style="margin: 5px;">
<input name="new_category" placeholder="Type here..." id="first_name" type="text" class="validate">
<label for="first_name">New Category</label>
</div>
</li>
<li class="collection-item"><div class="input-field " >
<input placeholder="Enter the price"id="username" type="number" class="validate" name="prize">
<label for="password">Price</label>
</div>
</li>
<li class="collection-item">
<div class="input-field">
<input name="locate" placeholder="Enter the location" id="first_name" type="text" class="validate">
<label for="first_name">Location</label>
</div>
</li>
<li class="collection-item">
<div class="input-field" >
<textarea id="textarea1" class="materialize-textarea" name="describe"></textarea>
<label for="textarea1">Descriptiion</label>
</div>
</li>
<li class="collection-item">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="image">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</ul>
<button class="btn waves-effect waves-light right" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
</div>
</div>
{%endblock content%}
views.py (for upload)
def upload(request):
categories=Category.objects.all()
if request.method=='POST':
images=request.FILES.get('image')
data=request.POST
location,created=LOCATION.objects.get_or_create(Location=data['locate'])
if data['the_category']!='none':
category=Category.objects.get(name=data['the_category'])
elif data['new_category']!='':
category=Category.objects.create(name=data['new_category'])
else:
category=None
models.Product.objects.create(
name=data['product_name'],
image=images,
category=category,
description=data.get('describe'),
Location=location,
price=data.get('prizes'),
created=datetime.datetime.now(),
is_available=True
)
return render(request,'upload.html',{'categories':categories})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
