我正在使用Python和Django创建一个电影网络应用程序,它有一个搜索功能,它给我一些我不太懂的错误。
ERROR
[16/May/2016 16:22:05] ERROR [django.request:284] Internal Server Error: /movies/search/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\user\git\DjangoServer\movies\views.py", line 157, in search
set(filteredresolution)
File "C:\Users\user\git\DjangoServer\movies\views.py", line 38, in list
request.session['nextpage'] = reverse('movies:list', kwargs={'page': page})
AttributeError: 'set' object has no attribute 'session'
movies / views.py搜索功能
def search(request, keyword='', resolution='all'):
if request.POST:
request.session['nextpage'] = reverse('movies:search', kwargs={
'keyword': request.POST.get('keyword'),
'resolution': request.POST.get('resolution')})
else:
request.session['nextpage'] = reverse('movies:search', kwargs={
'keyword': keyword,
'resolution': resolution})
searchresult = []
resolutions = Resolution.objects.all().order_by('name')
if request.POST:
keyword = request.POST.get('keyword')
resolutionid = request.POST.get('resolution')
else:
keyword = keyword
resolutionid = resolution
filteredkeyword = []
filteredresolution = []
filteredall = Movie.objects.all()
if keyword != '':
filteredkeyword = Movie.objects.filter(title_en__icontains=keyword)
else:
filteredkeyword = filteredall
if resolutionid != 'all':
storedmovies = StoredMovie.objects.filter(resolution__id=int(resolutionid))
for stored in storedmovies:
filteredresolution.append(stored.movie)
else:
filteredresolution = filteredall
searchresult = list(
set(filteredkeyword) &
set(filteredresolution)
)
return render(request, 'movies/search.html', {
'movies': searchresult,
'lang': lang,
'countresult': len(searchresult) #ez mire is?
})
我很明白错误是我创建2列表的段的地方,但那里没有会话。 我从下面的列表页面中调用它:
def list(request, page=1):
request.session['nextpage'] = reverse('movies:list', kwargs={'page': page})
lang = request.session.get('language', 'failed')
if lang == 'failed' or lang not in supported_langs :
return redirect('movies:language')
movie_list = Movie.objects.all().order_by('-added')
resolutions = Resolution.objects.all().order_by('name')
paginator = Paginator(movie_list, 20) # Show 20 contacts per page
#page = request.GET.get('page')
try:
movies = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
movies = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
movies = paginator.page(paginator.num_pages)
return render(request, 'movies/list.html', {
'movies': movies,
'lang': lang,
'resolutions': resolutions,
})
答案 0 :(得分:3)
通过使用名为list
的视图,您遮蔽了内置的list
。重命名你的观点。