• Pycharm 搭建 Django 项目 (非常详细)


    目录

    1. 安装需求

    在使用 python 框架 Django 需要注意下面事项

    1. Pycharm 版本是专业版而不是社区版本
    2. Pycharm 配置好了 python 解释器 (一般我们现在用的都是python3)
    3. 我自己使用的是 Pycharm 版本是2020.1.2

    2. 准备工作

    2.1 新建项目

    首先我们打开 Pycharm 如下所示

    在这里插入图片描述

    可能有些小伙伴到这个界面怎么打开,你可以这样做,打开你的Pycharm编译器,然后点击左上角的 File -> Close Project

    2.2 输入相关配置

    按照下面我图中提示的做就行了,配置完成之后 点击Create

    在这里插入图片描述

    2.3 项目创建完成

    这是在安装Django 框架, 等待一下即可

    在这里插入图片描述

    安装完成如下所示

    在这里插入图片描述

    2.4 查看安装 Django 版本

    在 Pycharm 底部选择 Terminal 然后在里面输入:python -m django --version

    (pytorch_gpu) D:python-workspace utorial>python -m django --version
    3.2

    2.5 启动项目

    在 Terminal 里面输入: python manage.py runserver

    然后出现下面一个小错误

    Traceback (most recent call last):
      File "manage.py", line 22, in 
        main()
      File "manage.py", line 18, in main
        execute_from_command_line(sys.argv)
      File "D:PythonAnacondaenvspytorch_gpulibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
        utility.execute()
      File "D:PythonAnacondaenvspytorch_gpulibsite-packagesdjangocoremanagement__init__.py", line 363, in execute
        settings.INSTALLED_APPS
      File "D:PythonAnacondaenvspytorch_gpulibsite-packagesdjangoconf__init__.py", line 82, in __getattr__
        self._setup(name)
      File "D:PythonAnacondaenvspytorch_gpulibsite-packagesdjangoconf__init__.py", line 69, in _setup
        self._wrapped = Settings(settings_module)
      File "D:PythonAnacondaenvspytorch_gpulibsite-packagesdjangoconf__init__.py", line 170, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "D:PythonAnacondaenvspytorch_gpulibimportlib__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "", line 1006, in _gcd_import
      File "", line 983, in _find_and_load
      File "", line 967, in _find_and_load_unlocked
      File "", line 677, in _load_unlocked
      File "", line 728, in exec_module
      File "", line 219, in _call_with_frames_removed
      File "D:python-workspace	utorial	utorialsettings.py", line 57, in 
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
    NameError: name 'os' is not defined
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    解决方案

    找到 seetings。py 文件,然后在settings.py文件头加上:import os

    在这里插入图片描述

    再次执行: python manage.py runserver

    在这里插入图片描述
    在这里插入图片描述
    出现上面这个界面基本上Django项目就搭建好了。

    2.6 解决一点小问题

    在启动 Django 项目的时候我发现控制台出现了下面一点小提示

    You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    
    • 1
    • 2

    这个问题的我也不是了解是啥原因直接给大家上解决方案。

    解决方法: 先 Ctrl + C 结束服务,执行这个命令 python manage.py migrate

    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying admin.0003_logentry_add_action_flag_choices... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying auth.0010_alter_group_name_max_length... OK
      Applying auth.0011_update_proxy_permissions... OK
      Applying auth.0012_alter_user_first_name_max_length... OK
      Applying sessions.0001_initial... OK
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    这样就大功告成了。

    3. 一点小补充

    可能有些人使用的社区版本,打开Pycharm 没有和我一样的界面显示,这里我推荐你们看下下面这个视频的教程,讲的还是挺详细的。

    如何在pycharm community 版中搭建 django+mysql 开发环境

    4. 参考文献

    1. 使用pycharm创建自己的第一个django项目

    2. Django运行服务报NameError: name ‘os‘ is not defined

    3. You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for …报错

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Pinia 与 Vuex 使用区别
    python 日志处理(基础篇)
    给图片添加图片水印
    kubeadm部署v1.26.1
    使用移动平均数调整曲线 by Python
    竞赛 题目:基于python的验证码识别 - 机器视觉 验证码识别
    nginx 开机自启(脚本+命令)
    SpringBoot框架下——设置字符编码——2种实现方式
    jdk和cglib动态代理
    GitHub最新发布,阿里十年架构师手写版spring全家桶笔记全新开源
  • 原文地址:https://blog.csdn.net/m0_67402026/article/details/126113879