2017-12-04 2 views
2

Ich versuche einige Python-Dateien von Django 1.3 auf Django 1.11 zu aktualisieren. In der views.py Datei habe ich eine Methode mit der folgenden ZeileUmzug von Django 1.3 nach Django 1.11 object_list

return object_list(request, queryset=results['flips'], extra_context=context, **kwargs) 

ich, dass Object_List gelesen habe jetzt veraltet ist und statt dass ich Listview verwenden, um von django.views.generic.list und verwende Klassenansicht.

Ich habe mit dieser versucht:

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 Klasse FlipView (Listview): paginate_by = 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'), 

Aber wenn ich versuche, in die Seite zu gelangen, bin ich getti ng die folgenden Fehler:

'FlipView' object has no attribute 'kwargs' 

oder

'FlipView' object has no attribute 'status_code' 

Gesamtfehlerbeschreibung:

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' 

Antwort

1
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'), 
] 
Verwandte Themen