将html文件命名为Message_board.html,快捷键ctrl+c复制,在templates目录下粘贴
在static目录下新建一个css目录,在创建一个style.css的文件。
在Message_board.html中,将style全部复制到style.css中,将头部尾部的style删除。
回到Message_board.html,将style删除,新建一个link,
配置数据库:
改成下面这样。
数据库配置好后我们来测试下是否能连接数据库,点击Tools,选择Run Manage.py Task
然后会出现No module named 'MySQLdb’错误:
Failed to get real commands on module "djangostart": python process died with code 1: Traceback (most recent call last):
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\backends\mysql\base.py", line 26, in
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm 2018.3.2\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in
django.setup()
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\stjas\Envs\testvir2\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\contrib\auth\models.py", line 4, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\contrib\auth\base_user.py", line 49, in
class AbstractBaseUser(models.Model):
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\models\base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\models\base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\models\options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\stjas\Envs\testvir2\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\stjas\Envs\testvir2\lib\site-packages\django\db\backends\mysql\base.py", line 29, in
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
python3.6使用django出现错误No module named ‘MySQLdb’解决办法:
安装mysql驱动: pip install MySQL-python
python3.6版本无法安装!只能安装2.7版本,解决方案看下面。
(testvir2) D:\PythonCode\PycharmProjects\djangostart>pip install MySQL-python
Collecting MySQL-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
Building wheel for MySQL-python (setup.py) ... error
Complete output from command c:\users\stjas\envs\testvir2\scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\stjas\\AppData\\Local\\Temp\\pip-install-hzakkym1\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d C:\Users\stjas\AppData\Local\Temp\pip-wheel-5cu6d7aj --python-tag cp36:
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-3.6
copying _mysql_exceptions.py -> build\lib.win-amd64-3.6
creating build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-3.6\MySQLdb
creating build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.6\MySQLdb\constants
running build_ext
building '_mysql' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
----------------------------------------
Failed building wheel for MySQL-python
Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
Running setup.py install for MySQL-python ... error
Complete output from command c:\users\stjas\envs\testvir2\scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\stjas\\AppData\\Local\\Temp\\pip-install-hzakkym1\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\stjas\AppData\Local\Temp\pip-record-ih19e2eb\install-record.txt --single-version-externally-managed --compile --install-headers c:\users\stjas\envs\testvir2\include\site\python3.6\MySQL-python:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.6
copying _mysql_exceptions.py -> build\lib.win-amd64-3.6
creating build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-3.6\MySQLdb
creating build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.6\MySQLdb\constants
running build_ext
building '_mysql' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
----------------------------------------
Command "c:\users\stjas\envs\testvir2\scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\stjas\\AppData\\Local\\Temp\\pip-install-hzakkym1\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\stjas\AppData\Local\Temp\pip-record-ih19e2eb\install-record.txt --single-version-externally-managed --compile --install-headers c:\users\stjas\envs\testvir2\include\site\python3.6\MySQL-python" failed with error code 1 in C:\Users\stjas\AppData\Local\Temp\pip-install-hzakkym1\MySQL-python\
python3.6的版本使用新的驱动:pip install mysqlclient
(testvir2) C:\Users\stjas\Downloads\Compressed>pip install mysqlclient
Collecting mysqlclient
Downloading https://files.pythonhosted.org/packages/c1/15/83f1444c0bbeeff9e14b0ee2cfbc985949404e325938a1b1c20adcd462ac/mysqlclient-1.4.2.post1-cp36-cp36m-win_amd64.whl (178kB)
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.2.post1
安装完mysqlclient库以后,在task一下:
发现可以连接数据库了!
然后通过makemigrations命令生成数据表:
然后在migrate。
然后,为URL创建映射,点击views.py,添加红框里面代码
点击urls.py,添加红框中的代码。
在回到settings.py,将TEMPLATES的DIRS改为红框中代码。
在settings.py最下方,添加STATICFILES_DIRS路径。
最后run,django
运行成功!!!
浏览器输入http://127.0.0.1:8000/form/
就可以看到我们创建的留言箱了!