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, ...
随机推荐
- 更改Pch配置
在Build Settings 里搜索prefix 在第一个AppleLLVM.7.0 有一个 PrefixHeader 属性 在属性里添加 $(SRCROOT)/加上pch文件名字 然后编 ...
- Windows应用替代方案接龙
使开源软件的优势: 开源安全产品的开发.测试和发布过程完全是透明的,同时提供产品的源代码及部分的文档.通过阅读源代码,大家可以清楚地了解开源安全技术的工作原理和实现方法,在选择开源安全技术时更有把握, ...
- .Net字符串驻留池
在.Net中,对于相同的字符串,.Net会将它们指向同一个地址,它们是相同的实例..Net中的字符串并不会更新,当更改一个字符串变量时,由于字符串的不可变性,.Net实际上是新创建一个字符串,而将变量 ...
- jquery的end(),addBack()方法example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS阻止页面双击选中文本
转载自:w3cui 在双击左右箭头,快速切换图片滚动时,会选择附近区域的文字,感觉不是很好,今天在同事在分享时,讲到了这个问题, 试了一下,不错,解决了问题IE及Chrome下的方法一样,对相应的元素 ...
- 做权限树时 使用EasyUI中Tree
符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的 [{ "id":1, "text":"My Documents", & ...
- SQL:判断某些元素是否存在及创建的SQL语句
--判断某个存储过程是否存在if exists (select * from sysobjects where id = object_id(N'[p_CreateTable]') and OBJEC ...
- leetcode 26
26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- Android IOS WebRTC 音视频开发总结(五七)-- 网络传输上的一种QoS方案
本文主要介绍一种QoS的解决方案,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help QoS出现的背景: 而当网络发生拥塞的时候,所有的数据流都有 ...
- JavaScript的匿名函数和模块化的使用方法
对于开发人员来说,很多时候我们都会涉及到JavaScript的使用,而在使用过程中,最令人沮丧的就是变量没有相应的使用范围. 在开发中,对于任何变量.数组.函数.对象等,只要不在函数的内部,都会被默认 ...