My First Django Project (2)

Source: Internet
Author: User

1. Next is the more important views.py, you will have more time here. A bit like the CS concept of aspx inside. NET, and ASPX is similar to the template HTML to be created next!

Here is a view def that I created.

From django.shortcuts import Render, Render_to_response
def Alert (Request): posts = AlertMaster.objects.all () #这里的alertmaster一个表的对象, Here is the output of all his objects return render_to_response ('external.html', { ' posts ':p osts}) #第一个参数是要使用的模板名称. The second parameter, which is the dictionary used when creating a Context for the template
  folder structure: 

│admin.py│models.py <-----------------model data class │tests.py│ views.py<-------------------Views-controller-Used to define DEF return request page and context│ __ init__ .py│├─migrations│ __init__ .py│ __init__ .pyc│├─static <--------------------------------use to store folders such as CSS or JS ││body.css││jquery -1.8.2.min.js└─templates <-------------------------------this place to store the template ajax.html Span style= "color: #993300;" ><------------------------------Ajax Request return template alert.html <-------------- --------------main template external.html<--------------------------Extended Template

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

<style= "Clear:both;" class = "Altrowstable" ID = "Alternatecolor" > {%block content%}{% endblock%}          #此处预留给extended. html, easy to splice and expand the template  </ Table >

3. Ajax Request views.py

Jquery/ajax Code:

function (data) {          #URL部分 "/ajax_response/" will be redirected in urls.py
$ ("#alternatecolor"). HTML (data);});

urls.py Code:

URL (r ' ^alert/$ ', alert),     # will enter views.py in Def alert:
URL (r'^ajax_response/$', ajax_response), #ajax URL definition (as defined by the previous block Ajax request URL) , return the Def Ajax_response method defined in views.py (block code below)

VIEWS.PY Ajax Controller Code:

defAjax_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 returnRender_to_response ('ajax.html',{'tests': Tests})#返回带新字典的ajax页面 (HTML)

My First Django Project (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.