Python Django

官网

       https://www.djangoproject.com/

概念

       Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

安装

       官网下载与本机Python对应版本的Django后,python setup.py install

       版本选择 - 长期支持版本Django 1.11.x、Django 1.8.x

       python -m django --version

常用命令

创建项目

       django-admin startproject project_name

创建应用

       python manage.py startapp app_name

       必须添加应用名到settings.py的INSTALL_APPS里(直接添加应用名即可)

使用开发服务器

       python manage.py runserver

       python manage.py runserver 8080

       python manage.py runserver 0:8080

       注:让其他服务器也能访问到需要在runserver后跟${本机IP}:port或者0.0.0.0:port或者0:port

       https://docs.djangoproject.com/en/2.0/ref/django-admin/#runserver

创建管理员

       python manage.py createsuperuser

修改管理员密码

       python manage.py changepassword username

项目目录结构分析(__init__.py不再说明)

>>>创建项目后(${workspace}/${project_name}下)

manage.py

       与项目进行交互命令行工具集的入口,即项目管理器。

       可以进行如清理会话、启动服务等工作

${project_name}

       项目的一个容器,包含项目的一些基本配置。

       目录名称不建议修改。(已作为包加载,更改较麻烦)

${project_name}/settings.py

       项目的总配置文件。包含了项目全局变量、数据库、web应用、时间等各种配置

${project_name}/urls.py

       项目的url配置文件

       django项目中所有地址(页面)都需要我们自己去配置URL

       https://docs.djangoproject.com/en/2.0/topics/http/urls/

       通过包模式引入${app_name}/views.py(import ${app_name}.views,然后配置该views中的函数到urlpatterns中)

${project_name}/wsgi.py

       WSGI - Python Web Server Gateway Interface

       python服务器网关接口,即python应用与web服务器之间的接口

>>>创建应用后(${workspace}/${project_name}下)

${app_name}/admin.py

       当前应用的后台管理系统配置文件

${app_name}/apps.py

       关于该应用的一些配置

       Django-1.9之后自动生成

${app_name}/migrations/

       数据库迁移模块

${app_name}/models.py

       数据模型模块(创建数据表时使用)

       使用ORM框架

       类似于MVC结构中的Models

${app_name}/tests.py

       自动化测试模块

${app_name}/views.py

       执行响应的代码所在模块

       代码中逻辑处理的主要地点

       项目中大部分代码均在这里编写

       类似于MVC结构中的Views

${app_name}/urls.py(需自行创建)

       见${project_name}/urls.py的Including another URLconf

       在${project_name}/urls.py中指定各应用使用的urls配置文件,在多应用、多页面时便于管理

${app_name}/templates(需自行创建)

       template放置的就是HTML文件

       template使用模版引擎(默认使用Django模板语言(DTL))

       可以使用第三方模版(如Jinja2)

       修改模版:${project_name}/settings.py -> TEMPLATE -> BACKEND

使用django搭建服务器思路

创建项目

       创建并添加应用

编写urls.py来添加访问的url

       可include各应用的urls.py

       可指定url的name,name作用见下

根据urls.py中的url编辑views.py

       可以获取到该url本身

       可以直接返回字符串,

       可以返回templates下的html文件

       每个url对应一个函数

       一个函数可以对应多个URL

       函数必须返回一个响应(HttpResponse,或者render)

       函数最少有一个形参,一般使用request来表示

编写html文件

       可以获取到views.py中传进来的参数

       可以获取到该url本身

Demo

       https://github.com/HanChengITer/PyStudy/tree/master/django_demo

views.py、templates文件夹、urls.py关系

       url所带的参数可以在views.py中使用request.GET[${param_name}]来获得

       urls.py中如果有使用正则分组匹配到的非url参数,则需在views.py的函数定义处使用对应数量的形参接收

       templates目录下放置着所有的html

       views.py如果要在界面中显示string,则使用HttpResponse;

       views.py如果需要显示指定HTML,则需要使用render(request, 'HTML位置', '传递给HTML的参数dict')渲染,且该HTML位于该应用下的templates文件夹中

       注意:如果一个项目中有多个应用使用的template有同名(如index.html),则会出现问题,所以解决办法:1.在templates目录下再建立一个以应用名命名的文件夹,将该同名template(即HTML)放在该目录下;2.创建HTML时即以应用名区分

urls.py中name的作用

       为每个进入到服务器urls.py中的url取一个名字,通过这个名字,我们可以在templates下的HTML文件中以变量形式获取到当前访问的url,这样,在我们修改了url.py中的url路径(分组有变动的情况除外),我们不需要修改views.py和template下的py文件中的内容

错误

1. You have 14 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.

http://blog.csdn.net/xufeng0991/article/details/40421857

附录

python代码调试

http://www.cnblogs.com/zhbzz2007/p/5502792.html

django学习网站

https://code.ziqiangxuetang.com/django/

virtualenv和virtualenvwrapper插件的使用

https://www.jianshu.com/p/6a3ff66cb8d3

基本命令

https://code.ziqiangxuetang.com/django/django-basic.html

Demo - HelloWorld

https://code.ziqiangxuetang.com/django/django-views-urls.html

views网址参数获得与网址获得

https://code.ziqiangxuetang.com/django/django-views-urls2.html

urls中name作用/url内部跳转

https://code.ziqiangxuetang.com/django/django-url-name.html

模版基础

https://code.ziqiangxuetang.com/django/django-template.html

模板中的循环、判断、过滤器等使用

https://code.ziqiangxuetang.com/django/django-template2.html

原文地址:https://www.cnblogs.com/yc913344706/p/8169596.html