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, ...
随机推荐
- c#面试题及答案
1:a=10,b=15,在不用第三方变量的前提下,把a,b的值互换2:已知数组int[] max={6,5,2,9,7,4,0};用快速排序算法按降序对其进行排列,并返回数组3:请简述面向对象的多态的 ...
- Java语法细节(2)
1.逻辑运算符 &和&&,|和||的区别 &&:和&的结果是一样的,但运算过程有区别 &&:只要左边结果为假,就不再执行右边的,结果为假 ...
- javascript 详解数组
概念 数组创建 数组读写 数组 VS. 一般对象 相同点 不同点 稀疏数组 数组的length属性 元素增删 数组迭代 二维数组 数组方法 Array.prototype.join Array.p ...
- CentOS 6.x版本升级Mysql
首先确定一下自己的DNS vi /etc/resolv.conf 我一般为114.114.114.114 #-----------------------------CentOS 6.x版本升 ...
- oracle中=>是什么意思
=> 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...
- MongoDb 2.4 beta新特性——全文索引
期待已久的特性,但目前仍然在beta阶段,所以官方建议不要在生产环境使用.也因此需要手动打开这个特性. 在命令行指定 mongod --setParameter textSearchEnabled=t ...
- 利用crontab自动更新SVN代码
shell.sh#!/bin/sh source ~/.bash_profile LANG=en_US.UTF- cd /opt/web/hzcms/ svn up >> /opt/web ...
- PHP JS HTML ASP页面跳转代码 延时跳转代码
1.PHP延时跳转代码 //跳转到浏览界面 header("Refresh:1;url=machine_list.php"); //不延时 <?php header(&quo ...
- jquery 匹配select下拉框与列表框
今天工作中用到 GrapyCity 的 wijmo ui 控件. 要给系统中所有类型的控件加统一样式 用法 $("input [type='text']").wijtext(); ...
- 第三节:卸载AppDomain
AppDomain很出色的一个能力就是它允许卸载.卸载AppDomain会导致CLR卸载AppDomain中的所有程序集.还会释放AppDomain的Loader堆.为了卸载一个AppDomain,可 ...