zoukankan      html  css  js  c++  java
  • 13. 创建项目及应用

    创建项目与应用

    创建项目

    如果你想把你的项目放在D盘,请在D盘打开cmd命令窗口(按住shift+鼠标右键)。输入django-admin,出现如下内容

    D:>django-admin
    
    Type 'django-admin help <subcommand>' for help on a specific subcommand.
    
    Available subcommands:
    
    [django]
        check
        compilemessages
        createcachetable
        dbshell
        diffsettings
        dumpdata
        flush
        inspectdb
        loaddata
        makemessages
        makemigrations
        migrate
        runserver
        sendtestemail
        shell
        showmigrations
        sqlflush
        sqlmigrate
        sqlsequencereset
        squashmigrations
        startapp
        startproject
        test
        testserver
    Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but
    settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() befo
    re accessing settings.).
    

    我们使用startproject命令来创建项目,项目命名为blog。D:>django-admin startproject blog
    我们用pycharm打开blog(FileOpen),项目结构如下:

    结构介绍

    • blog/init.py: 一个空的文件, 用它标识一个目录为 Python 的标准包。
    • blog/settings.py: Django 项目的配置文件, 包括 Django 模块应用配置, 数据库配置, 模板配置等。
    • blog/urls.py: Django 项目的 URL 声明。
    • blog/wsgi.py: 为 WSGI 兼容的 Web 服务器服务项目的切入点。
    • manage.py: 一个命令行工具, 可以让你在使用 Django 项目时以不同的方式进行交互。

    创建应用

    我们使用cd命令进入blog目录,然后输入python manage.py(注意,我把python重命名为python3,你们输入python即可),出现如下内容:

    D:>cd blog
    D:log>python3 manage.py 
    Type 'manage.py help <subcommand>' for help on a specific subcommand.
    
    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 
    

    我们使用startapp命令来创建应用,应用命名为article。D:log>python3 manage.py startapp article
    我们重新查看pycharm中blog项目目录,会发现目录发生了一些改变。

    结构介绍

    • migrations/: 用于记录 models 中数据的变更。
    • admin.py: 映射 models 中的数据到 Django 自带的 admin 后台。
    • apps.py: 在新的 Django 版本中新增, 用于应用程序的配置。
    • models.py: 创建应用程序数据表模型(对应数据库的相关操作) 。
    • tests.py: 创建 Django 测试。
    • views.py: 控制向前端显示哪些数据。

    运行Hello World

    1.我们打开settings.py,修改INSTALLED_APPS,增加一行'article',如下代码所示:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'article',  # 新增
    ]
    

    2.打开urls.py,输入如下代码:

    from django.contrib import admin
    from django.urls import path
    from article import views #新增
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('index/', views.index), #新增
    
    ]
    

    3.在article应用下面创建 templates/hello.html 文件,hello.html输入hello world,注意templates是固定写法,如下图所示:

    4.打开views.py,输入如下代码:

    from django.http import HttpResponse 
    from django.shortcuts import render
    
    
    # Create your views here.
    
    def index(request):
        return render(request, "hello.html")
    

    5.我们当前还在blog目录,在cmd窗口输入python manage.py runserver,如下内容所示:

    D:log>python3 manage.py runserver
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have 15 unapplied migration(s). Your project may not work properly until you …ttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    January 16, 2019 - 16:39:20
    Django version 2.1.4, using settings 'blog.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.
    

    6.浏览器访问http://127.0.0.1:8000/index/,出现下图所示,说明访问成功。

    小结:想想看,我们做了啥 。我们首先通过django-admin blog创建一个blog项目,然后使用python manage.py startapp article创建一个应用。修改了settings.py目的是为了添加article应用,然后修改url.py,是为了新增一个请求地址,通过该请求地址,指向index函数。那么为什么会返回hello world呢?因为我们的index函数指向了hello.html文件。流程如下:

    欢迎关注微信公众号:软件测试汪。软件测试交流群:809111560

  • 相关阅读:
    [php]php设计模式 (总结)
    MySql常用命令总结
    mysql常用命令
    搜集几个API接口
    c语言 11-7
    c语言中转换字符串函数 atoi函数
    c语言中 strncmp函数, 函数原型和头文件。
    c语言中strcmp函数,函数原型和函数头文件
    c语言中strncat函数,函数原型以头文件
    c语言中strcat函数,函数原型和函数头文件
  • 原文地址:https://www.cnblogs.com/suim1218/p/10677552.html
Copyright © 2011-2022 走看看