- 论坛徽章:
- 0
|
转:wang_yb
Django使用心得
本篇主要内容:
django中引用javascript
django中引用css及图片资源
1. django中引用javascript
web开发中必然要引用一些javascript的函数库来进行一些前端的处理,django也不例外。
下面主要介绍如何在django中引用当前比较流行的js库JQuery。
首先,新建一个django工程siteWithResources,新建的方法参照Django使用心得(一)
然后分别配置以下几个文件:
1.1 urls.py
view sourceprint?- 1 urlpatterns = patterns('',
- 2 # Example:
- 3 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 4
- 5 ( r'^js/(?P<path>.*)1.2 views.py
- view sourceprint?[code]1 from django.shortcuts import render_to_response
- 2
- 3 def PageWithJquery( request ):
- 4 return render_to_response( 'default.html',
- 5 {"mytitle":"customize_title"})
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 , 'django.views.static.serve',
6 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
7 ),
8 )
[/code]1.2 views.py
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?[ DISCUZ_CODE_2 ]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'siteWithResources.views.PageWithJquery'),
05
06 ( r'^js/(?P<path>.*)当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
6 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
7 ),
8 )
[/code]1.2 views.py
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?[ DISCUZ_CODE_2 ]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
07 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
08 ),
09
10 ( r'^css/(?P<path>.*)当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
6 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
7 ),
8 )
[/code]1.2 views.py
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?[ DISCUZ_CODE_2 ]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
11 { 'document_root': 'D:/Vim/python/django/django-resources/css/' }
12 ),
13
14 ( r'^images/(?P<path>.*)当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
6 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
7 ),
8 )
[/code]1.2 views.py
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?[ DISCUZ_CODE_2 ]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
15 { 'document_root': 'D:/Vim/python/django/django-resources/images/' }
16 ),
17 )
完整版的default.html如下:
view sourceprint?01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html>
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
05 <title>{{ mytitle }}</title>
06 <link rel="stylesheet" type="text/css" href="/css/base.css" />
07 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
08 <script language="javascript" type="text/javascript">
09 $(document).ready(function(){
10 $('#btn_down').bind( 'click', move_txt_down );
11 $('#btn_up').bind( 'click', move_txt_up );
12 $('#btn_fadeIn').bind( 'click', fade_img_in );
13 $('#btn_fadeOut').bind( 'click', fade_img_out );
14 });
15
16 function move_txt_down(){
17 $('#txt').animate({left:100,top:500 }, 'slow');
18 }
19 function move_txt_up(){
20 $('#txt').animate({left:100,top:150 }, 'slow');
21 }
22 function fade_img_in(){
23 $('#logo1').fadeIn('slow');
24 }
25 function fade_img_out(){
26 $('#logo1').fadeOut('slow');
27 }
28 </script>
29 </head>
30 <body>
31 <h1>My Resource Test</h1>
32 <input type="button" name="btn" id="btn_down" value="Move the text down"/>
33 <input type="button" name="btn" id="btn_up" value="Move the text up"/>
34 <input type="button" name="btn" id="btn_fadeIn" value="Fade the logo in"/>
35 <input type="button" name="btn" id="btn_fadeOut" value="Fade the logo out"/>
36 <br />
37 <img src="/images1/Logo1.gif" alt="logo1" id="logo1" />
38 <div id="txt" style="position: absolute;left:100px;top:150px;">this is the text for move</div>
39 </body>
40 </html> [/code]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ], 'django.views.static.serve',
6 { 'document_root': 'D:/Vim/python/django/django-resources/js/' }
7 ),
8 )
[/code]1.2 views.py
view sourceprint?- 01 urlpatterns = patterns('',
- 02 # Example:
- 03 # (r'^siteWithResources/', include('siteWithResources.foo.urls')),
- 04 (r'^test/当然,还得在settings.py中加上模板所在的配置。
- view sourceprint?[code]1 TEMPLATE_DIRS = (
- 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- 3 # Always use forward slashes, even on Windows.
- 4 # Don't forget to use absolute paths, not relative paths.
- 5 "D:/Vim/python/django/django-templates/siteWithResources",
- 6 )
- 最后,整个工程的目录结构如下:
- view sourceprint?01 D:\Vim\python\django
- 02 |- siteWithResources
- 03 | |- __init__.py
- 04 | |- manage.py
- 05 | |- settings.py
- 06 | |- urls.py
- 07 | `- views.py
- 08 |
- 09 |- django-resources
- 10 | |- css
- 11 | | `- base.css
- 12 | |
- 13 | |- images1
- 14 | | |- Sunset.jpg
- 15 | | `- Logo1.gif
- 16 | |
- 17 | `- js
- 18 | `- jquery
- 19 | `- jquery-1.4.4.min.js
- 20 |
- 21 `- django-templates
- 22 `- siteWithResources
- 23 `- default.html
复制代码 1.3 default.html (引用javascript)
view sourceprint?1 <script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
然后就可以在default.html中使用jquery的各种函数了。
2. django中引用css及图片资源
引用css和图片资源的方法也和引用js是一样的,在urls.py中加入如下内容:
view sourceprint?[ DISCUZ_CODE_2 ]当然,还得在settings.py中加上模板所在的配置。
view sourceprint?[ DISCUZ_CODE_3 ] |
|