- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将一些 python 文件从 Django 1.3 更新到 Django 1.11。在 views.py 文件中,我有一个包含以下行的方法
return object_list(request, queryset=results['flips'], extra_context=context, **kwargs)
我读到 object_list 现在已被弃用,我必须使用 django.views.generic.list
中的 ListView 并使用类 View 来代替它。
我试过这个:
Django 1.3
views.py
def flow(request, **kwargs):
agent = 'default'
lang = request.LANGUAGE_CODE.lower()
cache_key = '%s%s%s' % (request.build_absolute_uri(), request.user.id, lang)
results = cache.get(cache_key)
if results is None:
results = {}
flips = pushitem.visibles.visibles().by_lang(lang=lang)
sort = request.GET.get('sort', '')
if sort == 'popular':
flips = flips.order_by('-votes_total')
else:
flips = flips.order_by('-timestamp')
scope = request.GET.get('scope', '')
if scope:
today = datetime.now()
if scope == 'day':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month, timestamp__day=today.day)
if scope == 'month':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month)
if scope == 'year':
flips = flips.filter(timestamp__year=today.year)
#Todo use UserProfile instead of User for people and add a searchfield to UserProfile (faster)
query = request.GET.get('q', '')
if query:
people = User.objects.filter(Q(username__contains=query))
people |= User.objects.filter(Q(first_name__contains=query))
people |= User.objects.filter(Q(last_name__contains=query))
flips = flips.filter(Q(searchfield__contains=query))
topics = Tag.objects.filter(Q(name__contains=unidecode(query.lower())))
results['people'] = people
results['topics'] = topics
results['flips'] = flips
cache.set(cache_key, results)
context = {
'title': _('Flipter.com, Ask everything!'),
'description': _('The world\'s largest community of opinions, made with awesome polls.')
}
if results.has_key('people'):
context['people'] = results['people']
if results.has_key('topics'):
context['topics'] = results['topics']
return object_list(request, queryset=results['flips'], extra_context=context, **kwargs)
urls.py
url(r'^$', flow, dict(template_name='flow.html', paginate_by=12,), name='flow'),
Django 1.11
views.py 翻转 View 类( ListView ): 分页依据 = 12 template_name = 'flow.html'
def flow(request, **kwargs):
agent = 'default'
lang = LANGUAGE_CODE.lower()
cache_key = '%s%s%s' % (request.build_absolute_uri(), request.user.id, lang)
results = cache.get(cache_key)
if results is None:
results = {}
flips = pushitem.visibles.visibles()
sort = request.GET.get('sort', '')
if sort == 'popular':
flips = flips.order_by('-votes_total')
else:
flips = flips.order_by('-timestamp')
scope = request.GET.get('scope', '')
if scope:
today = datetime.now()
if scope == 'day':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month, timestamp__day=today.day)
if scope == 'month':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month)
if scope == 'year':
flips = flips.filter(timestamp__year=today.year)
#Todo use UserProfile instead of User for people and add a searchfield to UserProfile (faster)
query = request.GET.get('q', '')
if query:
people = User.objects.filter(Q(username__contains=query))
people |= User.objects.filter(Q(first_name__contains=query))
people |= User.objects.filter(Q(last_name__contains=query))
flips = flips.filter(Q(searchfield__contains=query))
topics = Tag.objects.filter(Q(name__contains=unidecode(query.lower())))
results['people'] = people
results['topics'] = topics
results['flips'] = flips
cache.set(cache_key, results)
context = {
'title': _('Flipter.com, Ask everything!'),
'description': _('The world\'s largest community of opinions, made with awesome polls.')
}
if 'people' in results:
context['people'] = results['people']
if 'topics' in results:
context['topics'] = results['topics']
return FlipView(queryset=results['flips'], extra_context=context)
urls.py
url(r'^$', views.flow, name='flow'),
但是当我尝试进入该页面时出现以下错误:
'FlipView' object has no attribute 'kwargs'
或
'FlipView' object has no attribute 'status_code'
完整的错误描述:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.11.3
Python Version: 3.6.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'django_countries',
'django_extensions',
'django_comments',
'sparkty_app',
'widget_tweaks',
'compressor',
'tagging',
'easy_thumbnails',
'social_django',
'registration']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware']
Traceback:
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\utils\deprecation.py" in __call__
142. response = self.process_response(request, response)
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\middleware\clickjacking.py" in process_response
32. if response.get('X-Frame-Options') is not None:
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\views\generic\list.py" in get
175. context = self.get_context_data()
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\views\generic\list.py" in get_context_data
135. paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
File "C:\Users\darth\AppData\Local\conda\conda\envs\SparktyEnv\lib\site-packages\django\views\generic\list.py" in paginate_queryset
70. page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
Exception Type: AttributeError at /
Exception Value: 'FlipView' object has no attribute 'kwargs'
最佳答案
class FlowListView(ListView):
model = Flow # Unsure of this one, probably don't even need it.
def get_queryset(self):
results = cache.get(cache_key)
if results is None:
results = {}
flips = pushitem.visibles.visibles()
sort = request.GET.get('sort', '')
if sort == 'popular':
flips = flips.order_by('-votes_total')
else:
flips = flips.order_by('-timestamp')
scope = request.GET.get('scope', '')
if scope:
today = datetime.now()
if scope == 'day':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month, timestamp__day=today.day)
if scope == 'month':
flips = flips.filter(timestamp__year=today.year, timestamp__month=today.month)
if scope == 'year':
flips = flips.filter(timestamp__year=today.year)
# Todo use UserProfile instead of User for people and add a searchfield to UserProfile (faster)
query = request.GET.get('q', '')
if query:
people = User.objects.filter(Q(username__contains=query))
people |= User.objects.filter(Q(first_name__contains=query))
people |= User.objects.filter(Q(last_name__contains=query))
flips = flips.filter(Q(searchfield__contains=query))
topics = Tag.objects.filter(Q(name__contains=unidecode(query.lower())))
results['people'] = people
results['topics'] = topics
results['flips'] = flips
cache.set(cache_key, results)
return results
def get_context_data(self, **kwargs):
context = super(FlowListView, self).get_context_data(**kwargs)
context['title'] = _('Flipter.com, Ask everything!')
context['description'] = _('The world\'s largest community of opinions, made with awesome polls.')
# This is something you'll have to do a bit differently, unsure if it will work
results = kwargs['object_list']
if 'people' in results:
context['people'] = results['people']
if 'topics' in results:
context['topics'] = results['topics']
return context
urls.py
urlpatterns = [
url(r'^$', FlowListView.as_view(), name='flow-list'),
]
关于python - 从 Django 1.3 移动到 Django 1.11 object_list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639833/
我在 Django 模板中遇到了一个奇怪的问题。它是一个显示文章列表的模板,我为每个人显示了一个我称之为关键概念的关键字列表。 奇怪的是,它显示的不是关键概念列表,而是使用该关键概念的文章列表。 下面
在我的 ProductList上课,当我尝试调用 get_context_data在另一种方法中,我得到一个错误 'ProductList' object has no attribute 'obje
我想使用 object_list generic view 。但我不知道它除了分页之外还给我带来什么好处?看来我还需要自己写模板? 这比写我自己的观点快多少?我错过了什么吗? 如果它值得使用并且我确实
我正在尝试编写一个测试,测试在基于类的 View ListView 传递的上下文中是否存在 object_list。 目前我有代码: View .py class BlogView(ListV
所以基本上我想知道如何使用 for 循环在 django 模板中索引图像。这个想法是使用您的用户添加到模型中的前三张图像创建幻灯片放映 模型.py class Product(models.Mode
将 Django 从 1.4 更新到 1.6 后,我遇到了 global name 'object_list' is not defined 问题 我有 views.py: def news(requ
我正在使用 Haystack 和 Solr 后端向现有 Django 站点添加搜索。我希望我的搜索能够跨多个不同的模型运行,并返回一组结果。 在迭代结果时,我想根据每个结果的模型类型来格式化每个结果
我正在尝试将一些 python 文件从 Django 1.3 更新到 Django 1.11。在 views.py 文件中,我有一个包含以下行的方法 return object_list(reques
我正在尝试在 ListView 上测试我的自定义 get_context_data() 方法,但我一直遇到此错误:AttributeError: 'Home' object has no attrib
我是一名优秀的程序员,十分优秀!