`
stingh711
  • 浏览: 126189 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

My first ajax app using django and jquery

阅读更多
Requirement:
There is a simple form that has two input field, one is zip code and another is city. If the zip code is filled, the page will fill the city automatically by getting the relevant city of the zip code in an AJAX way.

Implementation:
1. django-admin.py startproject study
2. cd study
3. python manage.py startapp ajax
4. Edit settings.py, add 'study.ajax' to INSTALLED_APPS.
5. mkdir site_media
6. Download jquery.js from www.jquery.com and copy it to site_media
7. Add the following line to urls.py:
<!---->(r'^site_media/(?P<path>.*)$''django.views.static.serve', {'document_root':'/home/sting/Django/study/site_media'}),
  Note: We will include jquery.js in our html, for this kind of static files, django's handling is different from others. Please see http://www.djangoproject.com/documentation/static_files/ to get more detailed information.
8. cd ajax
9. mkdir templates
  Note: This folder is used to put template files. It seems that django can load template files from this folder automatically, so you needn't to configure it in settings.py.
10. Create the following html file named form.html.
<!----><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Register form</title>
<script src="http://localhost:8000/site_media/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
  
function getZipData() {
    
var zipcode = $('#zip').val();
    $.get('http:
//localhost:8000/ajax/getzipdata/' + zipcode + '/', function(data) {
      $('#city').attr({'value':data});
    });
  }
</script>
</head>
<body>
<table>
<form actioon="" method="post">
<tr><td>Zip:</td><td><input id="zip" onblur="getZipData()" type="text" name="zip"/></tr>
<tr><td>City:</td><td><input id="city" type="text" name="city"/></tr>
</form>
</table>
</body>
</html>
11. Edit views.py and add the following methods.
<!---->from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import render_to_response

def getzipdata(request, zipcode):
  
if zipcode == '214028':
    city 
= 'wuxi'
  
else:
    city 
= ''

  
return HttpResponse(city)

def register(request):
  
return render_to_response('form.html', {})
12. Add the following lines to urls.py.
<!---->(r'^ajax/register/$''study.ajax.views.register'),
(r
'^ajax/getzipdata/(\d+)/$''study.ajax.views.getzipdata'),

Visit http://localhost:8000/ajax/register/, you will see the simple form, after fill the zip code and move to city, the city will be filled automatically.

Blogged with Flock

Tags: django, jquery, ajax, python

分享到:
评论
2 楼 stingh711 2008-08-18  
static file本来就是用的绝对路径:)
1 楼 supercode 2008-07-13  
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'/home/sting/Django/study/site_media'}),

document_root好像要用绝对路径,相对路径好像没有效果

相关推荐

    Django JavaScript AJAX and jQuery使用教程

    这本书的标题《Django JavaScript AJAX and jQuery使用教程》透露了其核心内容,即向读者介绍如何在Django框架中集成JavaScript,特别是通过使用AJAX和jQuery这两个强大的工具来创建Web应用程序。AJAX(Asynchronous...

    Django + jquery + json 实例实现 在此记录

    在本实例中,我们将探讨如何使用Django框架与jQuery库以及JSON数据格式来构建一个功能性的Web应用。Django是一个强大的Python Web框架,它提供了一整套解决方案来处理后端逻辑,而jQuery则是一个广泛使用的...

    django + jquery First Test Code

    本项目"django + jquery First Test Code"旨在演示如何结合这两种技术进行开发,以实现高效且用户友好的Web应用程序。 Django是一个用Python编写的高级Web框架,它遵循模型-视图-控制器(MVC)架构模式。Django强调...

    django ajax异步请求Django的Ajax库3813428.zip

    在Django中,我们通常使用jQuery库,它提供了便捷的Ajax函数,如`$.ajax()`和`$.get()`、`$.post()`等,使得发送Ajax请求变得简单。 **一、创建Ajax请求** 在前端,我们通常在JavaScript或jQuery代码中创建Ajax...

    用django+jquery做了一个即时更新的测试

    Django是著名的Web开发框架,以其MVT(Model-View-Template)架构模式著称,而jQuery则是前端开发中广泛使用的JavaScript库,简化了DOM操作、事件处理和Ajax交互。 【描述】:“准备开发webIM,今天用django+jquery...

    Django JavaScript Integration AJAX and jQuery.pdf

    《Django JavaScript Integration AJAX and jQuery》这本书深入探讨了如何将Django框架与JavaScript技术(包括AJAX和jQuery)进行有效集成,以构建现代化的Web应用。本书不仅适合已经熟悉Django框架并希望进一步提升...

    Packt.Django.JavaScript.Integration.AJAX.and.jQuery

    《Packt.Django.JavaScript.Integration.AJAX.and.jQuery》这本书主要涵盖了使用Django框架与JavaScript技术,特别是AJAX和jQuery,进行高效Web开发的知识点。以下是对这些主题的详细解析: **Django框架**: ...

    ztree ajax异步请求 django (服务端)

    ztree ajax异步调用,使用django作为服务端,可正常运行。开启服务(项目根目录下,与manange.py同级, 执行python manage.py runserver开启服务),浏览器输入地址http://127.0.0.1:8000/ztree/standar/即可

    Django结合ajax进行页面实时更新的例子

    在Web开发中,Django和Ajax的结合使用可以极大地提升用户体验,通过无刷新的方式实现实时页面更新。本文将详细讲解如何在Django框架下利用Ajax实现数据的动态更新。 首先,我们需要理解Django和Ajax的基本概念。...

    pyecharts_Django_Ajax_web前后端分离demo.zip

    - 使用JavaScript(可能是jQuery或者其他库)发送Ajax请求到Django服务器的特定URL。 - Django接收到请求后,执行相应的视图函数,可能涉及到数据库查询或业务逻辑处理。 - 视图函数处理完数据后,将结果转化为JSON...

    Django-jQuery-File-Uploader-Integration-demo-master.zip

    标题 "Django-jQuery-File-Uploader-Integration-demo-master.zip" 暗示这是一个关于集成jQuery文件上传器到Django框架的演示项目。这个压缩包包含了一个完整的示例,用于展示如何在Django web应用中使用jQuery实现...

    基于Django+ JQuery 学生就业管理系统.zip

    在Django项目中,JQuery可以用于前端的交互效果,例如通过Ajax调用Django视图函数,实现异步数据交换,提升用户体验。 4. **模型-视图-控制器(MVC)模式**: Django遵循MVC模式,其中模型(Model)负责数据管理,...

    基于django,jquery ,bootstrap4的一个后台管理系统.zip

    【标题】: "基于Django,jQuery,Bootstrap4的后台管理系统" 这个后台管理系统是利用了三个核心的技术:Django(一个强大的Python Web框架),jQuery(一个高效、简洁的JavaScript库),以及Bootstrap4(一个流行的...

    通过Django的form及Ajax等多种方式上传文件

    通过Django的Form方式上传文件,简单,但无法局部刷新实现ajax效果; 通过js或jQuery方式,需要使用FormData对象,仍有些浏览器不兼容; 通过Form+iframe方式上传文件,兼容性最好。 里面含有全部源代码。

Global site tag (gtag.js) - Google Analytics