欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django實(shí)現(xiàn)前后臺(tái)交互實(shí)例

 更新時(shí)間:2017年08月07日 11:02:03   作者:zhubaoJay  
本篇文章主要介紹了django實(shí)現(xiàn)前后臺(tái)交互實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了django實(shí)現(xiàn)前后臺(tái)交互實(shí)例,分享給大家,希望對(duì)大家有所幫助

準(zhǔn)備工作:

前端框架:AngularJS+bootstap

數(shù)據(jù)庫(kù):sqlite3

前端代碼:
index.html

<!DOCTYPE html> 
<html> 
  <head> 
    <link href="/WebApi/scripts/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="/WebApi/scripts/angular/angular.min.js"></script> 
    <script type="text/javascript" src="/WebApi/controller/controller.js"></script> 
    <script type="text/javascript" src="/WebApi/service/service.js"></script> 
    <title>hello</title> 
  </head> 
  <body ng-app="myApp" ng-controller="myCtrl"> 
    <h2>hello world!</h2> 
     
<!--   <form role="form"> --> 
    <table> 
      <tr> 
        <td> 
          <div class="form-group"> 
            <input type="text" class="form-control" id="name"  
          placeholder="請(qǐng)輸入用戶名" ng-model="username"> 
          </div> 
        </td> 
      </tr> 
      <tr> 
        <td> 
          <div class="form-group"> 
            <input type="passwd" class="form-control" id="name"  
          placeholder="請(qǐng)輸入密碼" ng-model="password"> 
          </div> 
        </td> 
      </tr> 
      <tr> 
        <td> 
          <button type="button" class="btn btn-primary" ng-click="my_submit()">保存</button> 
        </td> 
      </tr> 
    </table> 
<!--   </form> 
 --> 
 
    <p class="text-danger">[[ result ]]</p> 
  </body> 
</html> 

controller.js

var app = angular.module("myApp", []); 
app.config( 
  function($interpolateProvider) { 
    $interpolateProvider.startSymbol('[['); 
    $interpolateProvider.endSymbol(']]'); 
  })  
  .config(['$httpProvider', function($httpProvider) { 
    $httpProvider.defaults.xsrfCookieName = 'csrftoken'; 
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; 
}]); 
app.controller("myCtrl", ["$scope", "service", function($scope, service) { 
  $scope.result = ""; 
  $scope.my_submit = function() { 
    console.log($scope.username); 
    console.log($scope.password); 
    service.do_save_info($scope.username, $scope.password, function(response){ 
      console.log(response); 
      $scope.result = response.result; 
    }); 
  }; 
}]); 

service.js

app.service("service", ["$http", function($http) { 
  this.do_save_info = function(username, password, callback) { 
    $http({ 
      method: 'POST', 
      url: '/do_save_info', 
      data: { 
        'username': username, 
        'password': password 
      }, 
      headers: {'Content-Type': undefined}, 
    }).success(function(response){ 
      callback(response); 
    }); 
  }; 
}]); 

后端代碼:

urls.py

from django.conf.urls import patterns, include, url 
 
urlpatterns = patterns('app.views', 
  url(r'^index$', 'index'), 
  url(r'^/index$', 'index'), 
  url(r'^$', 'index'), 
  url(r'^/$', 'index'), 
  url(r'^do_save_info$', 'do_save_info'), 
) 

views.py

from django.shortcuts import render_to_response 
from django.template import RequestContext 
from django.http import HttpResponse 
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt 
import json 
import models 
 
# Create your views here. 
@ensure_csrf_cookie 
def index(request): 
  return render_to_response('static/index.html', 
    context_instance=RequestContext(request)) 
 
def do_save_info(request): 
  result = {'result':'save success'} 
  try: 
    data = json.loads(request.body) 
    username = data.get("username", None) 
    password = data.get("password", None) 
    models.do_save_info(username, password) 
  except Exception, e: 
    result['result'] = 'save error' 
  return HttpResponse(json.dumps(result)) 

models.py

#!/bin/python 
# -*- coding: utf-8 -*- 
 
import os 
import sys 
import sqlite3 
 
