我是Django的新手,刚刚完成了我的第一个应用程序。现在,我要部署它。由于该应用程序不稳定,因此我在服务器上使用了轻量级Python runserver
命令部署了该应用程序。
我对数据库进行了转储,并在服务器上与此转储生成了相同的数据库。现在的问题是,当我运行应用程序时,Django ORM使用[dbname].[tablename]
,例如hubstaff.logentry_project
访问数据库。 hubstaff
是服务器上数据库的名称,logentry
是应用程序名称,logentry_project
是表名称。
数据库包含不带[database]前缀(仅logentry_project
)的表,因此会给出错误(1146, "Table 'hubstaff.LogEntry_project' doesn't exist")
这是我的DATABASE的settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join('hubstaff'),
'USER':'root',
'PASSWORD': 'haha',
'HOST':'localhost'
}
}
以下是MySQL转储:
DROP TABLE IF EXISTS `logentry_project`;
CREATE TABLE IF NOT EXISTS `logentry_project` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`startDate` date NOT NULL,
`endDate` date DEFAULT NULL,
`sourceOfProject` varchar(20) NOT NULL,
`paymentType` varchar(20) NOT NULL,
`ProjectPaymentAmount` int(10) UNSIGNED DEFAULT NULL,
`sourceName` varchar(20) DEFAULT NULL,
`is_Active` tinyint(1) NOT NULL,
`projectSeverity` int(10) UNSIGNED NOT NULL,
`hubstaffName` varchar(30),
`hoursToWork` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `LogEntry_project_name_096d434e_uniq` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=latin1;
以下是错误的堆栈跟踪:
Environment:
Request Method: GET
Request URL: http://104.248.12.78:8000/admin/projects/
Django Version: 2.1.1
Python Version: 3.5.2
Installed Applications:
['LogEntry',
'Administration',
'Accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks']
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 /root/django/templates/base.html, error at line 25
1146
15 : <!-- Jquery Data Tabels -->
16 : <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
17 : <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
18 :
19 :
20 : <!--BootStrap-->
21 :
22 : <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
23 : <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
24 : <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
25 : <lin k rel="stylesheet" hr ef="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
26 :
27 :
28 : <!-- Date Picker CSS -->
29 : <link rel="stylesheet" href={% static 'LogEntry/datepicker/bootstrap-datepicker.min.css' %}>
30 : <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
31 :
32 : <!-- Date Picker JS -->
33 : <script src={% static 'LogEntry/datepicker/bootstrap-datepicker.min.js' %} ></script>
34 :
35 :
Traceback:
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py" in execute
71. return self.cursor.execute(query, args)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in execute
250. self.errorhandler(self, exc, value)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py" in defaulterrorhandler
50. raise errorvalue
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in execute
247. res = self._query(query)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in _query
412. rowcount = self._do_query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in _do_query
375. db.query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py" in query
276. _mysql.connection.query(self, query)
The above exception ((1146, "Table 'hubstaff.LogEntry_project' doesn't exist")) was the direct cause of the following exception:
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/mixins.py" in dispatch
109. return super().dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/root/django/Administration/views.py" in get
281. return render(request,'Administration/projects.html' , {'emp_pr':emp_pr})
File "/usr/local/lib/python3.5/dist-packages/django/shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.5/dist-packages/django/template/loader.py" in render_to_string
62. return template.render(context, request)
File "/usr/local/lib/python3.5/dist-packages/django/template/backends/django.py" in render
61. return self.template.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
171. return self._render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
163. return self.nodelist.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
937. bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
904. return self.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
150. return compiled_parent._render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
163. return self.nodelist.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
937. bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
904. return self.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
937. bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
904. return self.render(context)
File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py" in render
166. len_values = len(values)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in __len__
250. self._fetch_all()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in _fetch_all
1186. self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in __iter__
54. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py" in execute_sql
1065. cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
100. return super().execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in _execute_with_wrappers
77. return executor(sql, params, many, context)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py" in execute
71. return self.cursor.execute(query, args)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in execute
250. self.errorhandler(self, exc, value)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py" in defaulterrorhandler
50. raise errorvalue
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in execute
247. res = self._query(query)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in _query
412. rowcount = self._do_query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py" in _do_query
375. db.query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py" in query
276. _mysql.connection.query(self, query)
Exception Type: ProgrammingError at /admin/projects/
Exception Value: (1146, "Table 'hubstaff.LogEntry_project' doesn't exist")
答案 0 :(得分:0)
与其转储SQL相比,不如执行migrations创建表更好。另外,您可以dumpdata从本地计算机(作为固定装置)并loaddata在生产服务器中。例如:
python manage.py migrate # will generate tables
在执行“迁移命令”之前,请确保事先具有迁移文件。您可以使用python manage.py makemigrations
创建迁移文件。另外,如果您使用版本控制,请提交这些迁移文件。
python manage.py dumpdata db.json
python manage.py loaddata db.json
Here是一个很好的教程,详细介绍了如何使用dumpdata和loaddata。