安装好django,配置模板,静态文件

	# 创建Django工程
django-admin startproject 【工程名称】 mysite
- mysite # 对整个程序进行配置
- init
- settings # 配置文件
- url # URL对应关系
- wsgi # 遵循WSIG规范,uwsgi + nginx
- manage.py # 管理Django程序:
- python manage.py
- python manage.py startapp xx
- python manage.py makemigrations
- python manage.py migrate # 运行Django功能
python manage.py runserver 127.0.0.1:8001 chouti
- chouti
- 配置
- 主站 app
- 后台管理 app # 创建app
python manage.py startapp cmdb
python manage.py startapp openstack
python manage.py startapp xxoo.... app:
migrations 数据修改表结构
admin Django为我们提供的后台管理
apps 配置当前app
models ORM,写指定的类 通过命令可以创建数据库结构
tests 单元测试
views 业务代码 1、配置模板的路径 TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
2、配置静态目录
static STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
) <link rel="stylesheet" href="/static/commons.css" /> 内容整理
1. 创建Django工程
django-admin startproject 工程名 2. 创建APP
cd 工程名
python manage.py startapp cmdb 3、静态文件
project.settings.py STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
) 4、模板路径 TEMPLATES中加 [os.path.join(BASE_DIR,'templates'),] 5、settings中 middlerware # 注释 csrf 6、定义路由规则
url.py "login" --> 函数名 7、定义视图函数
app下views.py def func(request):
# request.method GET / POST # http://127.0.0.1:8009/home?nid=123&name=alex
# request.GET.get('',None) # 获取请求发来的而数据 # request.POST.get('',None) # return HttpResponse("字符串")
# return render(request, "HTML模板的路径")
# return redirect('/只能填URL') 8、模板渲染
特殊的模板语言 -- {{ 变量名 }} def func(request):
return render(request, "index.html", {'current_user': "alex"}) index.html <html>
..
<body>
<div>{{current_user}}</div>
</body> </html> ====> 最后生成的字符串 <html>
..
<body>
<div>alex</div>
</body> </html>
-- For循环
def func(request):
return render(request, "index.html", {'current_user': "alex", 'user_list': ['alex','eric']}) index.html <html>
..
<body>
<div>{{current_user}}</div> <ul>
{% for row in user_list %} {% if row == "alex" %}
<li>{{ row }}</li>
{% endif %} {% endfor %}
</ul> </body> </html> #####索引#################
def func(request):
return render(request, "index.html", {
'current_user': "alex",
'user_list': ['alex','eric'],
'user_dict': {'k1': 'v1', 'k2': 'v2'}}) index.html <html>
..
<body>
<div>{{current_user}}</div> <a> {{ user_list.1 }} </a>
<a> {{ user_dict.k1 }} </a>
<a> {{ user_dict.k2 }} </a> </body> </html> ###### 条件 def func(request):
return render(request, "index.html", {
'current_user': "alex",
"age": 18,
'user_list': ['alex','eric'],
'user_dict': {'k1': 'v1', 'k2': 'v2'}}) index.html <html>
..
<body>
<div>{{current_user}}</div> <a> {{ user_list.1 }} </a>
<a> {{ user_dict.k1 }} </a>
<a> {{ user_dict.k2 }} </a> {% if age %}
<a>有年龄</a>
{% if age > 16 %}
<a>老男人</a>
{% else %}
<a>小鲜肉</a>
{% endif %}
{% else %}
<a>无年龄</a>
{% endif %}
</body> </html>

  

