我创建的页面会在用户搜索后返回包含基本详细信息的电影列表。
搜索结束后,我希望用户能够点击某部电影,并获取更多相关详情。
这里是该网站的链接:(温柔,我2个月前才开始学习这些东西!哈哈)http://moniblog.pythonanywhere.com/compare/
数据来自TMDB's api和初始"通用"搜索JSON响应没有我在电影的详细信息页面上显示的具体细节,但它有一个ID将用于特定电影的搜索。
我只使用views.py来抓取/显示搜索结果,我不确定这是否是正确的方法,或者我是否应该使用模型,但是&#39 ; s可能是一个不同的问题。
forms.py:
from django import forms
class MovieSearch(forms.Form):
moviename = forms.CharField(label="Search", max_length=250)
views.py:
from django.shortcuts import render, get_object_or_404
from django.conf import settings
from .forms import MovieSearch
import tmdbsimple as tmdb
tmdb.API_KEY = settings.TMDB_API_KEY
def search_movie(request):
parsed_data = {'results': []}
if request.method == 'POST':
form = MovieSearch(request.POST)
if form.is_valid():
search = tmdb.Search()
query = form.cleaned_data['moviename']
response = search.movie(query=query)
for movie in response['results']:
parsed_data['results'].append(
{
'title': movie['title'],
'id': movie['id'],
'poster_path': movie['poster_path'],
'release_date': movie['release_date'][:-6],
'popularity': movie['popularity'],
'overview': movie['overview']
}
)
for i in range(2, 5 + 1):
response = search.movie(query=query, page=i)
for movie in response['results']:
parsed_data['results'].append(
{
'title': movie['title'],
'id': movie['id'],
'poster_path': movie['poster_path'],
'release_date': movie['release_date'][:-6],
'popularity': movie['popularity'],
'overview': movie['overview']
}
)
context = {
"form": form,
"parsed_data": parsed_data
}
return render(request, './moviecompare/movies.html', context)
else:
form = MovieSearch()
else:
form = MovieSearch()
return render(request, './moviecompare/compare.html', {"form": form})
和html:
{% extends 'moviecompare/compare.html' %}
{% block movies_returned %}
<div class="wrap">
<div class="compare-gallery">
{% for key in parsed_data.results|dictsortreversed:'release_date' %}
{% if key.poster_path and key.release_date and key.title and key.overview %}
<div class="gallery-item">
<img src="http://image.tmdb.org/t/p/w185/{{ key.poster_path }}">
<div class="gallery-text">
<div class="gallery-date"><h5><span><i class="material-icons">date_range</i></span> {{ key.release_date }}</h5></div>
<div class="gallery-title"><h3><a href="../detail/{{ key.id }}">{{ key.title }}</a></h3></div>
<div class="gallery-overview">{{ key.overview|truncatechars:80 }}</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}
我已经开始玩urls.py来获得一些工作,但到目前为止还没有运气。
网站的网址:
urlpatterns = [
url(r'^$', home, name="home"),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^compare/', include('moviecompare.urls', namespace='compare')),
url(r'^movies/', include('moviecompare.urls', namespace='movies')),
和app的urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.search_movie, name='compare'),
url(r'^(?P<movid>[0-9])+$', views.get_movie, name='movies')
]
编辑:在电影详情视图中添加我的第一次(失败)尝试:
def get_movie(request, movid=None):
instance = get_object_or_404(request, movid=movid)
context = {
'instance': instance
}
return render(request, './moviecompare/detail.html', context)
答案 0 :(得分:1)
我认为您应该尝试修复此行中的urls.py:
$1
将括号内的“+”移动到:
url(r'^(?P<movid>[0-9])+$', views.get_movie, name='movies')
答案 1 :(得分:0)
我能够通过修复每个罗马的答案的urlpattern以及一些必要的调整来实现这一点。
应用程序urls.py中的我需要更改顺序:
urlpatterns = [
url(r'^(?P<movid>[0-9]+)$', views.get_movie, name='movie_detail'),
url(r'^', views.search_movie, name='compare'),
]
在根urls.py中我有:
url(r'^compare/', include('moviecompare.urls', namespace='compare')),
在视图中添加了一个函数:
def get_movie(request, movid):
movie = tmdb.Movies(movid)
response = movie.info()
context = {
'response': response
}
return render(request, './moviecompare/detail.html', context)
并用它来链接html中的细节:
{% url 'compare:movie_detail' movid=key.id %}