簡體   English   中英

Django:無法將datetime.datetime與datetime.date進行比較

[英]Django: can't compare datetime.datetime to datetime.date

我正在做Django 1.8教程來制作Polls應用程序:

https://docs.djangoproject.com/en/1.8/intro/tutorial01/

我添加了一個名為was_published today()的布爾函數,如果最近發布了一個帖子,它將返回True或False。 該函數在這里在Question類下定義

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

    def was_published_recently(self):
        return self.pub_date >= timezone.now() -  datetime.timedelta(days=1)

    def __str__(self):
    return self.question_text

另外,我有一個管理員設置,其中顯示了問題,如下所示:from django.contrib import admin

from .models import Choice,Question

class ChoiceInLine(admin.TabularInline):
    model = Choice
    extra = 3


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,                 {'fields':['question_text']}),
        ('Date information', {'fields':['pub_date'],'classes':['collapse']}),
    ]
    inlines = [ChoiceInLine]
    list_display = ('question_text','pub_date','was_published_recently')


admin.site.register(Question, QuestionAdmin)

但是,當我訪問管理員的“問題”部分時,出現錯誤消息:

TypeError at /admin/polls/question/
can't compare datetime.datetime to datetime.date
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/polls/question/
Django Version: 1.8.3
Exception Type: TypeError
Exception Value:    
can't compare datetime.datetime to datetime.date
Exception Location: C:\Users\Owner\Desktop\venv\forumtest\polls\models.py  in was_published_recently, line 12
Python Executable:  C:\Users\Owner\Desktop\venv\Scripts\python.EXE
Python Version: 3.4.2
Python Path:    
['C:\\Users\\Owner\\Desktop\\venv\\forumtest',
 'C:\\Windows\\system32\\python34.zip',
 'C:\\Users\\Owner\\Desktop\\venv\\DLLs',
 'C:\\Users\\Owner\\Desktop\\venv\\lib',
 'C:\\Users\\Owner\\Desktop\\venv\\Scripts',
 'C:\\Python34\\Lib',
 'C:\\Python34\\DLLs',
 'C:\\Users\\Owner\\Desktop\\venv',
 'C:\\Users\\Owner\\Desktop\\venv\\lib\\site-packages']
Server time:    Wed, 22 Jul 2015 16:30:07 -0700

Error during template rendering

In template C:\Users\Owner\Desktop\venv\lib\site-packages\django\contrib\admin\templates\admin\change_list.html, error at line 91
can't compare datetime.datetime to datetime.date


81          {% endif %}
82        {% endblock %}
83  
84        <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %} novalidate>{% csrf_token %}
85        {% if cl.formset %}
86          <div>{{ cl.formset.management_form }}</div>
87        {% endif %}
88  
89        {% block result_list %}
90            {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
91  

  {% result_list cl %}



92            {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
93        {% endblock %}
94        {% block pagination %}{% pagination cl %}{% endblock %}
95        </form>
96      </div>
97    </div>
98  {% endblock %}
99

我使用的是django 1.8,我使用1.7進行了相同的教程,並且沒有相同的問題。 我不確定這個問題是否是錯誤。

請讓我知道如何解決此問題。 謝謝。

嘗試這個:

pub_date = models.DateTimeField('date published')

在教程pub_date中定義為DateTimeField,用於比較日期時間值。

用這個:

    def was_published_recently(self):
        now = timezone.now().date()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM