My First Django Project (2)
1. 接下来是比较重要的VIEWS.py,您将会比较多的时间在这.有点想.net里面的aspx的cs概念,而aspx就是和接下来要创建的template html相似!
下面是我创建的一个view def.
from django.shortcuts import render, render_to_response
def alert(request):
posts = AlertMaster.objects.all() #这里的alertmaster一个表的对象,这里是输出他的所有对象
return render_to_response('external.html',{'posts':posts}) #第一个参数是要使用的模板名称。第二个参数,是为该模板创建 Context 时所使用的字典
文件夹结构: │ admin.py
│ models.py <-----------------model数据类
│ tests.py
│ views.py<-------------------views - controller - 用于定义def返回请求页面和context
│ __init__.py
│
├─migrations
│ __init__.py
│ __init__.pyc
│
├─static <--------------------------------用去存放css或者js之类的文件夹
│ │ body.css
│ │ jquery-1.8.2.min.js
└─templates <-------------------------------此地方用来存放模板
ajax.html <------------------------------ajax请求返回模板
alert.html <----------------------------主模板
external.html<--------------------------extended模板
2.1 external.html
{% extends "alert.html" %} #继承主模板alert.html
{% block content %}
{% for post in posts %}
<tr>
<td>{{post.production_day}}</td>
<td>{{post.silo}}</td>
<td>{{post.server}}</td>
<td>{{post.region}}</td>
<td>{{post.service}}</td>
<td>{{post.osm}}</td>
<td>{{post.pap}}</td>
<td>{{post.sla}}</td>
</tr>
{%endfor%}
{% endblock %}
2.2 alert.html
<table style="clear:both;" class="altrowstable" id="alternatecolor">
{%block content%}{% endblock%} #此处预留给extended.html,方便模板的拼接和可拓展
</table>
3. ajax 请求 views.py
jquery/ajax 代码:
$.post("/ajax_response/", {"param": a,"param1":b,"param2":c}, function (data) { #URL部分"/ajax_response/"将在urls.py中重定向
$("#alternatecolor").html(data); });
Urls.py 代码:
url(r'^alert/$', alert), # 将进入views.py 中 def alert:
url(r'^ajax_response/$',ajax_response), #ajax URL定义(如上块ajax请求URL所定义),返回views.py中定义的def ajax_response 方法(如下块代码)
views.py ajax controller 代码:
def ajax_response(request):
param=request.POST.get("param") #获取由模板ajax传来的参数
raw_sql='select * from alert.alert_master where param= "'+param+'"'
tests = AlertMaster.objects.raw(raw_sql) #执行rawsql
return render_to_response('ajax.html',{'tests':tests}) #返回带新字典的ajax页面(html)
My First Django Project (2)的更多相关文章
- [Python] Create a Django project in Pycharm
From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...
- Start Your Django Project in Nginx with uWsgi
Step 0:Install A,B,C,blabla needed This can be seen in my another article in the blog.click here(una ...
- Apache:To Config The Vhost of Django Project
It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...
- Django project troubleshootings
1. 当django project文件夹放到cgi-bin目录下面时会出现下面的错误: [Wed Jan 09 01:52:52.611690 2019] [core:notice] [pid 15 ...
- django project 的快速构建
2003年,堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组 ——World Online 小组,为了方便制作维护当地的几个新闻站点(一般要求几天或者几小时内被建立),Adrian ...
- Prepare tasks for django project deployment.md
As we know, there are some boring tasks while deploy Django project, like create db, do migrations a ...
- 18 12 30 新建一个 django project
1. 新建一个 django project 1 2 django-admin.py startproject project_name 特别是在 windows 上,如果报错,尝试用 django- ...
- Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work
So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally ...
- [Django] Start a new django project
Assume we already have a env created call 'demo-env': cd demo-env . bin/activate pip install django ...
- My First Django Project (3) - Apache set up
Holy moly!!!!因为漏了一下斜杠,害我反复调试了2,3天,无法读取static 文件,一直找不出原因,后来在apache的error.log中发现了原因. 1. 下载了apache 2.4, ...
随机推荐
- Puppet安装与配置简介(附视频教程)
Puppet是一种Linux平台的集中配置管理系统,他可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资源,puppet采用C/S星状的结构,所有的客户端和一个 ...
- iOS iOS7 20px 处理
- (void)viewWillAppear:(BOOL)animated { // View defaults to full size. If you want to customize the ...
- ibatis 改下数据库连接
<!--<dataSource name="iBatisNet" connectionString="data source= DESKTO ...
- Find out who the “mole” is?
Blueheat Company’s production server was out of order again. The CEO was very upset and want their ...
- Android IOS WebRTC 音视频开发总结(六一)-- 大数据解密国内实时通讯行业开发现状
本文主要介绍国内实时通讯行业现状,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 这几年移动互联网发展势头很猛,与 ...
- MySQL的20条基本优化 加参考资料
mysql的性能优化包罗甚广:索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整理 ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- (转载)前端构建工具gulp使用
前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中 ...
- MongoDB的安装小结
正在做毕业设计,想尝试着用mongoDB来做数据库,之前没有接触过,然后,就在网上找资料,自己捣鼓,弄了好久才算上真正的把它安上,好心累.... 网上有很多安装教程,大同小异,这里呢,我只是想记录一下 ...
- 深入浅出MongoDB(一)NoSQL
从本文开始,我们一起学习一下MongoDB相关内容,在学习MongoDB之前,首先要做的就是学习NoSQL. 为什么要学习NoSQL,原因很简单,因为MongoDB是NoSQL数据库的一种,换言之,如 ...