Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

django - Not ImplementedError: subclasses of BaseDatabaseOperations may require a date_trunc_sql() method

I was using Django to build a simple blog site, and tried to use context processors for the Monthly blog archive (such as 2020-12).

in models.py
class Article(models.Model):
  time_publish = models.DateTimeField()

in context_processors.py
def Side_Menu_Context(request):
    return {
        'QS_YM_BlogCount':Article.objects.datetimes('time_publish','month',order='DESC') 
            .values('time_publish')
            .annotate(num_of_article=Count('id')),
    }

in setting.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"static")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'IoTSite.context_processors.Side_Menu_Context',
            ],
        },
    },
]

in Base_Layout.html which is the base template inherited by all other templates.
<aside>
        <div>Archive</div>
            <ul>
                {% for item in QS_YM_BlogCount %}
                    <li><a href="#">{{item.time_publish.year}}-{{item.time_publish.month}} ({{ item.num_of_article }})</a></li>
                {% endfor %}
            </ul>
    </aside>

When I run the server and try to access homepage, encountered below error:

NotImplementedError at /
subclasses of BaseDatabaseOperations may require a datetime_trunc_sql() method
Request Method: GET
Request URL:    http://info.yanatm.fun/
Django Version: 2.2.17
Exception Type: NotImplementedError
Exception Value:    
subclasses of BaseDatabaseOperations may require a datetime_trunc_sql() method
Exception Location: C:UsersyantianAnaconda3libsite-packagesdjangodbackendsaseoperations.py in datetime_trunc_sql, line 141
Python Executable:  C:UsersyantianAnaconda3python.exe
Python Version: 3.7.3
Python Path:    
['Z:\Documents\IoT\ec2-user\IoTSite',
 'C:\Users\yantian\Anaconda3\python37.zip',
 'C:\Users\yantian\Anaconda3\DLLs',
 'C:\Users\yantian\Anaconda3\lib',
 'C:\Users\yantian\Anaconda3',
 'C:\Users\yantian\Anaconda3\lib\site-packages',
 'C:\Users\yantian\Anaconda3\lib\site-packages\win32',
 'C:\Users\yantian\Anaconda3\lib\site-packages\win32\lib',
 'C:\Users\yantian\Anaconda3\lib\site-packages\Pythonwin']

Error during template rendering
In template Z:DocumentsIoTec2-userIoTSitestaticBase_Layout.html, error at line 37

subclasses of BaseDatabaseOperations may require a datetime_trunc_sql() method
27      </nav>
28  
29      <main>
30          {% block main %}
31          {% endblock %}
32      </main>
33  
34      <aside>
35          <div>Archive</div>
36              <ul>
37                  {% for item in QS_YM_BlogCount %}
38                      <li><a href="#">{{item.time_publish.year}}-{{item.time_publish.month}} ({{ item.num_of_article }})</a></li>
39                  {% endfor %}
40              </ul>
41      </aside>

I have been trying to fix this for nearly 3 days, no luck so far, and it is driving me crazy.. thanks in advance for any help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...