gpt4 book ai didi

python - Django AttributeError 'datetime.date' 对象没有属性 'utcoffset'

转载 作者:行者123 更新时间:2023-12-02 02:26:48 40 4
gpt4 key购买 nike

我是 Django 的新手,如果问题的解释看起来很奇怪,我很抱歉。

我在 Django 项目中创建了一个博客应用程序。

models.py:

from django.db import models

class Blog(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateTimeField() # issue appears because of this statement
body = models.TextField()
image = models.ImageField(upload_to='images/')

当我在网站的管理页面中创建一个新的博客对象时,一切都很好,并且对象被创建并存储到数据库中。但是当我想编辑此博客对象(在博客列表中单击它)时,我收到此错误:

AttributeError at /admin/blog/blog/2/change/
'datetime.date' object has no attribute 'utcoffset'

完整回溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/blog/blog/2/change/

Django Version: 2.0.2
Python Version: 3.6.4
Installed Applications:
['jobs.apps.JobsConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
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']


Template error:
In template C:\Users\LENOVO\Anaconda3\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
'datetime.date' object has no attribute 'utcoffset'
9 : {% for field in line %}
10 : <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
11 : {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 : {% if field.is_checkbox %}
13 : {{ field.field }}{{ field.label_tag }}
14 : {% else %}
15 : {{ field.label_tag }}
16 : {% if field.is_readonly %}
17 : <div class="readonly">{{ field.contents }}</div>
18 : {% else %}
19 : {{ field.field }}
20 : {% endif %}
21 : {% endif %}
22 : {% if field.field.help_text %}
23 : <div class="help">{{ field.field.help_text|safe }}</div>
24 : {% endif %}
25 : </div>
26 : {% endfor %}
27 : </div>
28 : {% endfor %}
29 : </fieldset>


Traceback:

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
158. response = self.process_exception_by_middleware(e, request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
156. response = response.render()

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\response.py" in render
106. self.content = self.rendered_content

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\response.py" in rendered_content
83. content = template.render(context, self._request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
175. return self._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
155. return compiled_parent._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
155. return compiled_parent._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
67. result = block.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
67. result = block.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
211. nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
194. return template.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
177. return self._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
211. nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
211. nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
314. return nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
314. return nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
999. return render_value_in_context(output, context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_value_in_context
978. value = str(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\utils\html.py" in <lambda>
371. klass.__str__ = lambda self: mark_safe(klass_str(self))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\boundfield.py" in __str__
36. return self.as_widget()

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\boundfield.py" in as_widget
118. **kwargs

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in render
234. context = self.get_context(name, value, attrs)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\contrib\admin\widgets.py" in get_context
104. context = super().get_context(name, value, attrs)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in get_context
806. value = self.decompress(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in decompress
894. value = to_current_timezone(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\utils.py" in to_current_timezone
176. if settings.USE_TZ and value is not None and timezone.is_aware(value):

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\utils\timezone.py" in is_aware
247. return value.utcoffset() is not None

Exception Type: AttributeError at /admin/blog/blog/2/change/
Exception Value: 'datetime.date' object has no attribute 'utcoffset'

如果我将 models.DateTimeField() 更改为 models.DateField() 或者将 auto_now_add=True 添加到 DateTimeField (这样它使得该字段不可编辑)一切都会变得正常,并且博客对象变得可编辑而不会出现错误。

所以问题显然是导致此错误的原因以及如何修复它?

Django 版本 2.0.2(也在 2.0.5 上尝试过)

Python版本3.6.4

最佳答案

似乎模型实例返回了日期数据。查看数据库列,看看其类型是否不是日期。可能之前在数据库创建期间您的模型的 pub_dateDateField 。如果是这样,请运行 makemigrationsmigrate 根据您当前的模型调整数据库类型。

关于python - Django AttributeError 'datetime.date' 对象没有属性 'utcoffset',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51870088/

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