Loading

上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 45 下一页
摘要: 在 django 中不像PHP那样有根目录的概念 而取而代之的是包的概念, 通过URLS.PY 来提供每个URL 对应的DJANGO的 函数来显示页面在包的 temolates目录中 的html页面里面 是不能直接写图片 或者 CSS JS 文件的 相对|绝对 路径的 , 而是用 URLS 提供的URL对应 图片/js/css 目录的调用方法如下:(r'^medias/(?P.*)$', 'django.views.static.serve', {'document_root': '/web/www/python/templates/im 阅读全文
posted @ 2013-12-04 12:27 青岛欧姆网络科技 阅读(2807) 评论(0) 推荐(0)
摘要: 以MySql数据库为例,先到http://dev.mysql.com/downloads/connector/python/处下载MysqlConnector for python的连接器。from django.shortcuts import render_to_responseimport mysql.connectordef book_list(request):db = mysql.connector.connect(user='me', db='mydb', passwd='secret', host='localhost&# 阅读全文
posted @ 2013-11-21 15:56 青岛欧姆网络科技 阅读(191) 评论(0) 推荐(0)
摘要: 标签下面的部分概述了常见的Django标签。if/else{%if%} 标签 对一个变量值进行测试,如果结果为true,系统将会显示在{%if%} 和 {%endif%}之间的一切,看个例子:{% if today_is_weekend %} Welcome to the weekend!{% endif %} An {% else %} tag is optional:{% if today_is_weekend %} Welcome to the weekend!{% else %} Get back to work.{% endif %}{%if%} 标签接受 a... 阅读全文
posted @ 2013-11-19 09:53 青岛欧姆网络科技 阅读(452) 评论(0) 推荐(0)
摘要: 接下来,我们开始学习如何使用模板系统,但我们并不和前面说的View相结合,我们的这里的目的是展示模板系统是如何独立于Django框架运行的。下面是在pyhon代码中使用Django模板系统的基础例子:1.通过提供原生文本代码创建Template模板对象2.调用Template对象的render()方法并传入所给的参数(上下文文本)。>>> from django import template>>> t = template.Template('My name is {{ name }}.')>>> c = template 阅读全文
posted @ 2013-11-18 10:47 青岛欧姆网络科技 阅读(225) 评论(0) 推荐(0)
摘要: 模板系统不是django特有的,它是python的一个库,你可以在任何地方使用它。使用方法:1.使用 Template()方法创建Template对象。2.调用Template对象的render()方法。>>> from django import template>>> t = template.Template('My name is {{ name }}.')>>> c = template.Context({'name': 'Adrian'})>>> print t. 阅读全文
posted @ 2013-11-15 16:31 青岛欧姆网络科技 阅读(282) 评论(0) 推荐(0)
摘要: 引入模板系统的原因,view中引入硬编码并非明智的选择,设计上的任何改变都会需要改动代码。python代码和HTML代码应该分开,这是多数Web站点的共识,分开会提高效率。基本模板系统Django模板是一串用来分离数据与文档模型的文本。参考下面的模板:Ordering noticeOrdering noticeDear {{ person_name }},Thanks for placing an order from {{ company }}. It's scheduled toship on {{ ship_date|date:"F j, Y" }}.Here 阅读全文
posted @ 2013-11-15 15:54 青岛欧姆网络科技 阅读(230) 评论(0) 推荐(0)
摘要: 前面的例子中,虽然时间是动态可变的,但它的URL却是静态的(/time/).很多时候,URL也是需要动态改变,然后展示出不通的内容来。现在我们就来创建一个可以动态改变URL的例子。如果URLconf 是下面这样:urlpatterns = patterns('',('^time/$', current_datetime),('^time/plus/1/$', one_hour_ahead),('^time/plus/2/$', two_hours_ahead),('^time/plus/3/$', three_ho 阅读全文
posted @ 2013-11-15 14:50 青岛欧姆网络科技 阅读(251) 评论(0) 推荐(0)
摘要: 前面的例子体现了一个设计模式中的重要思想,松耦合。不论我们是将/time/改成/current_time/,还是新建一个/another-time-page/同样指向views.py中的current_datetime函数,都只需要改动urls.py中的url样式,并不需要改动views.py中的代码。这样的做的好处就是降低了程序间的耦合度,修改起来更加方便快捷。 阅读全文
posted @ 2013-11-15 14:23 青岛欧姆网络科技 阅读(249) 评论(0) 推荐(0)
摘要: “Hello World”只是简单的展现了Django 基本的工作机制,但它不是动态的网页。第二个View我们将创建一个动态的网页,该页面上将展现当前的时间和日期。该View需要做两件事,第一,计算当前的日期和时间,第二,返回一个HttpResponse包含计算的结果。代码如下:>>> import datetime>>> now = datetime.datetime.now()>>> nowdatetime.datetime(2008, 12, 13, 14, 9, 39, 2731)>>> print now2008 阅读全文
posted @ 2013-11-15 14:17 青岛欧姆网络科技 阅读(257) 评论(0) 推荐(0)
摘要: 1.如何找到django在Ubuntu下的安装路径: 进入python命令行,import django,print(django.__path__)2.使用django-admin.py 创建项目 django-admin.py startproject 'projectname'3.启动项目服务 进入项目文件目录 python manage.py runserver4.Hello,World. 在创建的项目文件夹下创建一个新文件,命名为Hello.py,里边编写代码如下: from django.http import HttpResponsedef hello(reque 阅读全文
posted @ 2013-11-15 11:42 青岛欧姆网络科技 阅读(222) 评论(0) 推荐(0)
上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 45 下一页