Django入门第一个坑
程序员文章站
2022-06-18 18:01:47
首先通过pycharm创建一个Django项目File ==> New ProJect ==> Django先看看目录结构,测试一下看看项目能否正常运行HelloWorld首先,在ulrs.py 配置路由的路径#导入app views 模块from app import viewsurlpatterns = [ path('admin/', admin.site.urls), path('index/', views.index)]其次,在app中的vi...
首先通过pycharm创建一个Django项目
File ==> New ProJect ==> Django
先看看目录结构,测试一下看看项目能否正常运行
HelloWorld
首先,在ulrs.py 配置路由的路径
#导入app views 模块
from app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index)
]
其次,在app中的views.py中编写逻辑代码,要跳转的路径
from django.shortcuts import render, HttpResponse, redirect
# Create your views here.
def index(request):
return render(request, 'index.html')
最后在,templates目录下创建index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
运行项目
直接访问http://127.0.0.1:8000
在请求的URL中 加入index / http://127.0.0.1:8000/index
成功访问!
这个是我学习Django的一个入门案例,但是这次总结的重点不在这,我们接者往下走!!!
数据迁移的坑
先拿出问题一
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. django配置mysql数据库
F:\workSpace\Python\Book>python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\mysql\base.py", line 15, in <module>
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Administrator\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Administrator\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Administrator\venv\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Administrator\venv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\Administrator\venv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Administrator\venv\lib\site-packages\django\db\models\base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\Administrator\venv\lib\site-packages\django\db\models\base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\models\options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\Administrator\venv\lib\site-packages\django\db\__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\Administrator\venv\lib\site-packages\django\db\utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\mysql\base.py", line 20, in <module>
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
解决方法
将原来的settings.py中的
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
修改为
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bookManager', //数据库名称
'HOST':'127.0.0.1', //本机
'POST':3306, //端口
'USER':'root', //用户名
'PASSWORD':'root', //密码
}
}
#导入 pymysql模块
import pymysql
pymysql.install_as_MySQLdb()
本以为问题都解决结果又来一波
F:\workSpace\Python\Book>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Administrator\venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle
loader.check_consistent_history(connection)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\migrations\loader.py", line 283, in check_consistent_history
applied = recorder.applied_migrations()
File "C:\Users\Administrator\venv\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\Administrator\venv\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
return self._cursor()
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
self.ensure_connection()
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
self.init_connection_state()
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\mysql\base.py", line 231, in init_connection_state
if self.features.is_sql_auto_is_null_enabled:
File "C:\Users\Administrator\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\mysql\features.py", line 82, in is_sql_auto_is_null_enabled
cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\utils.py", line 103, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "C:\Users\Administrator\venv\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
我原地裂开!!
仔细查看最后一行
AttributeError: ‘str’ object has no attribute 'decode’
这不是没设置编码嘛
我进入到 里面
运行成功
偶偶偶,我又好了!!!!又是开心快乐的一天
本文地址:https://blog.csdn.net/weixin_45470171/article/details/110238259
上一篇: ppt打不开的常见情况及解决方法整理