def do_save_info(username, password): 
  db_path = os.path.normpath('/home/zhubao/Code/django_code/hello/db.sqlite3') 
  try: 
    conn = sqlite3.connect(db_path) 
    sql = "insert into t_user(username, password) values('%s', '%s')" % (username, password) 
    conn.execute(sql) 
    conn.commit() 
    conn.close() 
    print 'save success...' 
  except Exception, e: 
    print '------', str(e) 
    try: 
      conn.close() 
    except Exception, e: 
      pass 

t_user表結(jié)構(gòu):

create table t_user(username varchar(255), password varchar(255)); 

頁(yè)面演示:

剛打開(kāi)頁(yè)面如下:


輸入數(shù)據(jù),點(diǎn)擊保存:


后臺(tái)查看數(shù)據(jù)庫(kù):

可以看到,已經(jīng)保存在數(shù)據(jù)庫(kù)里面了。

這只是個(gè)小示例,在此不考慮頁(yè)面排版和安全性問(wèn)題。。。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Pymongo常用查詢方法總結(jié)

    詳解Pymongo常用查詢方法總結(jié)

    這篇文章主要介紹了詳解Pymongo常用查詢方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python 調(diào)用釘釘機(jī)器人的方法

    python 調(diào)用釘釘機(jī)器人的方法

    今天小編就為大家分享一篇python 調(diào)用釘釘機(jī)器人的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • python如何實(shí)現(xiàn)不用裝飾器實(shí)現(xiàn)登陸器小程序

    python如何實(shí)現(xiàn)不用裝飾器實(shí)現(xiàn)登陸器小程序

    這篇文章主要介紹了python如何實(shí)現(xiàn)不用裝飾器實(shí)現(xiàn)登陸器小程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • python生成并處理uuid的實(shí)現(xiàn)方式

    python生成并處理uuid的實(shí)現(xiàn)方式

    這篇文章主要介紹了python生成并處理uuid的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python中np.random.randint()參數(shù)詳解及用法實(shí)例

    Python中np.random.randint()參數(shù)詳解及用法實(shí)例

    numpy.random.randint()函數(shù)不僅可以生成一維隨機(jī)數(shù)組,也可以生成多維度的隨機(jī)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于Python中np.random.randint()參數(shù)詳解及用法的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 使用pandas 將DataFrame轉(zhuǎn)化成dict

    使用pandas 將DataFrame轉(zhuǎn)化成dict

    今天小編就為大家分享一篇使用pandas 將DataFrame轉(zhuǎn)化成dict,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Django解決CORS跨域問(wèn)題的方案

    Django解決CORS跨域問(wèn)題的方案

    Cross-Origin Resource Sharing(CORS) 跨域問(wèn)題,在前后端后離項(xiàng)目,selenium , playweight 自動(dòng)化測(cè)試代碼中經(jīng)常遇到,本文給大家介紹了Django解決CORS跨域問(wèn)題的方案,文中有詳細(xì)的代碼講解,需要的朋友可以參考下
    2024-03-03
  • python字典中g(shù)et()函數(shù)的基本用法實(shí)例

    python字典中g(shù)et()函數(shù)的基本用法實(shí)例

    在字典內(nèi)置的方法中,想說(shuō)的方法為get,這個(gè)方法是通過(guò)鍵來(lái)獲取相應(yīng)的值,但是如果相應(yīng)的鍵不存在則返回None,這篇文章主要給大家介紹了關(guān)于python字典中g(shù)et()函數(shù)的基本用法,需要的朋友可以參考下
    2022-03-03
  • Python中使用subprocess庫(kù)創(chuàng)建附加進(jìn)程

    Python中使用subprocess庫(kù)創(chuàng)建附加進(jìn)程

    這篇文章主要介紹了subprocess庫(kù):Python中創(chuàng)建附加進(jìn)程的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Python鏈表排序相關(guān)問(wèn)題解法示例

    Python鏈表排序相關(guān)問(wèn)題解法示例

    這篇文章主要為大家介紹了Python鏈表排序相關(guān)問(wèn)題解法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01

最新評(píng)論