django 初始化 介绍 生命周期的更多相关文章

  1. Django的请求生命周期

    Django的请求生命周期 请求生命周期 请求生命周期是指当用户在浏览器上输入url到用户看到网页的这个时间段内,Django后台所发生的事情. 1.客户端发送Http请求 2 .服务器接收,根据请求 ...

  2. Django 框架 django的请求生命周期

    概述 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串,同时http请求是无状态的,可以通过session和cookie来辅助. 浏览器通过ip和端口及路由方式访问服务端. 在Djang ...

  3. django请求的生命周期

    1. 概述 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串. 在Django中,当我们访问一个的url时,会通过路由匹配进入相应的html网页中. Django的请求生命周期是指当用户 ...

  4. DRF框架(一)——restful接口规范、基于规范下使用原生django接口查询和增加、原生Django CBV请求生命周期源码分析、drf请求生命周期源码分析、请求模块request、渲染模块render

    DRF框架    全称:django-rest framework 知识点 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下的CBV接口 3.请求组件 ...

  5. rest_framework框架下的Django声明和生命周期

    rest_framework框架下的Django声明和生命周期 Django声明周期(request) 客户端发起请求 请求经过wsgi wsgi: 是一个协议----web服务网关接口,即在web服 ...

  6. Servlet学习笔记(一)之Servlet原理、初始化、生命周期、结构体系

    Servlet是用java语言编写的应用到Web服务器端的扩展技术,与java对象的区别是,Servlet对象主要封装了对HTTP请求的处理,并且它的运行需要Servlet容器的支持(以下会介绍原因, ...

  7. Spring 基础知识(二)Spring的bean初始化与生命周期,以及注入

    Spring bean 初始化: 参考博文: https://www.cnblogs.com/luyanliang/p/5567164.html 1. 加载xml 文件. 扫描注解 ,形成bean定义 ...

  8. Django框架请求生命周期

    先看一张图吧! 1.请求生命周期 - wsgi, 他就是socket服务端,用于接收用户请求并将请求进行初次封装,然后将请求交给web框架(Flask.Django) - 中间件,帮助我们对请求进行校 ...

  9. Django的请求生命周期与中间件中的5中方法

    请求生命周期: 客户端——>WSGI——> 中间件——>路由匹配——>视图函数——>WSGI——>客户端 中间件: 在全局层明处理请求和响应的 form djang ...

随机推荐

  1. python 和 matlab的caffe读数据细节

    (1).prototxt中的输入表示一样,如 dim: 10 dim: 3 dim: 227 dim: 227 (2)代码喂入数据不一样: python:     input_blob = np.ze ...

  2. php在foreach中使用引用赋值&可能遇到的问题(转)

    楼主在写项目的时候,由于初涉PHP的赋值引用操作,觉得这个功能非常强大,用时一时爽,没有深入了解过其中的原理,导致了一些当时觉得不可思议的BUG,废话不都说,我举个例子详细的描述一下这个问题. 代码: ...

  3. 【leetcode】7-ReverseInteger

    problem: Reverse Integer 注意考虑是否越界: INT_MAX INT_MIN 32bits or 64bits 调整策略,先从简单的问题开始:

  4. python调用caffe环境配置

    背景是这样的,项目需要,必须将训练的模型通过C++进行调用,所以必须使用caffe或者mxnet,而caffe是用C++实现,所以有时候简单的加载一张图片然后再进行预测十分不方便 用caffe写pro ...

  5. mysql设置外键约束开启-关闭

    在MySQL中删除一张表或一条数据的时候,出现 [Err] 1451 -Cannot delete or update a parent row: a foreign key constraint f ...

  6. supervisor-program配置

    [program:check_server_state]directory=/sunlight/shellcommand=/usr/bin/sh check_server_state.shautost ...

  7. [LeetCode&Python] Problem 122. Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. active在iphone上不起作用

    在js中加一个空的touchstart函数 $(function(){ document.body.addEventListener('touchstart',function(){ }); 或在&l ...

  9. lamp 相关

    1.LAMP = linux + apache + mysql(mariadb/mongodb) + php 2.mysql 安装:先下载安装包: wget -c http://mirrors.soh ...

  10. 《DSP using MATLAB》Problem 6.20

    先放子函数: function [C, B, A, rM] = dir2fs_r(h, r); % DIRECT-form to Frequency Sampling form conversion ...