免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2905 | 回复: 2
打印 上一主题 下一主题

Django使用心得 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-22 11:18 |只看该作者 |倒序浏览
转: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. 1 urlpatterns = patterns('',  

  2. 2     # Example:  

  3. 3     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 4   

  5. 5     ( r'^js/(?P<path>.*)1.2 views.py
  6. view sourceprint?[code]1 from django.shortcuts import render_to_response  

  7. 2   

  8. 3 def PageWithJquery( request ):  

  9. 4     return render_to_response( 'default.html',  

  10. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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?
  1. 01 urlpatterns = patterns('',  

  2. 02     # Example:  

  3. 03     # (r'^siteWithResources/', include('siteWithResources.foo.urls')),  

  4. 04     (r'^test/当然,还得在settings.py中加上模板所在的配置。

  5. view sourceprint?[code]1 TEMPLATE_DIRS = (  

  6. 2     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  

  7. 3     # Always use forward slashes, even on Windows.  

  8. 4     # Don't forget to use absolute paths, not relative paths.  

  9. 5     "D:/Vim/python/django/django-templates/siteWithResources",  

  10. 6 )

  11. 最后,整个工程的目录结构如下:

  12. view sourceprint?01 D:\Vim\python\django  

  13. 02      |- siteWithResources  

  14. 03      |         |- __init__.py  

  15. 04      |         |- manage.py  

  16. 05      |         |- settings.py  

  17. 06      |         |- urls.py  

  18. 07      |         `- views.py  

  19. 08      |  

  20. 09      |- django-resources  

  21. 10      |         |- css  

  22. 11      |         |   `- base.css  

  23. 12      |         |  

  24. 13      |         |- images1  

  25. 14      |         |   |- Sunset.jpg  

  26. 15      |         |   `- Logo1.gif  

  27. 16      |         |  

  28. 17      |         `- js  

  29. 18      |             `- jquery  

  30. 19      |                  `- jquery-1.4.4.min.js  

  31. 20      |  

  32. 21      `- django-templates  

  33. 22                `- siteWithResources  

  34. 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        ]

论坛徽章:
0
2 [报告]
发表于 2011-04-24 13:31 |只看该作者
拷贝过来的请注意下格式 :)

要不看起来会让人崩溃。

论坛徽章:
0
3 [报告]
发表于 2011-04-25 08:49 |只看该作者
mark
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP