搭建环境 叁歲伎倆 2022-06-13 07:50 311阅读 0赞 > Django仿天猫项目 ### 一、 创建虚拟环境 ### * python版本:python3 * windows下python虚拟环境管理工具virtualenv & virtualenvwrapper mkvirtualenv django_tmall workon django_tmall ### 二、 下载并安装xadmin ### > xadmin的安装文档: [https://xadmin.readthedocs.io/en/latest/quickstart.html][https_xadmin.readthedocs.io_en_latest_quickstart.html] 有两种安装方式,这里选择**源码安装**: 下载xadmin # git clone 方式下载 git clone git@github.com:sshwsfc/django-xadmin # 解压并进入目录 # 安装需要的包 pip install -r requirements.txt requirements.txt中的包有: django~=1.9.0 django-crispy-forms~=1.6.0 django-reversion~=2.0.0 django-formtools==1.0 future==0.15.2 httplib2==0.9.2 six==1.10.0 所以项目的Django版本是1.9.13 ### 创建项目所需的mysql数据库 ### 数据库名: django_tmall 为本项目创建一个用户,用户名:djangotmall 用户密码:djangotmall321 权限:该用户拥有对django_tmall数据库的所有权限 ![这里写图片描述][SouthEast] ![这里写图片描述][SouthEast 1] ### 三、 创建Django项目 ### * 使用Pycharm创建Django项目,并指定上面步骤创建的虚拟环境 * 新建apps, extra\_apps, media, static 等文件夹,并在 apps, extra\_apps下添加**init**.py空文件 * 将xadmin源码复制到extra\_apps下 ![这里写图片描述][SouthEast 2] * 修改settings.py import sys os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 将apps和extra_apps添加Django的路径中 sys.path.insert(0, os.path.join(BASE_DIR,'apps')) sys.path.insert(0, os.path.join(BASE_DIR,'extra_apps')) # INSTALLED_APPS中添加xadmin INSTALLED_APPS = [ …… 'xadmin', 'crispy_forms', 'reversion', ] # 将修改数据库配置,改为mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': "django_tmall", 'USER': 'djangotmall', 'PASSWORD': "djangotmall321", 'HOST': "127.0.0.1" } } # 系统语言配置,改为中文 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' # 添加资源路径 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') * 安装mysql驱动 pip install pymysql 在项目settings.py同目录下的init.py中添加 import pymysql pymysql.install_as_MySQLdb() * 迁移数据库 python manage.py makemigrations python manage.py migrate * 创建超级用户 python manage.py createsuperuser * 项目创建成功,访问: [http://127.0.0.1:8000/xadmin/][http_127.0.0.1_8000_xadmin] ![这里写图片描述][SouthEast 3] ### 遇到的问题 ### 我最开始安装的Django的版本是1.10,用该版本创建的Django的settings.py中 ![这里写图片描述][SouthEast 4] 而后来pip install -r requirements.txt 安装xadmin的requirements后的Django的版本变为了1.9.13,而Django1.9~及之前的版本settings.py中应该是这样 ![这里写图片描述][SouthEast 5] 由于版本变动的问题,没有改正MIDDLEWARE\_CLASSES,所以访问[http://127.0.0.1:8000/xadmin/][http_127.0.0.1_8000_xadmin] 的时候报下面的异常: AttributeError: ‘WSGIRequest’ object has no attribute ‘user’ Internal Server Error: /xadmin/ Traceback (most recent call last): File "E:\Envs\django_tmall\lib\site-packages\django\core\handlers\base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "E:\Envs\django_tmall\lib\site-packages\django\core\handlers\base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\小旋锋的文件\大三下-课程\django仿天猫\django_tmall\extra_apps\xadmin\sites.py", line 300, in wrapper return self.admin_view(view, cacheable)(*args, **kwargs) File "E:\Envs\django_tmall\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "E:\小旋锋的文件\大三下-课程\django仿天猫\django_tmall\extra_apps\xadmin\sites.py", line 206, in inner if not self.has_permission(request) and getattr(view, 'need_site_permission', True): File "E:\小旋锋的文件\大三下-课程\django仿天猫\django_tmall\extra_apps\xadmin\sites.py", line 161, in has_permission return request.user.is_active and request.user.is_staff AttributeError: 'WSGIRequest' object has no attribute 'user' [01/Jul/2017 19:06:09] "GET /xadmin/ HTTP/1.1" 500 78215 ![这里写图片描述][SouthEast 6] 把settings.py中的MIDDLEWARE改为MIDDLEWARE\_CLASSES即可正常访问 [https_xadmin.readthedocs.io_en_latest_quickstart.html]: https://xadmin.readthedocs.io/en/latest/quickstart.html [SouthEast]: /images/20220613/d1c8e95934b74fd988cb7a6663256116.png [SouthEast 1]: /images/20220613/cffce92873264fb39fb381312172330d.png [SouthEast 2]: /images/20220613/1af351e68bbe4732a0bc2437c7654706.png [http_127.0.0.1_8000_xadmin]: http://127.0.0.1:8000/xadmin/ [SouthEast 3]: /images/20220613/1ebfe327e9a34c5dbe24982e9d0e7263.png [SouthEast 4]: /images/20220613/d7aefca4aa7d49eeb8cac764d15117f9.png [SouthEast 5]: /images/20220613/1c04a5cb20f54c90a15c34a9acfd3963.png [SouthEast 6]: /images/20220613/aad99d5937534927bcf4a2ca97a22788.png
还没有评论,来说两句吧...