'NoneType' 对象没有属性 'META'
'NoneType' object has no attribute 'META'
以下是我的 Django 1.10 views.py 代码,它生成我的博客 RSS 提要。我现在正在尝试将我的使用情况统计跟踪添加到此。
我将所有统计代码保存在项目的指定区域中。我只需要 rss views.py 中的这 3 行代码。这在我的另一个 views.py 中有效,只是在我目前很烂的 rss 中无效。
from statistics.service.add import ServiceAdd as ServiceStatisticsAdd
rss_reference = {'utc': timezone.now()}
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
我收到的错误都是关于 request。最初数据库 def 是 def items(self):。当我去添加跟踪时,我预计包括 request 会修复错误。
这是我的views.py
from datetime import datetime
from django.contrib.syndication.views import Feed
from django.urls import reverse
from django.utils import timezone
from database.ron_home.models import BlogWebsite
from statistics.service.add import ServiceAdd as ServiceStatisticsAdd
class BlogRssFeed(Feed):
title = "Rons-Home.net Blog RSS Feed"
link = "/en/blog/"
description = "Ron Piggott shares updates from his health care and logistics of daily living with a physical disability."
def item_title(self, obj):
return obj.entry_title
def item_description(self, obj):
return obj.entry
def item_link(self, obj):
return reverse('blog:entry', args=[obj.reference])
def items(self, request, **kwargs):
rss_reference = {'utc': timezone.now()}
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
return BlogWebsite.objects.filter(entry_date__lt=datetime.utcnow()).order_by('-entry_date')[:15]
这是完整的回溯
[09/Apr/2017 14:02:48] "GET / HTTP/1.1" 302 0
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField BlogWebsite.entry_date received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField NewsletterEditions.issue_date received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField VideoProfile.date_created received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
[09/Apr/2017 14:02:48] "GET /en/ HTTP/1.1" 200 20414
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField BlogWebsite.entry_date received a naive datetime (2017-04-09 14:02:51.335566) while time zone support is active.
RuntimeWarning)
[09/Apr/2017 14:02:51] "GET /en/blog/ HTTP/1.1" 200 21780
Internal Server Error: /en/rss/blog
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 41, in __call__
feedgen = self.get_feed(obj, request)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 169, in get_feed
for item in self._get_dynamic_attr('items', obj):
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 92, in _get_dynamic_attr
return attr(obj)
File "/home/rpiggott/PyCharmProjects/rons-home.net/rss/views.py", line 27, in items
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/home/rpiggott/PyCharmProjects/rons-home.net/statistics/service/add.py", line 25, in add
ip_address = get_real_ip(self.request)
File "/usr/local/lib/python3.5/dist-packages/ipware/ip.py", line 36, in get_real_ip
return get_ip(request, real_ip_only=True, right_most_proxy=right_most_proxy)
File "/usr/local/lib/python3.5/dist-packages/ipware/ip.py", line 14, in get_ip
value = request.META.get(key, request.META.get(key.replace('_', '-'), '')).strip()
AttributeError: 'NoneType' object has no attribute 'META'
[09/Apr/2017 14:02:54] "GET /en/rss/blog HTTP/1.1" 500 114831
我觉得有点棘手。将此方法添加到 BlogRssFeed
def get_feed(self, obj, request):
self.custom_var = request
return super().get_feed(obj, request)
然后
ServiceStatisticsAdd(self.custom_var).add('rss', rss_reference, kwargs)
以下是我的 Django 1.10 views.py 代码,它生成我的博客 RSS 提要。我现在正在尝试将我的使用情况统计跟踪添加到此。
我将所有统计代码保存在项目的指定区域中。我只需要 rss views.py 中的这 3 行代码。这在我的另一个 views.py 中有效,只是在我目前很烂的 rss 中无效。
from statistics.service.add import ServiceAdd as ServiceStatisticsAdd
rss_reference = {'utc': timezone.now()}
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
我收到的错误都是关于 request。最初数据库 def 是 def items(self):。当我去添加跟踪时,我预计包括 request 会修复错误。
这是我的views.py
from datetime import datetime
from django.contrib.syndication.views import Feed
from django.urls import reverse
from django.utils import timezone
from database.ron_home.models import BlogWebsite
from statistics.service.add import ServiceAdd as ServiceStatisticsAdd
class BlogRssFeed(Feed):
title = "Rons-Home.net Blog RSS Feed"
link = "/en/blog/"
description = "Ron Piggott shares updates from his health care and logistics of daily living with a physical disability."
def item_title(self, obj):
return obj.entry_title
def item_description(self, obj):
return obj.entry
def item_link(self, obj):
return reverse('blog:entry', args=[obj.reference])
def items(self, request, **kwargs):
rss_reference = {'utc': timezone.now()}
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
return BlogWebsite.objects.filter(entry_date__lt=datetime.utcnow()).order_by('-entry_date')[:15]
这是完整的回溯
[09/Apr/2017 14:02:48] "GET / HTTP/1.1" 302 0
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField BlogWebsite.entry_date received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField NewsletterEditions.issue_date received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField VideoProfile.date_created received a naive datetime (2017-04-09 14:02:48) while time zone support is active.
RuntimeWarning)
[09/Apr/2017 14:02:48] "GET /en/ HTTP/1.1" 200 20414
/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1430: RuntimeWarning: DateTimeField BlogWebsite.entry_date received a naive datetime (2017-04-09 14:02:51.335566) while time zone support is active.
RuntimeWarning)
[09/Apr/2017 14:02:51] "GET /en/blog/ HTTP/1.1" 200 21780
Internal Server Error: /en/rss/blog
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 41, in __call__
feedgen = self.get_feed(obj, request)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 169, in get_feed
for item in self._get_dynamic_attr('items', obj):
File "/usr/local/lib/python3.5/dist-packages/django/contrib/syndication/views.py", line 92, in _get_dynamic_attr
return attr(obj)
File "/home/rpiggott/PyCharmProjects/rons-home.net/rss/views.py", line 27, in items
ServiceStatisticsAdd(request).add('rss', rss_reference, kwargs)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/home/rpiggott/PyCharmProjects/rons-home.net/statistics/service/add.py", line 25, in add
ip_address = get_real_ip(self.request)
File "/usr/local/lib/python3.5/dist-packages/ipware/ip.py", line 36, in get_real_ip
return get_ip(request, real_ip_only=True, right_most_proxy=right_most_proxy)
File "/usr/local/lib/python3.5/dist-packages/ipware/ip.py", line 14, in get_ip
value = request.META.get(key, request.META.get(key.replace('_', '-'), '')).strip()
AttributeError: 'NoneType' object has no attribute 'META'
[09/Apr/2017 14:02:54] "GET /en/rss/blog HTTP/1.1" 500 114831
我觉得有点棘手。将此方法添加到 BlogRssFeed
def get_feed(self, obj, request):
self.custom_var = request
return super().get_feed(obj, request)
然后
ServiceStatisticsAdd(self.custom_var).add('rss', rss_reference, kwargs)