我正在嘗試設定 django-tables2 并遵循他們檔案中的建議。但是,如果我呼叫視圖,它會引發錯誤
File "/home/ubuntu/mysite/mysite_venv/lib/python3.8/site-packages/asgiref/local.py", line 94, in __del__
NameError: name 'TypeError' is not defined
注意:據我所知,不幸的是,此錯誤訊息也發生在其他背景關系中,因為最受好評的執行緒處理Apache 配置問題。一旦簡明扼要地掌握了這個問題,我很樂意為清晰起見調整標題。
Django-tables2 應該訪問(只讀)我在默認 SQLite 資料庫后面添加的舊版 MySQL 資料庫settings.py(請參閱下文)。
我的polls/models.py:
from django.db import models
import datetime
from django.db import models
from django.utils import timezone
...
class Dashboard(models.Model):
host = models.CharField(db_column='Host', max_length=255, blank=True, null=True)
title = models.TextField(db_column='Title', blank=True, null=True)
operation = models.CharField(db_column='Operation', max_length=255, blank=True, null=True)
performedat = models.DateTimeField(db_column='PerformedAt', blank=True, null=True)
class Meta:
managed = False
db_table = 'dashboard'
如果有必要,MySQL 中的基礎表如下所示:
c.execute('''CREATE TABLE dashboard(
id INT PRIMARY KEY AUTO_INCREMENT,
Host VARCHAR(255),
Title TEXT,
Operation VARCHAR(255),
PerformedAt DATETIME);''')
我的polls/views.py:
from django.shortcuts import render
from django.views.generic.list import ListView
from .models import Dashboard
from django.http import HttpResponse
class DashboardListView(ListView):
model = Dashboard
template_name = 'polls/dashboard.html'
注意:我已確保dashboard.html 確實駐留在mysite/polls/templates/polls 中。
我的polls/urls.py:
from django.urls import path
from polls.views import DashboardListView, detail, accept, reject
urlpatterns = [
# ex: /polls/
path('', DashboardListView.as_view()),
#ex: /polls/5/
path('<int:question_id>/', detail, name='detail'),
#ex: /polls/5/accept/
path('<int:question_id>/accept/', accept, name='results'),
# ex: /polls/5/deny/
path('<int:question_id>/deny/', reject, name='reject'),
]
我的polls/dbrouters.py:
from polls.models import Dashboard
class DashboardDBRouter:
def db_for_read (self, model, **hints):
if (model == Dashboard):
# your model name as in settings.py/DATABASES
return 'dashbaord'
return None
我的dashboard.html(剛剛使用教程模板):
{# polls/templates/polls/dashboard.html #}
{% load render_table from django_tables2 %}
<!doctype html>
<html>
<head>
<title>List of persons</title>
</head>
<body>
{% render_table object_list %}
</body>
</html>
我的settings.py:
# Application definition
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tables2',
]
DATABASE_ROUTERS = (
'polls.dbrouters.DashboardDBRouter',
)
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'dashboard': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdatabase',
'USER': 'my_username',
'PASSWORD': 'my_password',
'HOST': 'my_host',
'PORT': '3306'
},
}
我注意到的是,如果我打開一個 shell并通過我python manage.py shell呼叫經理Dashboard.objects.all()
django.utils.connection.ConnectionDoesNotExist: The connection 'dashbaord' doesn't exist.
雖然如果我打電話給Dashboard.objects.all().using(‘dashboard’)它似乎作業,因為我得到
<QuerySet [<Dashboard: Dashboard object (1)>]>
我的路由器可能有問題嗎?我認為如果我正確理解了這個答案,它應該會自動連接。
我的第二個猜測:我對 Django 和基于類的視圖還沒有太多經驗,所以這可能完全不適用,但我很困惑,我的模型和視圖都不包含任何包含任何return或str()陳述句的陳述句,盡管我發現了相反的例子在這里和這里。
我認為這可能是問題所在,因為它可以解釋錯誤,因為 Python 解釋器在行程完成之前被破壞,因為沒有回傳任何內容,正如這里討論的那樣(不幸的是沒有明確的結果)。
老實說,我不知道從哪里開始,非常感謝任何幫助。
uj5u.com熱心網友回復:
routers.py中dashbaord(should be )的拼寫有誤:dashboard
django.utils.connection.ConnectionDoesNotExist:
The connection 'dashbaord' doesn't exist.
^^^^^^^^^^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498005.html
