0
我正在用Python和Django創建一個電影網絡應用程序,它有一個搜索功能,它給了我一些我不太瞭解的錯誤。'set'object has no attribute'session'
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'
電影/ 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,
})
當你做'searchresult = list(set(...)&...)'你的意思是使用內建列表函數還是你自己的? –
我打算使用內置的一個,我的壞 – MorRich