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, ...
随机推荐
- CLRS: online maximum (n,k)algorithm
//the first k elements interviewed and rejected, //for the latter n-k elements ,if value >max,re ...
- Windows phone 8 学习笔记(9) 集成(转)
本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用提供的相关支持:锁定屏幕展示了我们可以对锁定屏幕提供背景图像 ...
- 简单linux字符设备驱动程序
本文代码参考<LINUX设备驱动程序>第三章 字符设备驱动程序 本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间 功能:向字符设备写入字符串:从字符设备读出字符串 代码: 1. ...
- Hive[1] 初识 及 安装
本文前提是Hadoop & Java & mysql 数据库,已经安装配置好,并且 环境变量均已经配置到位 声明:本笔记参照 学习<Hive 编程指南>而来,如果有错误 ...
- CentOS下修复grub引导程序
1.早上装系统,试了好几次.最终grub修复成功! 解决方法大致如下: 开机遇见 grub>的时候 下面看解决方法 首先是既然插上U盘能进系统 那我们插上U盘 正常进入系统 然后看操作 [r ...
- LevelDB源码剖析
LevelDB的公共部件并不复杂,但为了更好的理解其各个核心模块的实现,此处挑几个关键的部件先行备忘. Arena(内存领地) Arena类用于内存管理,其存在的价值在于: 提高程序性能,减少Heap ...
- 基于zookeeper的远程方法调用(RMI)的实现
采用zookeeper的命名服务,采用不同的目录结构存储不同模块不同服务的rmi的url,使用key来对应不同的服务.同时采用zookeeper解决了单点问题. 当有两个相同的服务注册时,因为采用的是 ...
- JS正则表达式获取字符串中特定字符
JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test 实 ...
- 取消界面的title
在setContentView(R.layout.activity_main)方法上面添加代码(继承Activity的写法): requestWindowFeature(Window.FEATURE_ ...
- Wordpress模板标签大全
Wordpress模板基本文件 style.css 样式表文件 index.php 主页文件 single.php 日志单页文件 page.php 页面文件 archvie.php 分类和日期存档页文 ...