Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6895288
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: Python/Ruby

2014-08-15 08:03:51

原文地址:django和extjs结合入门实例 作者:wwm

很强大,是一个出色的前端。下面通过一个小例子来演示下使用。
centos环境,python2.7.6    django1.5.1  ext-4.2.1   

一、创建项目               
1、创建一个项目
    django-admin.py startproject django_extjs

2、创建两个新目录                  
  cd  django_extjs  #进入第1层目录
    cd  django_extjs   #进入第2层目录         
   mkdir static          #不同应用公用文件 ,如extjs,css  javascript等                   
    mkdir templates   #公用模版    
3、解开ext-4.2.1,复制extjs-4.2.1文件件到 /django_extjs/django_extjs/static下面                
      文件件        并修改名字为extjs               
   如:  /django_extjs/django_extjs/static/extjs

4、修改settings.py    (./django_extjs/django_extjs/settings.py   )
   1) 上面添加
      

点击(此处)折叠或打开

  1. import os
  2. _ROOT_PATH = os.path.dirname(__file__)

 2)修改database
 

点击(此处)折叠或打开

  1. DATABASES = {
  2.     'default': {
  3.         'ENGINE': 'django.db.backends.sqlite3', 
  4.         'NAME': os.path.join(_ROOT_PATH, 'django_extjs_tut.sqlite3'), 
  5.         'USER': '',
  6.         'PASSWORD': '',
  7.         'HOST': '',
  8.         'PORT': '',
  9.     }
  10. }
3、修改  STATICFILES_DIRS 
 

点击(此处)折叠或打开

  1. STATICFILES_DIRS = (
  2.     os.path.join(_ROOT_PATH, 'static'),
  3. )
4、修改TEMPLATE_DIRS     

点击(此处)折叠或打开

  1. TEMPLATE_DIRS = (
  2. os.path.join(_ROOT_PATH, 'templates'),
  3. )
5、测试配置
    python manage.py syncdb
6、创建公用模版  base.html(./django_extjs/django_extjs/templates/base.html)

                        

点击(此处)折叠或打开


  1. <html>
  2. <head>



  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">



  4. <link rel="stylesheet" type="text/css" href="/static/extjs/resources/css/ext-all.css">






  5. <script type="text/javascript" src="/static/extjs/adapter/ext/ext-base.js"></script>


  6. <script type="text/javascript" src="/static/extjs/ext-all-debug.js"></script>






  7. <title>{% block title %}My amazing site{% endblock %}</title>




  8. <script type="text/javascript">
  9. // Path to the blank image must point to a valid location on your server
  10. Ext.BLANK_IMAGE_URL = '/static/extjs/resources/images/default/s.gif';
  11. </script>

  12. {% block head %}{% endblock %}

  13. </head>

  14. <body>
  15. {% block content %}{% endblock %}
  16. </body>

  17. </html>
  18. ~
二、在项目下建立一个应用
1、建立hello_extjs    
cd  django_extjs  #进入第1层目录
python manage.py startapp hello_extjs

2、修改配置 settings.py
 在 INSTALLED_APPS 增加  hello_extjs                                    

点击(此处)折叠或打开

  1. INSTALLED_APPS = (
  2.     'django.contrib.auth',
  3.     'django.contrib.contenttypes',
  4.     'django.contrib.sessions',
  5.     'django.contrib.sites',
  6.     'django.contrib.messages',
  7.     'django.contrib.staticfiles',
  8.     'hello_extjs',
  9.   
  10. )
3、同步配置 
python manage.py syncdb

4、在hello_extjs 下创建目录
  cd hello_extjs 
   mkdir templates                
   mkdir templates/hello_extjs    

  mkdir static
   mkdir static/hello_extjs

5、添加路由
 django_extjs/urls.py             

点击(此处)折叠或打开

  1. urlpatterns = patterns('',
  2. (r'^hello_extjs/', include('hello_extjs.urls')),
  3. )

创建hello_extjs/urls.py

点击(此处)折叠或打开

  1. from django.conf.urls import patterns, include, url

  2. urlpatterns = patterns('hello_extjs.views',
  3. (r'^$', 'index'),
  4. )

6、创建模版文件
   ./hello_extjs/templates/hello_extjs/index.html                                                                                                                                                                                                                                 

点击(此处)折叠或打开

  1. {% extends "base.html" %}
  2. {% block title %}Hello中国 ExtJs{% endblock %}
  3. {% block head %}
  4. <script type='text/javascript' src='/static/hello_extjs/hello_extjs.js'></script>
  5. {% endblock %}
  6. {% block content %}
  7. <p>Click aa on me</p>
  8. {% endblock %}
7、创建js脚本文件 hello_extjs.js

点击(此处)折叠或打开

  1. Ext.onReady(function() {
  2. var paragraphClicked = function(e) {
  3. var paragraph = Ext.get(e.target);
  4. paragraph.highlight();

  5. Ext.MessageBox.show({
  6. title: 'Paragraph Clicked',
  7. msg: paragraph.dom.innerHTML,
  8. width:400,
  9. buttons: Ext.MessageBox.OK,
  10. animEl: paragraph
  11. });
  12. }
  13. Ext.select('p').on('click', paragraphClicked);
  14. });

8、增加视图函数                     
   vi  hello_extjs/views.py
       
点击(此处)折叠或打开
  1. from django.shortcuts import render_to_response

  2. def index(request):
  3.         return render_to_response('hello_extjs/index.html')

三、运行
 python manage.py runserver 192.168.1.109:8910
 
浏览器    .     

              

  参考文章     
      
     
阅读(768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~