tôi đã nhận được lỗi này khi kiểm tra ra một dự án tôi chỉ cần thiết lậpDjango hỏi tôi để cài đặt pytz và bây giờ mô hình của tôi sẽ không xác nhận Django
Exception Type: ImproperlyConfigured
Exception Value:
This query requires pytz, but it isn't installed.
Sau một chút googling tôi thấy rằng tôi đã nhận được này bởi vì trong cài đặt của tôi, tôi đã chỉ định
USE_TZ = True
mà tôi tin là phải làm cho múi giờ của dự án của tôi biết.
Vì vậy, tôi đã chạy sudo pip install pytz
để cài đặt pytz vì vậy tôi sẽ không gặp phải lỗi này. Vấn đề là bây giờ, với pytz cài đặt mô hình của tôi sẽ không xác nhận khi tôi cố gắng làm ./manage.py runserver
và tôi nhận được lỗi này
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/log.py", line 10, in <module>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/views/debug.py", line 10, in <module>
from django.http import (HttpResponse, HttpResponseServerError,
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/__init__.py", line 4, in <module>
from django.http.response import (HttpResponse, StreamingHttpResponse,
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/response.py", line 13, in <module>
from django.core.serializers.json import DjangoJSONEncoder
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/base.py", line 6, in <module>
from django.db import models
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/__init__.py", line 6, in <module>
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/query.py", line 13, in <module>
from django.db.models.fields import AutoField, Empty
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
from django.db.models.lookups import default_lookups, RegisterLookupMixin
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/lookups.py", line 6, in <module>
from django.utils import timezone
File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/timezone.py", line 149, in <module>
utc = pytz.utc if pytz else UTC()
AttributeError: 'module' object has no attribute 'utc'
gì đang xảy ra ở đây? Tại sao tôi nhận được lỗi này? Với tôi điều này có vẻ như vấn đề là đến từ pytz nhưng tôi không chắc chắn. Mọi sự trợ giúp sẽ rất được trân trọng.
Đây là mô hình của tôi, mà làm việc tốt trước khi cài đặt pytz
from django.db import models
from django.conf import settings
# Create your models here.
class Item(models.Model):
title = models.CharField(max_length=100,)
description = models.TextField()
seller = models.ForeignKey(settings.AUTH_USER_MODEL)
price = models.DecimalField(max_digits=11, decimal_places=2)
timestamp = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
Edit: Tôi thêm vào tập tin views.py tôi
from .models import Item
from django.views.generic import ListView, DetailView
class ItemListView(ListView):
model = Item
class ItemDetailView(DetailView):
model = Item
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
#This is getting a dictionary of the objects being displayed to
#the template
context = super(ItemDetailView, self).get_context_data(**kwargs)
# Add in a QuerySet of all items up for trade by the user
context['other_things_for_trade_by_user'] = Item.objects.filter(seller=context['seller'])
return context
kiểm tra 'TIME_ZONE' trong cài đặt của bạn và thử thay đổi' từ django.utils múi giờ 'trong trường của bạn 'models.DateTimeField (mặc định = timezone.now)', tại đây trong tài liệu https://docs.djangoproject.com/ en/dev/topics/i18n/timezones/# ngây thơ và nhận thức-datetime-đối tượng – madzohan
Tôi đã thử thay đổi nó thành điều đó và tôi vẫn nhận được cùng một lỗi đối tượng 'AttributeError: 'module' không có thuộc tính 'utc'' – guribe94
hiển thị truy vấn của bạn, yêu cầu pytz – madzohan