step1 :
设置python 虚拟环境并激活,然后安装Django开发版本 这里可以本人参考博客
step2 :
新建项目 之前安装Django以及虚拟环境都是在C盘下处理的,在这里用D盘存储项目 出现小插曲:
C:\Users\用户名>d:
D:\>cd .DjangoProj
D:\.DjangoProj>django-admin startproject mysite
'django-admin' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\.DjangoProj>python
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https:
Type "help", "copyright", "credits" or "license" for more information.
>>> django-admin startproject mysite
File "<stdin>", line 1
django-admin startproject mysite
^
SyntaxError: invalid syntax
>>> import Django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Django'
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
>>>
D:\.DjangoProj>python -m django --version
D:\Anaconda\python.exe: No module named django
D:\.DjangoProj>python -m django --version
D:\Anaconda\python.exe: No module named django
但是没关系,我们可以在这里同样使用命令
python -m pip install Django -i https:
然后:
D:\.DjangoProj>python -m pip install Django -i https:
Looking in indexes: https:
Collecting Django
Using cached https:
Collecting asgiref<4,>=3.3.2
Using cached https:
Collecting sqlparse>=0.2.2
Using cached https:
Requirement already satisfied: pytz in d:\anaconda\lib\site-packages (from Django) (2020.1)
Installing collected packages: asgiref, sqlparse, Django
Successfully installed Django-3.2.9 asgiref-3.4.1 sqlparse-0.4.2
D:\.DjangoProj>django-admin startproject mysite
D:\.DjangoProj>
没有错误发生
step3:
发现创建了文件夹mysite 并且项目结构为:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
这些目录和文件的用处是:
- 最外层的 mysite/ 根目录只是你项目的容器, 根目录名称对 Django 没有影响,你可以将它重命名为任何你喜欢的名称。
- manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。你可以阅读 django-admin 和 manage.py 获取所有 manage.py 的细节。
- 里面一层的 mysite/ 目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如 mysite.urls).
- mysite/init.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。如果你是 Python 初学者,阅读官方文档中的 更多关于包的知识。
- mysite/settings.py:Django 项目的配置文件。如果你想知道这个文件是如何工作的,请查看 Django 配置 了解细节。
- mysite/urls.py:Django 项目的 URL 声明,就像你网站的“目录”。阅读 URL调度器 文档来获取更多关于 URL 的内容。
- mysite/asgi.py:作为你的项目的运行在 ASGI 兼容的 Web 服务器上的入口。阅读 如何使用 ASGI 来部署 了解更多细节。
- mysite/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。阅读 如何使用 WSGI 进行部署 了解更多细节。
然后出现了问题:
D:\.DjangoProj\mysite>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "D:\Anaconda\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\Anaconda\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Anaconda\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "D:\Anaconda\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Anaconda\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Anaconda\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\Anaconda\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "D:\Anaconda\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\Anaconda\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "D:\Anaconda\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "D:\Anaconda\lib\site-packages\django\db\models\base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "D:\Anaconda\lib\site-packages\django\db\models\base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "D:\Anaconda\lib\site-packages\django\db\models\options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "D:\Anaconda\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "D:\Anaconda\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "D:\Anaconda\lib\site-packages\django\db\utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "D:\Anaconda\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "D:\Anaconda\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "D:\Anaconda\lib\site-packages\django\db\backends\sqlite3\base.py", line 15, in <module>
from sqlite3 import dbapi2 as Database
File "D:\Anaconda\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "D:\Anaconda\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: 找不到指定的模块。
是因为缺少mysqlclient 吗? 然后:
pip install mysqlclient -i https:
依旧不行 换到C盘下并且在虚拟环境中执行该命令: 发现玄学问题: 根据教程找到: 从网站下载对应文件并解压 网站传送门 解压后移动到Andconda下对应目录,如图: 执行命令之后:
然后成功!!!!!!!
|