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, ...
随机推荐
- [drp 4] 使用dom4j,读取XML数据,保存至数据库
导读:上篇文章介绍了用XML文件配置数据库的连接,然后通过读取XML文件连接数据库的内容,本篇博客介绍读取XML文件,进行数据持久化的操作.PS:从某种意义上来说,经过Scheme校正的XML文件,本 ...
- c#实现每隔规定时间自动执行程序代码
c#实现每隔规定时间自动执行程序代码 在一般的项目中我们很少用到c#实现每隔规定时间自动执行程序代码,但是如果你经历的项目多,或者应用程序做的比较多的话,c#实现每隔规定时间自动执行程序代码就用的比 ...
- Atom markdown .md 编写格式技巧
使用Atom预览markdown 1.打开任意.md文件(markdown源文件) 菜单栏File->Open file...(ctrl+o)打开文件: 2.windows下使用快捷键 ctrl ...
- Excel2007给表格设置成只读加密属性 让他人无法修改
在制作一些报表和公司的表格时,我们常常需要给Excel表格加密或者设置成只读属性来加以防护这些重要表格.给表格加密可以预防他人无法打开表格,只有通过输入正确的密码后才可以正常打开.设置成只读的话,可以 ...
- 操作系统模仿CMD
实验一.命令解释程序的编写 专业:商软(2)班 姓名:列志华 学号:201406114254 一. 实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3 ...
- hdu2571
if(x==1) f(x,y)=max{f(x,y-1),f(x,z)} {y%z==0&&1<y/z<=y&&1<=z<<y} els ...
- 使用java配置定时任务的几种配置方式及示例
Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package ...
- 用户View,五大布局
1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...
- 7-ZIP实现数据高度压缩
From:http://www.cnblogs.com/killerlegend/p/3746395.html Author:KillerLegend Date:2013.5.22 选中文件,鼠标右键 ...
- Git - Tutorial [Lars Vogel]
From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...