Django - 项目结构

Django - 项目结构 首页 / Django入门教程 / Django - 项目结构

现在,您已经安装了Django。现在是时候开始一个新的项目和应用程序了。

Django启动项目

在任何版本的django中,您都可以使用以下命令启动新项目:

django-admin startproject project_name

项目目录最初将如下所示:

    project_name/
        manage.py
        project_name/
            __init__.py
            settings.py
            urls.py
            asgi.py
            wsgi.py
                                

之后,您可以移动到项目目录。 具有项目名称的外部文件夹仅仅是容纳您的项目, manage.py 和数据库的集合。

cd project_name

Django的manage.py

manage.py 通过允许您运行与 django-admin 相同的命令行参数来帮助您管理项目。

    Available subcommands:

    [auth]
        changepassword
        createsuperuser
    
    [contenttypes]
        remove_stale_contenttypes
    
    [django]
        check
        compilemessages
        createcachetable
        dbshell
        diffsettings
        dumpdata
        flush
        inspectdb
        loaddata
        makemessages
        makemigrations
        migrate
        sendtestemail
        shell
        showmigrations
        sqlflush
        sqlmigrate
        sqlsequencereset
        squashmigrations
        startapp
        startproject
        test
        testserver
    
    [sessions]
        clearsessions
    
    [staticfiles]
        collectstatic
        findstatic
        runserver
                                    

Django迁移

迁移(migrate) 用于应用迁移或发布更改。通常在 makemigrations 之后。 这里使用 migrate 的目的是为内置应用(例如 admin,auth,contentypes和session )应用迁移。

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 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 sessions.0001_initial... OK
                                

Django运行服务器

现在无涯教程的django骨架项目已经准备就绪,只需要运行内置的本地开发服务器即可。

python manage.py runserver
django project

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

代码精进之路 -〔范学雷〕

现代C++编程实战 -〔吴咏炜〕

Selenium自动化测试实战 -〔郭宏志〕

手机摄影 -〔@随你们去〕

手把手教你玩音乐 -〔邓柯〕

性能优化高手课 -〔尉刚强〕

现代React Web开发实战 -〔宋一玮〕

Serverless进阶实战课 -〔静远〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)