当用户点击电影并将其保存到模型时,我正在尝试获取{{ id }} 。我不能使用 modelForm 因为 id 参数来自 html 模板。有什么建议么?
也许,有一种不同的方法来获取该电影用户点击的 id?
顺便说一句,我正在使用 tmdb api。
{% for id, poster in poster_id_zip %}
<div style="display: inline-block; margin: 10px;" class="">
<a href="{{ movie_details }}"><img src="{{ poster }}" alt=""></a>
<form class="add-movie-form" method="POST">
{% csrf_token %}
<input type="hidden" name="movie-id" value="{{ id }}">
<button style="display: block; margin: 5px auto 0 auto;" type="submit" name="add">Add</button>
</form>
</div>
{% endfor %}
视图.py
def index(request):
api_key = "api_key"
image_base_url = "https://image.tmdb.org/t/p/"
if request.method == "POST":
user_query = request.POST.get('search-box', ' ')
if user_query == '':
user_query = ' '
search_movies_url = "https://api.themoviedb.org/3/search/movie?api_key={}&query={}".format(api_key, user_query)
search_results_data = requests.get(search_movies_url)
total_results = search_results_data.json().get("total_results") # for handling no search results
movie_ids, poster_path, poster_full_url = [], [], []
for movie in search_results_data.json().get("results"):
movie_ids.append(movie.get("id"))
poster_path.append(movie.get("poster_path"))
for poster in poster_path:
poster_full_url.append(image_base_url+"{}{}".format("w185", poster))
movie_details = "https://api.themoviedb.org/3/movie/{}?api_key={}".format(movie_ids, api_key)
poster_id_zip = zip(movie_ids, poster_full_url) # grouping ids with poster images
context = {"movie_ids": movie_ids, "poster_url": poster_full_url, "movie_details": movie_details, "poster_id_zip": poster_id_zip, "total_results": total_results,}
return render(request, 'homepage/movies.html', context)
return render(request, 'homepage/index.html')
模型.py
from django.db import models
# Create your models here.
class MovieId(models.Model):
movie_id = models.IntegerField()
def __str__(self):
return self.movie_id