我从api获取数据,所以无法将数据存储在database.when中,单击提交按钮时,错误为MultiValueDictKeyError,有时会出现其他错误。谁能帮助我摆脱困境? 这是我的代码
Templates.html:
<div>
<form method="POST">
{% csrf_token %}
<i class="fas fa-search"></i>
<input type="text" name="search" class="form-control" id="input" aria-label="Username" placeholder="Search any movie ...">
<button type="submit" name='submit' value="search" id="button">Search</button>
</form>
</div>
<div class="album py-4 bg-black">
<div class="wrap" class="container-fluid">
<div class="grid">
{% for movies in movie %}
<div>
<div class="card h-100 shadow-sm d-flex flex-row">
{% if movies.poster %}
<form action="/templates/search" method="POST">
{% csrf_token %}
<a href="/cast/{{ movies.id }}" >
<img for="poster" class="bd-placeholder-img card-img-top w-100 h-100" style="max-width: 250px; object-fit: cover" src="https://image.tmdb.org/t/p/w500/{{ movies.poster }}" alt="NO"></img>
{% else %}
<img class="bd-placeholder-img card-img-top w-100 h-100" style="max-width: 200px; object-fit: cover" src="https://via.placeholder.com/150/000000/FFFFFF/?text={{ movies.title }}" alt="NO"></img>
</a>
{% endif %}
<div class="card-body">
<a href="/cast/{{ movies.id }}" >
<p for="title" name="title" id="title" class="card-text">{{ movies.title }}</p>
</a>
<div class="d-flex justify-content-between align-items-center">
<p for="overview" class="card" id="overview" >{{ movies.overview|slice:":140" }}....</p>
</div>
<div class="rating_search">
<span class="fa fa-star checked"></span><br>
<div class="vote_average">{{ movies.vote_average }}</div>
<small for="release" id="date" class="text-muted ml-auto">{{ movies.release }}</small>
</div>
<button class="taskAdd" name="taskAdd" type="submit"><i class="fa fa-plus icon"></i>Add task</button><br>
</div>
</div>
</div>
</form>
{% endfor %}
</div>
</div>
这是views.py文件,我认为这是错误,但我无法解决该错误。 我使用不同的方法,但是我遇到了错误。
views.py:
def index(request):
if request.method=="POST":
if "taskAdd" in request.POST:
try:
title=request.POST["title"]
print(title)
overview=request.POST["overview"]
release=str(request.POST["release"])
poster=request.POST["poster"]
user = Movie(title=title,overview=overview,release=release,poster=poster)
user.save()
except:
pass
return redirect('/search')
models.py:
class Movie(models.Model):
title=models.CharField(verbose_name='movie_name',max_length=30)
overview=models.TextField()
release=models.DateField()
poster=models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100)
def __str__(self):
return self.title