gpt4 book ai didi

python - 导入错误 : cannot import name timezone pythonanywhere

转载 作者:行者123 更新时间:2023-11-28 16:05:57 25 4
gpt4 key购买 nike

我正在尝试在 bash 控制台中使用 collectstatic 让我的 CSS 在 pythonanywhere 上的 django 应用程序上运行。

不幸的是,我遇到了一个错误:

23:49 ~/mysite/mysite $ python manage.py collectstatic                                                                                                                                                            
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 429, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in execute
self.validate()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/nikk2009/mysite/mysite/polls/models.py", line 4, in <module>
from django.utils import timezone
ImportError: cannot import name timezone
23:49 ~/mysite/mysite $

这里是导入时区的.py

import datetime

from django.db import models
from django.utils import timezone

# Create your models here.

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

def was_published_recently(self):
now = timezone.now()
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)<= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'

class Choice(models.Model):
choice_text = models.CharField(max_length= 200)
votes = models.IntegerField(default= 0)
question = models.ForeignKey(Question, on_delete=models.CASCADE)

def __str__(self):
return self.choice_text

最佳答案

如果我没记错的话,pythonanywhere 默认使用 Django 1.3.7。看起来 Django 时区支持直到 1.4 版才添加:

https://docs.djangoproject.com/en/1.10/releases/1.4/#what-s-new-in-django-1-4

您应该将 Django 更新到最新版本(或至少是较早的版本),并且一切都应该按预期工作(至少在时区方面)。您可以通过从 pythonanywhere 配置文件的 Consoles 选项卡打开 bash 控制台并运行以下命令来升级:

$ pip install --upgrade django

或者在virtualenv中安装更新的版本:

$ mkvirtualenv myenv --python=/usr/bin/python3.4

$ pip install django

编辑:

我测试了我的第一个建议,但无法让它在我的 pythonanywhere 帐户上运行(我认为这与 pythonanywhere 为其用户提供的权限有关)。但是,使用第二种方法(即使用 virtualenv)确实可以安装最新版本的 Django,它在 django.utils.timezone 中包含时区支持。

关于python - 导入错误 : cannot import name timezone pythonanywhere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com