Django 讲解



复制代码

step 2

  View Code

step3

  View Code

step4

  View Code

伙计们,不知不觉我们自己已经写出一个web框架啦!

二 MVC和MTV模式

著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层;他们之间以一种插件似的,松耦合的方式连接在一起。

模型负责业务对象与数据库的对象(ORM),视图负责与用户的交互(页面),控制器(C)接受用户的输入调用模型和视图完成用户的请求。

                   

Django的MTV模式本质上与MVC模式没有什么差别,也是各组件之间为了保持松耦合关系,只是定义上有些许不同,Django的MTV分别代表:

       Model(模型):负责业务对象与数据库的对象(ORM)

       Template(模版):负责如何把页面展示给用户

       View(视图):负责业务逻辑,并在适当的时候调用Model和Template

       此外,Django还有一个url分发器,它的作用是将一个个URL的页面请求分发给不同的view处理,view再调用相应的Model和Template

 三 django的流程和命令行工具

django实现流程

  View Code

django的命令行工具

django-admin.py 是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每一个Django Project里都会有一个mannage.py。

<1> 创建一个django工程 : django-admin.py startproject mysite

        当前目录下会生成mysite的工程,目录结构如下:

        

  • manage.py ----- Django项目里面的工具,通过它可以调用django shell和数据库等。
  • settings.py ---- 包含了项目的默认设置,包括数据库信息,调试标志以及其他一些工作的变量。
  • urls.py ----- 负责把URL模式映射到应用程序。

<2>在mysite目录下创建blog应用: python manage.py startapp blog

        

<3>启动django项目:python manage.py runserver 8080

       这样我们的django就启动起来了!当我们访问:http://127.0.0.1:8080/时就可以看到:

       

<4>生成同步数据库的脚本:python manage.py makemigrations  

                     同步数据库:  python manage.py migrate   

       注意:在开发过程中,数据库同步误操作之后,难免会遇到后面不能同步成功的情况,解决这个问题的一个简单粗暴方法是把migrations目录下

                的脚本(除__init__.py之外)全部删掉,再把数据库删掉之后创建一个新的数据库,数据库同步操作再重新做一遍。            

<5>当我们访问http://127.0.0.1:8080/admin/时,会出现:

       

       所以我们需要为进入这个项目的后台创建超级管理员:python manage.py createsuperuser设置好用户名和密码后便可登录啦!

<6>清空数据库:python manage.py  flush

<7>查询某个命令的详细信息: django-admin.py  help  startapp

       admin 是Django 自带的一个后台数据库管理系统。

<8>启动交互界面 :python manage.py  shell

     这个命令和直接运行 python 进入 shell 的区别是:你可以在这个 shell 里面调用当前项目的 models.py 中的 API,对于操作数据,还有一些小测试非常方便。

<9> 终端上输入python manage.py 可以看到详细的列表,在忘记子名称的时候特别有用

实例练习1-提交数据并展示

  View Code

实例练习2-提交数据并展示(数据库)

  View Code

四 Django的配置文件(settings)

静态文件设置:

  View Code

其它重要参数设置:

APPEND_SLASH
       Default: True
       When set to True, if the request URL does not match any of the patterns in the URLconf and it 
doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended. Note
that the redirect may cause any data submitted in a POST request to be lost.

五 Django URL (路由系统)

     URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表;你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那个URL调用那段代码。

1
2
3
urlpatterns = [
     url(正则表达式, views视图函数,参数,别名),
]

参数说明:

  • 一个正则表达式字符串
  • 一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
  • 可选的要传递给视图函数的默认参数(字典形式)
  • 一个可选的name参数

5.1 Here’s a sample URLconf:

复制代码
from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [

    url(r'^articles/2003/$', views.special_case_2003),

    #url(r'^articles/[0-9]{4}/$', views.year_archive),

    url(r'^articles/([0-9]{4})/$', views.year_archive),  #no_named group

    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),

    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),

]
复制代码

Note:

  View Code

5.2 Named groups

      The above example used simple, non-named regular-expression groups (via parenthesis) to capture bits of the URL and pass them as positional arguments to a view. In more advanced usage, it’s possible to use named regular-expression groups to capture URL bits and pass them as keyword arguments to a view.

       In Python regular expressions, the syntax for named regular-expression groups is (?Ppattern), where name is the name of the group and pattern is some pattern to match.

Here’s the above example URLconf, rewritten to use named groups:

  ready
复制代码
from django.conf.urls import url
  
from . import views
  
urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/(?P[0-9]{4})/$', views.year_archive),
    url(r'^articles/(?P[0-9]{4})/(?P[0-9]{2})/$', views.month_archive),
    url(r'^articles/(?P[0-9]{4})/(?P[0-9]{2})/(?P[0-9]{2})/$', views.article_detail),
]
复制代码

       This accomplishes exactly the same thing as the previous example, with one subtle difference: The captured values are passed to view functions as keyword arguments rather than positional arguments.

5.3  Passing extra options to view functions

       URLconfs have a hook that lets you pass extra arguments to your view functions, as a Python dictionary.

The django.conf.urls.url() function can take an optional third argument which should be a dictionary of extra keyword arguments to pass to the view function.

For example:

from django.conf.urls import url
from . import views
  
urlpatterns = [
    url(r'^blog/(?P[0-9]{4})/$', views.year_archive, {
            'foo': 'bar'}),
]

       In this example, for a request to /blog/2005/, Django will call views.year_archive(request, year='2005',foo='bar').

This technique is used in the syndication framework to pass metadata and options to views.

Dealing with conflicts

       It’s possible to have a URL pattern which captures named keyword arguments, and also passes arguments with the same names in its dictionary of extra arguments. When this happens, the arguments in the dictionary will be used instead of the arguments captured in the URL.

5.4 name param

  View Code

5.5 Including other URLconfs

复制代码
#At any point, your urlpatterns can “include” other URLconf modules. This
#essentially “roots” a set of URLs below other ones.

#For example, here’s an excerpt of the URLconf for the Django website itself.
#It includes a number of other URLconfs:


from django.conf.urls import include, url

urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^blog/', include('blog.urls')),
]
复制代码

六 Django Views(视图函数)

http请求中产生两个核心对象:

        http请求:HttpRequest对象

        http响应:HttpResponse对象

所在位置:django.http

之前我们用到的参数request就是HttpRequest    检测方法:isinstance(request,HttpRequest)

1 HttpRequest对象的属性和方法:

  View Code

注意一个常用方法:request.POST.getlist('')

2 HttpResponse对象:

  对于HttpRequest对象来说,是由django自动创建的,但是,HttpResponse对象就必须我们自己创建。每个view请求处理方法必须返回一个HttpResponse对象。

  HttpResponse类在django.http.HttpResponse

  在HttpResponse对象上扩展的常用方法:

1
2
3
页面渲染:         render()(推荐)
                 render_to_response(),
页面跳转:         redirect( "路径" )
locals():    可以直接将函数中所有的变量传给模板

补充:

  View Code 

七 Template基础 

模板系统的介绍

你可能已经注意到我们在例子视图中返回文本的方式有点特别。 也就是说,HTML被直接硬编码在 Python代码之中。

def current_datetime(request):
    now = datetime.datetime.now()
    html = "It is now %s." % now
    return HttpResponse(html)

尽管这种技术便于解释视图是如何工作的,但直接将HTML硬编码到你的视图里却并不是一个好主意。 让我们来看一下为什么:

  • 对页面设计进行的任何改变都必须对 Python 代码进行相应的修改。 站点设计的修改往往比底层 Python 代码的修改要频繁得多,因此如果可以在不进行 Python 代码修改的情况下变更设计,那将会方便得多。

  • Python 代码编写和 HTML 设计是两项不同的工作,大多数专业的网站开发环境都将他们分配给不同的人员(甚至不同部门)来完成。 设计者和HTML/CSS的编码人员不应该被要求去编辑Python的代码来完成他们的工作。

  • 程序员编写 Python代码和设计人员制作模板两项工作同时进行的效率是最高的,远胜于让一个人等待另一个人完成对某个既包含 Python又包含 HTML 的文件的编辑工作。

基于这些原因,将页面的设计和Python的代码分离开会更干净简洁更容易维护。 我们可以使用 Django的 模板系统 (Template System)来实现这种模式,这就是本章要具体讨论的问题。

------------------------------------------------------------------模板语法------------------------------------------------------------------

一模版的组成

组成:HTML代码+逻辑控制代码

二 逻辑控制代码的组成

1  变量(使用双大括号来引用变量):

1
语法格式:       { {var_name}}

------Template和Context对象

  View Code

 Django 模板解析非常快捷。 大部分的解析工作都是在后台通过对简短正则表达式一次性调用来完成。 这和基于 XML 的模板引擎形成鲜明对比,那些引擎承担了 XML 解析器的开销,且往往比 Django 模板渲染引擎要慢上几个数量级。

  推荐方式

------深度变量的查找(万能的句点号)

在到目前为止的例子中,我们通过 context 传递的简单参数值主要是字符串,然而,模板系统能够非常简洁地处理更加复杂的数据结构,例如list、dictionary和自定义的对象。

在 Django 模板中遍历复杂数据结构的关键是句点字符 (.)。

  View Code

------变量的过滤器(filter)的使用

1
语法格式:      { {obj|filter:param}}
  View Code

2 标签(tag)的使用(使用大括号和百分比的组合来表示使用tag)

1
{% tags %}

------{% if %} 的使用 

{% if %}标签计算一个变量值,如果是“true”,即它存在、不为空并且不是false的boolean值,系统则会显示{% if %}和{% endif %}间的所有内容

  View Code

------{% for %}的使用

{% for %}标签允许你按顺序遍历一个序列中的各个元素,每次循环模板系统都会渲染{% for %}和{% endfor %}之间的所有内容

  View Code

------{%csrf_token%}:csrf_token标签

     用于生成csrf_token的标签,用于防治跨站攻击验证。注意如果你在view的index里用的是render_to_response方法,不会生效

     其实,这里是会生成一个input标签,和其他表单标签一起提交给后台的。

------{% url %}:  引用路由配置的地址

  View Code

------{% with %}:用更简单的变量名替代复杂的变量名

1
{% with total=fhjsaldfhjsdfhlasdfhljsdal %} { { total }} {% endwith %}

------{% verbatim %}: 禁止render

1
2
3
{% verbatim %}
          { { hello }}
{% endverbatim %}

------{% load %}: 加载标签库 

3 自定义filter和simple_tag

------a、在app中创建templatetags模块(必须的)

------b、创建任意 .py 文件,如:my_tags.py

  View Code

------c、在使用自定义simple_tag和filter的html文件中导入之前创建的 my_tags.py :{% load my_tags %}

------d、使用simple_tag和filter(如何调用)

  View Code

------e、在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag.

注意:

filter可以用在if等语句后,simple_tag不可以

  View Code

extend模板继承

------include 模板标签

在讲解了模板加载机制之后,我们再介绍一个利用该机制的内建模板标签: {% include %} 。该标签允许在(模板中)包含其它的模板的内容。 标签的参数是所要包含的模板名称,可以是一个变量,也可以是用单/双引号硬编码的字符串。 每当在多个模板中出现相同的代码时,就应该考虑是否要使用 {% include %} 来减少重复。

------extend(继承)模板标签

到目前为止,我们的模板范例都只是些零星的 HTML 片段,但在实际应用中,你将用 Django 模板系统来创建整个 HTML 页面。 这就带来一个常见的 Web 开发问题: 在整个网站中,如何减少共用页面区域(比如站点导航)所引起的重复和冗余代码?

解决该问题的传统做法是使用 服务器端的 includes ,你可以在 HTML 页面中使用该指令将一个网页嵌入到另一个中。 事实上, Django 通过刚才讲述的 {% include %} 支持了这种方法。 但是用 Django 解决此类问题的首选方法是使用更加优雅的策略—— 模板继承 。

本质上来说,模板继承就是先构造一个基础框架模板,而后在其子模板中对它所包含站点公用部分和定义块进行重载。

让我们通过修改 current_datetime.html 文件,为 current_datetime 创建一个更加完整的模板来体会一下这种做法:

  View Code

这看起来很棒,但如果我们要为 hours_ahead 视图创建另一个模板会发生什么事情呢?

  View Code

很明显,我们刚才重复了大量的 HTML 代码。 想象一下,如果有一个更典型的网站,它有导航条、样式表,可能还有一些 JavaScript 代码,事情必将以向每个模板填充各种冗余的 HTML 而告终。

解决这个问题的服务器端 include 方案是找出两个模板中的共同部分,将其保存为不同的模板片段,然后在每个模板中进行 include。 也许你会把模板头部的一些代码保存为 header.html 文件:

  View Code

你可能会把底部保存到文件 footer.html :

  View Code

对基于 include 的策略,头部和底部的包含很简单。 麻烦的是中间部分。 在此范例中,每个页面都有一个

My helpful timestamp site

 标题,但是这个标题不能放在 header.html 中,因为每个页面的 </tt> 是不同的。 如果我们将 <tt class="docutils literal" style="margin:0px;padding:0px;"><h1></tt> 包含在头部,我们就不得不包含 <tt class="docutils literal" style="margin:0px;padding:0px;"><title></tt> ,但这样又不允许在每个页面对它进行定制。 何去何从呢?</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Django 的模板继承系统解决了这些问题。 你可以将其视为服务器端 include 的逆向思维版本。 你可以对那些<span style="margin:0px;padding:0px;">不同</span> 的代码段进行定义,而不是 <span style="margin:0px;padding:0px;">共同</span> 代码段。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">第一步是定义 基础模板,该框架之后将由子模板所继承。 以下是我们目前所讲述范例的基础模板:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">这个叫做 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 的模板定义了一个简单的 HTML 框架文档,我们将在本站点的所有页面中使用。 子模板的作用就是重载、添加或保留那些块的内容。 (如果你一直按顺序学习到这里,保存这个文件到你的template目录下,命名为 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> .)</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">我们使用模板标签: <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 。 所有的 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签告诉模板引擎,子模板可以重载这些部分。 每个<tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt>标签所要做的是告诉模板引擎,该模板下的这一块内容将有可能被子模板覆盖。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">现在我们已经有了一个基本模板,我们可以修改 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime.html</tt> 模板来 使用它:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">再为 <tt class="docutils literal" style="margin:0px;padding:0px;">hours_ahead</tt> 视图创建一个模板,看起来是这样的:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">看起来很漂亮是不是? 每个模板只包含对自己而言 <span style="margin:0px;padding:0px;">独一无二</span> 的代码。 无需多余的部分。 如果想进行站点级的设计修改,仅需修改 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> ,所有其它模板会立即反映出所作修改。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <span style="margin:0px;padding:0px;color:rgb(0,0,128);"><span style="margin:0px;padding:0px;"> 以下是其工作方式:</span></span></p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      在加载 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime.html</tt> 模板时,模板引擎发现了 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">extends <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签, 注意到该模板是一个子模板。 模板引擎立即装载其父模板,即本例中的 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 。此时,模板引擎注意到 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 中的三个 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签,并用子模板的内容替换这些 block 。因此,引擎将会使用我们在 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{ <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">title <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt> 中定义的标题,对 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">content <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt> 也是如此。 所以,网页标题一块将由<tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">title <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt>替换,同样地,网页的内容一块将由 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">content <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt>替换。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     注意由于子模板并没有定义 <tt class="docutils literal" style="margin:0px;padding:0px;">footer</tt> 块,模板系统将使用在父模板中定义的值。 父模板 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签中的内容总是被当作一条退路。继承并不会影响到模板的上下文。 换句话说,任何处在继承树上的模板都可以访问到你传到模板中的每一个模板变量。你可以根据需要使用任意多的继承次数。 使用继承的一种常见方式是下面的三层法:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;line-height:1.5;"> <1> 创建 base.html 模板,在其中定义站点的主要外观感受。 这些都是不常修改甚至从不修改的部分。 <2> 为网站的每个区域创建 base_SECTION.html 模板(例如, base_photos.html 和 base_forum.html )。这些模板对base.html 进行拓展,<br style="margin:0px;padding:0px;"> 并包含区域特定的风格与设计。 <3> 为每种类型的页面创建独立的模板,例如论坛页面或者图片库。 这些模板拓展相应的区域模板。</span></pre> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      这个方法可最大限度地重用代码,并使得向公共区域(如区域级的导航)添加内容成为一件轻松的工作。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">以下是使用模板继承的一些诀窍:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h2 style="padding:8px 0px 8px 10px;font-size:18px;line-height:20px;background:rgb(0,104,139);color:#FFFFFF;font-family:Futura;height:20px;width:953.366px;text-align:center;margin-top:12px;margin-bottom:12px;">八 Models</h2> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">数据库的配置</h3> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">1    django默认支持sqlite,mysql, oracle,postgresql数据库。</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">    <span style="margin:0px;padding:0px;"> <1> sqlite</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            django默认使用sqlite的数据库,默认自带sqlite的数据库驱动 , 引擎名称:django.db.backends.sqlite3</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <span style="margin:0px;padding:0px;"><2> mysql</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            引擎名称:django.db.backends.mysql</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">2    mysql驱动程序</h4> <ul style="margin-bottom:0px;margin-left:30px;"> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   MySQLdb(mysql python)</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   mysqlclient</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   MySQL</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   PyMySQL(纯python的mysql驱动程序)</li> </ul> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">3     在django的项目中会默认使用sqlite数据库,在settings里有如下设置:</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/2280860010334f73ac25481db030ee7e.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">    </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">如果我们想要更改数据库,需要修改如下:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        <img src="http://img.e-com-net.com/image/info8/c4d97741a42149c6a560b9fc34f2d145.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">   </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><span style="margin:0px;padding:0px;">注意:</span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">ORM(对象关系映射)</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">用于实现面向对象编程语言里不同类型系统的数据之间的转换,换言之,就是用面向对象的方式去操作数据库的创建表以及增删改查等操作。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,102,0);">优点: 1 ORM使得我们的通用数据库交互变得简单易行,而且完全不用考虑该死的SQL语句。快速开发,由此而来。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          2 可以避免一些新手程序猿写sql语句带来的性能问题。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            比如 我们查询User表中的所有字段:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            <img src="http://img.e-com-net.com/image/info8/3406653de84a44c1aadb90816c261455.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            新手可能会用select * from  auth_user,这样会因为多了一个匹配动作而影响效率的。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> <span style="margin:0px;padding:0px;color:rgb(255,102,0);">缺点:1  性能有所牺牲,不过现在的各种ORM框架都在尝试各种方法,比如缓存,延迟加载登来减轻这个问题。效果很显著。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          2  对于个别复杂查询,ORM仍然力不从心,为了解决这个问题,ORM一般也支持写raw sql。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          3  通过QuerySet的query属性查询对应操作的sql语句</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">author_obj=models.Author.objects.filter(id=2)</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">print(author_obj.query)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     下面要开始学习Django ORM语法了,为了更好的理解,我们来做一个基本的 书籍/作者/出版商 数据库结构。 我们这样做是因为 这是一个众所周知的例子,很多SQL有关的书籍也常用这个举例。</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">表(模型)的创建:</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">实例:我们来假定下面这些概念,字段和关系</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">作者模型:一个作者有姓名。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">作者详细模型:把作者的详情放到详情表,包含性别,email地址和出生日期,作者详情模型和作者模型之间是一对一的关系(one-to-one)(类似于每个人和他的身份证之间的关系),在大多数情况下我们没有必要将他们拆分成两张表,这里只是引出一对一的概念。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">出版商模型:出版商有名称,地址,所在城市,省,国家和网站。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">书籍模型:书籍有书名和出版日期,一本书可能会有多个作者,一个作者也可以写多本书,所以作者和书籍的关系就是多对多的关联关系(many-to-many),一本书只应该由一个出版商出版,所以出版商和书籍是一对多关联关系(one-to-many),也被称作外键。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);">注意1:记得在settings里的INSTALLED_APPS中加入'app01',然后再同步数据库。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);">注意2: models.ForeignKey("Publish") & models.ForeignKey(Publish)</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">分析代码:</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <1>  每个数据模型都是django.db.models.Model的子类,它的父类Model包含了所有必要的和数据库交互的方法。并提供了一个简介漂亮的定义数据库字段的语法。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <2>  每个模型相当于单个数据库表(多对多关系例外,会多生成一张关系表),每个属性也是这个表中的字段。属性名就是字段名,它的类型(例如CharField)相当于数据库的字段类型(例如varchar)。大家可以留意下其它的类型都和数据库里的什么字段对应。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <3>  模型之间的三种关系:一对一,一对多,多对多。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             一对一:实质就是在主外键(author_id就是foreign key)的关系基础上,给外键加了一个UNIQUE=True的属性;</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             一对多:就是主外键关系;(foreign key)</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             多对多:(<span style="margin:0px;padding:0px;">ManyToManyField</span>) 自动创建第三张表(当然我们也可以自己创建第三张表:两个foreign key)</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <4>  <span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">模型常用的字段类型参数</span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <5>  <span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">Field重要参数</span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">表的操作(增删改查):</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;">-------------------------------------</span>增(create  ,  save) <span style="margin:0px;padding:0px;">-------------------------------</span></span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> app01.models <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span> * <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">create方式一: Author.objects.create(name='Alvin')</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">create方式二: Author.objects.create(**{"name":"alex"})</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">save方式一: author=Author(name="alvin")</span> <span style="margin:0px;padding:0px;line-height:1.5;"> author.save() </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">save方式二: author=Author()</span> author.name=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">alvin</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;"> author.save()</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">重点来了-------><span style="margin:0px;padding:0px;color:rgb(255,0,0);">那么如何创建存在一对多或多对多关系的一本书的信息呢?(如何处理外键关系的字段如一对多的<code class="python spaces" style="margin:0px;padding:0px;"></code><code class="python plain" style="margin:0px;padding:0px;">publisher和</code>多对多的authors)</span></span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,102,0);"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;">-----------------------------------------</span>删(delete) <span style="margin:0px;padding:0px;">---------------------------------------------</span></span></span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">>>> Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">).delete() (</span>3, { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">app01.Book_authors</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: 2, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">app01.Book</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: 1})</pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">我们表面上删除了一条信息,实际却删除了三条,因为我们删除的这本书在Book_authors表中有两条相关信息,这种删除方式就是django默认的级联删除。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;">如果是多对多的关系: remove()和clear()方法:</span></span> </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">正向</span> book = models.Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">) </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">删除第三张表中和女孩1关联的所有关联信息</span> book.author.clear() <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">清空与book中id=1 关联的所有数据</span> book.author.remove(2) <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">可以为id</span> book.author.remove(*[1,2,3,4]) <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">可以为列表,前面加*</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">反向</span> author = models.Author.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">) author.book_set.clear() </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">清空与boy中id=1 关联的所有数据</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;">-----------------------------------------</span>改(</span>update和save) <span style="margin:0px;padding:0px;">----------------------------------------</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">实例:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">    <img src="http://img.e-com-net.com/image/info8/058d31d08a474845a6acec3e2d9f7939.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><1> 第二种方式修改不能用get的原因是:update是QuerySet对象的方法,get返回的是一个model对象,它没有update方法,而filter返回的是一个QuerySet对象(filter里面的条件可能有多个条件符合,比如name='alvin',可能有两个name='alvin'的行数据)。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><2><span style="margin:0px;padding:0px;font-size:15px;"><tt class="docutils literal" style="margin:0px;padding:0px;">在“插入和更新数据”小节中,我们有提到模型的save()方法,这个方法会更新一行里的所有列。 而某些情况下,我们只需要更新行里的某几列。</tt></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      在这个例子里我们可以看到Django的save()方法更新了不仅仅是title列的值,还有更新了所有的列。 若title以外的列有可能会被其他的进程所改动的情况下,只更改title列显然是更加明智的。更改某一指定的列,我们可以调用结果集(QuerySet)对象的update()方法,与之等同的SQL语句变得更高效,并且不会引起竞态条件。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">此外,update()方法对于任何结果集(QuerySet)均有效,这意味着你可以同时更新多条记录update()方法会返回一个整型数值,表示受影响的记录条数。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意,这里因为update返回的是一个整形,所以没法用query属性;对于每次创建一个对象,想显示对应的raw sql,需要在settings加上日志记录部分:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">LOGGING</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;">注意:如果是多对多的改:</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"> obj=Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">)[0] author</span>=Author.objects.filter(id__gt=2<span style="margin:0px;padding:0px;line-height:1.5;">) obj.author.clear() obj.author.add(</span>*author)</pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><span style="margin:0px;padding:0px;">---------------------------------------查(filter,value等) <span style="margin:0px;padding:0px;">-------------------------------------</span></span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><span style="margin:0px;padding:0px;">---------->查询API:</span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">补充:</span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">extra</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->惰性机制:</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">所谓惰性机制:Publisher.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象),它并不会马上执行sql,而是当调用QuerySet的时候才执行。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">QuerySet特点:</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <1>  可迭代的</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <2>  可切片</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">QuerySet的高效使用:</span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->对象查询,单表条件查询,多表条件关联查询</span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意:条件查询即与对象查询对应,是指在filter,values等方法中的通过__来明确查询条件。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->聚合查询和分组查询</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><1> aggregate(*args,**kwargs):</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   通过对QuerySet进行计算,返回一个聚合值的字典。aggregate()中每一个参数都指定一个包含在字典中的返回值。即在查询集上生成聚合。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><2> annotate(*args,**kwargs):</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   可以通过计算查询结果中每一个对象所关联的对象集合,从而得出总计值(也可以是平均值或总和),即为查询集的每一项生成聚合。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       查询alex出的书总价格 <span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">                  </span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/c933e563464f43c582153c505d2281e6.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        查询各个作者出的书的总价格,这里就涉及到分组了,分组条件是authors__name</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/71b06489b22441debbae6ba4e9400f78.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">    </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">         查询各个出版社最便宜的书价是多少</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/606020075b20420d9dba28e8620111b3.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->F查询和Q查询</span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><span style="margin:0px;padding:0px;font-size:13px;color:rgb(51,51,153);">仅仅靠单一的关键字参数查询已经很难满足查询要求。此时Django为我们提供了F和Q查询:</span></span></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">raw sql</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">django中models的操作,也是调用了ORM框架来实现的,pymysql 或者mysqldb,所以我们也可以使用原生的SQL语句来操作数据库!</p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">九 admin的配置</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">admin是django强大功能之一,它能共从数据库中读取数据,呈现在页面中,进行管理。默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,它已经够用,但是有时候,一些特殊的功能还需要定制,比如搜索功能,下面这一系列文章就逐步深入介绍如何定制适合自己的admin应用。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">如果你觉得英文界面不好用,可以在setting.py 文件中修改以下选项</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">LANGUAGE_CODE </code> <code class="python keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">=</code>  <code class="python string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'en-us'</code>   <code class="python comments" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,130,0);">#LANGUAGE_CODE = 'zh-hans'</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">一  认识ModelAdmin</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   管理界面的定制类,如需扩展特定的model界面需从该类继承。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">二 注册medel类到admin的两种方式:</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <1>   使用register的方法</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">admin.site.register(Book,MyAdmin)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <2>   使用register的装饰器</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python decorator" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">@admin</code> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">.register(Book)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;">三 掌握一些常用的设置技巧</span></p> <ul style="margin-bottom:0px;margin-left:30px;"> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    list_display:     指定要显示的字段</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    search_fields:  指定搜索的字段</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    list_filter:        指定列表过滤器</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    ordering:       指定排序字段</li> </ul> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.contrib <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> admin </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> app01.models <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span> * <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> Register your models here.</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> @admin.register(Book)#----->单给某个表加一个定制</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">class</span><span style="margin:0px;padding:0px;line-height:1.5;"> MyAdmin(admin.ModelAdmin): list_display </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">) search_fields </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">) list_filter </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">,) ordering </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">,) fieldsets </span>=<span style="margin:0px;padding:0px;line-height:1.5;">[ (None, { </span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">fields</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">]}), (</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price information</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>, { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">fields</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>], <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">classes</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">collapse</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">]}), ] admin.site.register(Book,MyAdmin) admin.site.register(Publish) admin.site.register(Author)</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">参考文献:http://www.admin10000.com/document/2220.html</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:20px 0px 0px;padding:0px;"> <div style="margin:0px 0px 10px;padding:0px;"></div> <div style="margin:20px 0px 0px;padding:0px;font-size:9pt;color:#808080;"></div> <div style="margin:0px;padding:0px;"> <div style="margin:10px 0px;padding:10px 0px;border:1px dashed #C0C0C0;font-size:12px;width:350px;text-align:center;"> 好文要顶  关注我  收藏该文  <img src="http://img.e-com-net.com/image/info8/302c4368f33946ac81a6c9b3e9b2a4f6.jpg" alt="" style="margin-bottom:0px;margin-left:5px;padding:0px;border:none;vertical-align:middle;" width="0" height="0">  <img src="http://img.e-com-net.com/image/info8/fad0f75d7e64488aa6ab06af2e85daab.jpg" alt="" style="margin-bottom:0px;margin-left:5px;padding:0px;border:medium none;width:24px;height:24px;vertical-align:middle;" width="0" height="0"> </div> <div style="margin:0px 0px 10px;padding:0px;float:left;width:280px;color:rgb(0,0,0);font-size:12px;"> <div class="author_profile_info" style="margin:0px;padding:0px;float:left;line-height:18px;"> <a href="http://img.e-com-net.com/image/info8/94f0358926ff439da3929ef1b2ba8998.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/94f0358926ff439da3929ef1b2ba8998.jpg" class="author_avatar" alt="" style="margin-right:5px;margin-bottom:0px;padding:5px 0px 0px 2px;border:0px;vertical-align:top;float:left;" width="48" height="48"></a> <div class="author_profile_info" style="margin:0px;padding:0px;float:left;line-height:18px;"> Yuan先生 <br style="margin:0px;padding:0px;"> 关注 - 0 <br style="margin:0px;padding:0px;"> 粉丝 - 1159 </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:0px;padding:0px;"></div> <div style="margin:0px;padding:0px;"> +加关注 </div> </div> <div style="margin:10px 30px 10px 0px;padding:0px;float:right;font-size:12px;width:125px;text-align:center;"> <div class="diggit" style="margin:2px 0px 0px;padding:5px 0px 0px;float:left;width:46px;height:52px;background:url("//static.cnblogs.com/images/upup.gif") no-repeat;"> <span class="diggnum" style="margin:0px;padding:0px;font-size:14px;color:rgb(7,93,179);font-family:Verdana;line-height:1.5em;">10</span> </div> <div class="buryit" style="margin:2px 0px 0px 20px;padding:5px 0px 0px;float:right;width:46px;height:52px;background:url("//static.cnblogs.com/images/downdown.gif") no-repeat;"> <span class="burynum" style="margin:0px;padding:0px;font-size:14px;color:rgb(7,93,179);font-family:Verdana;line-height:1.5em;">1</span> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div class="diggword" style="margin:5px 0px 0px;padding:0px;color:#808080;"></div> </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:0px;padding:0px;line-height:1.8;font-size:12px;"> « 上一篇: 前端学习之jquery <br style="margin:0px;padding:0px;"> » 下一篇: Mysql与sql语句 <br style="margin:0px;padding:0px;"> </div> </div> </div> <div class="postDesc" style="margin:5px 0px 0px;padding:0px 5px 0px 0px;float:right;width:1368.95px;clear:both;text-align:right;color:rgb(102,102,102);"> posted @  <span style="margin:0px;padding:0px;">2016-11-20 20:36</span>  Yuan先生 阅读( <span style="margin:0px;padding:0px;">11158</span>) 评论( <span style="margin:0px;padding:0px;">9</span>)   编辑  收藏 </div> </div> </div> </div> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"></div> <br style="margin:0px;padding:0px;"> <div class="feedback_area_title" style="margin:20px 0px 10px;padding:0px 0px 0px 8px;font-weight:bold;border-bottom:1px solid rgb(51,51,51);"> 发表评论 </div> <div class="feedbackNoItems" style="margin:0px;padding:0px;"></div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #1楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-08-08 02:18</span> |  great_zhi    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 比alex的好多了 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(2) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #2楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-17 20:57</span> |  西雅图的邂逅    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 课讲的不错 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #3楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-19 09:26</span> |  define_null    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> @ great_zhi <br style="margin:0px;padding:0px;">敢黑我金角大王? </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #4楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-19 09:29</span> |  great_zhi    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> @ define_null <br style="margin:0px;padding:0px;">事实啊 他讲的太啰嗦了!我说的是事实!!! </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #5楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-11-09 10:47</span> |  深以为然    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 支持一下 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #6楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-11-10 03:30</span> |  其修远兮666    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 课讲的真不错,逻辑进度都挺好的,您讲的视频看过的都懂了。谢谢! </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #7楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-12-18 16:31</span> |  战争热诚    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 老师您好,我在学习您博客的第一部分,什么是web框架的时候,运行您博客的step1 <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1300.5px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;width:1300px;margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 4 </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 5 </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 6 </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 7 </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 8 </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 9 </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 10 </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 11 </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 12 </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 13 </div></td> <td class="code" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">from</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">wsgiref.simple_server import make_server</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">def application(environ, start_response):</code> </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">start_response(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'200 OK'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, [(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'Content-Type'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'text/html'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">)])</code> </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">return</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">[b</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'<h1>Hello, web!</h1>'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">]</code> </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd = make_server(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">''</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, 8080, application)</code> </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">print(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'Serving HTTP on port 8000...'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">)</code> </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp preprocessor" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#808080;"># 开始监听HTTP请求:</code> </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd.serve_forever()</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;">出现如下错误 <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;"> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1300.5px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;width:1300px;margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 4 </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 5 </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 6 </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 7 </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 8 </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 9 </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 10 </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 11 </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 12 </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 13 </div> <div class="line number14 index13 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 14 </div></td> <td class="code" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">Traceback (most recent call last):</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:/pycode/老男孩全栈工程师教育/server3.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 14, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);"><module></code> </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd = make_server(host=</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">''</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">,port=8888,app =application)</code> </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\wsgiref\simple_server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 153, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">make_server</code> </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server = server_class((host, port), handler_class)</code> </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\socketserver.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 453, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">__init__</code> </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">self.server_bind()</code> </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\wsgiref\simple_server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 50, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server_bind</code> </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">HTTPServer.server_bind(self)</code> </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\http\server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 139, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server_bind</code> </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">self.server_name = socket.getfqdn(host)</code> </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\socket.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 673, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">getfqdn</code> </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">hostname, aliases, ipaddrs = gethostbyaddr(name)</code> </div> <div class="line number14 index13 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">UnicodeDecodeError: </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'utf-8'</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">codec can't decode </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">byte</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">0xcd </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">position 0: invalid continuation </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">byte</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;">请问怎么解决 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #8楼   <span class="comment_date" style="margin:0px;padding:0px;">2018-01-08 16:06</span> |  Alan_chow    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 写的太好了,细致,实用。跳过了不少坑。 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #9楼   <span class="comment_date" style="margin:0px;padding:0px;">2018-01-15 16:43</span> |  雪竺轩    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 老师你博客的目录能开放吗 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(10) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div style="margin:0px;padding:0px;"></div> </div> <div class="commentform" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"></div> <div style="margin:0px;padding:0px;text-align:right;"> <span style="margin:0px;padding:0px;"></span> 刷新评论 刷新页面 返回顶部 </div> <div style="margin:0px;padding:0px;"> (评论功能已被禁用) </div> <div class="ad_text_commentbox" style="margin:5px 0px;padding:0px;"></div> <div style="margin:5px 0px 0px;padding:0px;line-height:1.8;"> 【推荐】超50万VC++源码: 大型组态工控、电力仿真CAD与GIS源码库! <br style="margin:0px;padding:0px;"> 【推荐】华为云7大明星产品0元免费使用 <br style="margin:0px;padding:0px;"> 【推荐】腾讯云如何降低移动开发成本 <br style="margin:0px;padding:0px;"> 【大赛】2018首届“顶天立地”AI开发者大赛 <br style="margin:0px;padding:0px;"> </div> <div style="margin:0px;padding:0px;line-height:1.8;"></div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <a href="http://img.e-com-net.com/image/info8/88f22ea4f28e49949857e4d1babdf381.jpg" target="_blank"><img width="600" height="500" src="http://img.e-com-net.com/image/info8/88f22ea4f28e49949857e4d1babdf381.jpg" alt="Django 讲解_第1张图片" style="margin-bottom:0px;padding:0px;border:0px;;border:1px solid black;"></a> </div> <div style="margin:0px;padding:0px;"> <div class="itnews c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <span style="margin:0px;padding:0px;">最新IT新闻</span>: <br style="margin:0px;padding:0px;">·  任正非签发内部文:警惕“华为中年危机”! <br style="margin:0px;padding:0px;">·  《我的世界》国区用户破亿 官方宣布手游PC版数据互通 <br style="margin:0px;padding:0px;">·  风雨飘摇中的联想:贸工技与技工贸之争的答案 <br style="margin:0px;padding:0px;">·  经纬十年 张颖给自己打了69分 <br style="margin:0px;padding:0px;">·  陆奇“卸任”为何引发中国互联网人集体感伤? <br style="margin:0px;padding:0px;">»  更多新闻... </div> </div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <img src="http://img.e-com-net.com/image/info8/32e71ddcb0a44959a25eea5b2588cd37.jpg" alt="" style="margin-bottom:0px;padding:0px;border:0px;width:468px;height:60px;" width="0" height="0"> </div> <div style="margin:0px;padding:0px;"> <div class="itnews c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <span style="margin:0px;padding:0px;">最新知识库文章</span>: <br style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> ·  你可以把编程当做一项托付终身的职业 <br style="margin:0px;padding:0px;">·  评审的艺术——谈谈现实中的代码评审 <br style="margin:0px;padding:0px;">·  如何高效学习 <br style="margin:0px;padding:0px;">·  如何成为优秀的程序员? <br style="margin:0px;padding:0px;">·  菜鸟工程师的超神之路 -- 从校园到职场 <br style="margin:0px;padding:0px;"> </div>»  更多知识库文章... </div> </div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"></div> </div> </div> </div> <div style="margin:0px;padding:16px 0px 0px 5px;width:230px;min-height:200px;float:left;color:rgb(136,134,111);"> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;width:229px;"></div> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="sidebar-block" style="margin:0px;padding:0px;"></div> </div> </div> </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;color:rgb(70,70,70);font-family:verdana, 'ms song', Arial, Helvetica, sans-serif;font-size:12px;background-color:rgb(250,247,239);"></div> <div> <div style="margin:0px;padding:0px;width:1608.95px;min-width:950px;text-align:left;color:rgb(70,70,70);font-family:verdana, 'ms song', Arial, Helvetica, sans-serif;font-size:12px;background-color:rgb(250,247,239);"> <div style="margin:0px 0px 0px -25em;padding:0px 0px 10px;min-height:200px;float:right;width:1608.95px;"> <div class="forFlow" style="margin:0px 10em;padding:0px;float:none;width:auto;"> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px 0px 10px;width:1368.95px;min-height:200px;"> <div class="post" style="margin:0px;padding:0px;"> <div class="postBody" style="margin:0px;padding:5px 2px 5px 5px;line-height:1.5;color:rgb(57,57,57);border-bottom:1px solid #000000;font-size:14px;text-align:justify;"> <div class="blogpost-body" style="margin:0px 0px 20px;padding:0px;"> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_hide" style="margin:0px;padding:0px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">step 2</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">step3</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">step4</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">伙计们,不知不觉我们自己已经写出一个web框架啦!</p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">二 MVC和MTV模式</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层;他们之间以一种插件似的,松耦合的方式连接在一起。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">模型负责业务对象与数据库的对象(ORM),视图负责与用户的交互(页面),控制器(C)接受用户的输入调用模型和视图完成用户的请求。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">                   <img src="http://img.e-com-net.com/image/info8/4e1c717570124e3b8743278ba6c06273.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Django的MTV模式本质上与MVC模式没有什么差别,也是各组件之间为了保持松耦合关系,只是定义上有些许不同,Django的MTV分别代表:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       Model(模型):负责业务对象与数据库的对象(ORM)</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       Template(模版):负责如何把页面展示给用户</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       View(视图):负责业务逻辑,并在适当的时候调用Model和Template</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       此外,Django还有一个url分发器,它的作用是将一个个URL的页面请求分发给不同的view处理,view再调用相应的Model和Template</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><img src="http://img.e-com-net.com/image/info8/0465dc8c2e814bbb8048717986aed507.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;"> 三 django的流程和命令行工具</h3> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">django</strong>实现流程</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">django的命令行工具</strong></h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">django-admin.py 是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每一个Django Project里都会有一个mannage.py。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><1> 创建一个django工程 : django-admin.py startproject mysite</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        当前目录下会生成mysite的工程,目录结构如下:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        <img src="http://img.e-com-net.com/image/info8/bb53210c798a434782bc6dad99c37085.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <ul style="margin-bottom:0px;margin-left:30px;"> <li class="p0" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">manage.py ----- Django项目里面的工具,通过它可以调用django shell和数据库等。</li> <li class="p0" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">settings.py ---- 包含了项目的默认设置,包括数据库信息,调试标志以及其他一些工作的变量。</li> <li class="p0" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">urls.py ----- 负责把URL模式映射到应用程序。</li> </ul> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><2>在mysite目录下创建blog应用: python manage.py startapp blog</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        <img src="http://img.e-com-net.com/image/info8/3f6fbffa859d4c308ecdf376a6c64042.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><3>启动django项目:python manage.py runserver 8080</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       这样我们的django就启动起来了!当我们访问:http://127.0.0.1:8080/时就可以看到:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/032a59aaa6e44434860caed98d45637e.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><4>生成同步数据库的脚本:python manage.py makemigrations  </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">                     </strong>同步数据库:  python manage.py migrate   </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <span style="margin:0px;padding:0px;color:rgb(0,0,128);">注意:在开发过程中,数据库同步误操作之后,难免会遇到后面不能同步成功的情况,解决这个问题的一个简单粗暴方法是把migrations目录下</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,0,0);"><span style="margin:0px;padding:0px;color:rgb(0,0,128);">                的脚本(除__init__.py之外)全部删掉,再把数据库删掉之后创建一个新的数据库,数据库同步操作再重新做一遍。  </span>          </span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><5>当我们访问http://127.0.0.1:8080/admin/时,会出现:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/fba5bd0a95bd4f00b79a28bacef4dbb0.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       所以我们需要为进入这个项目的后台创建超级管理员:python manage.py createsuperuser<span style="margin:0px;padding:0px;color:rgb(0,0,0);"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;font-size:18px;color:rgb(255,102,0);"><span style="margin:0px;padding:0px;color:rgb(0,0,0);font-size:15px;">,</span></span></strong><span style="margin:0px;padding:0px;font-size:18px;color:rgb(255,102,0);"><span style="margin:0px;padding:0px;color:rgb(0,0,0);font-size:15px;"><span style="margin:0px;padding:0px;font-size:14px;">设置好用户名和密码后便可登录啦!</span></span></span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><6>清空数据库:python manage.py  flush</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><7>查询某个命令的详细信息: django-admin.py  help  startapp</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       admin 是Django 自带的一个后台数据库管理系统。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><8>启动交互界面 :python manage.py  shell</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,102,0);font-size:18px;"><strong style="margin:0px;padding:0px;">    </strong><span style="margin:0px;padding:0px;font-size:14px;"> <span style="margin:0px;padding:0px;color:rgb(128,0,128);">这个命令和直接运行 python 进入 shell 的区别是:你可以在这个 shell 里面调用当前项目的 models.py 中的 API,对于操作数据,还有一些小测试非常方便。</span></span></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><9><strong style="margin:0px;padding:0px;"> </strong>终端上输入python manage.py 可以看到详细的列表,在忘记子名称的时候特别有用<strong style="margin:0px;padding:0px;">。</strong></p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">实例练习1-提交数据并展示</strong></h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">实例练习2-提交数据并展示(数据库)</strong></h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">四 Django的配置文件(settings)</h3> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">静态文件设置:</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">其它重要参数设置:</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;line-height:1.5;">APPEND_SLASH Default: True When set to True, </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> the request URL does <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">not</span> match any of the patterns <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">in</span> the URLconf <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">and</span> it <br style="margin:0px;padding:0px;"> doesn’t end <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">in</span> a slash, an HTTP redirect <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">is</span> issued to the same URL with a slash appended. Note <br style="margin:0px;padding:0px;"> that the redirect may cause any data submitted <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">in</span> a POST request to be lost.</pre> </div> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">五 Django URL (路由系统)</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表;你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那个URL调用那段代码。</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">urlpatterns = [</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">url(正则表达式, views视图函数,参数,别名),</code> </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">]</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">参数说明:</p> <ul style="margin-bottom:0px;margin-left:30px;"> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">一个正则表达式字符串</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">可选的要传递给视图函数的默认参数(字典形式)</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">一个可选的name参数</li> </ul> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">5.1 Here’s a sample URLconf:</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.conf.urls <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> url </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.contrib <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> admin </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> app01 <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> views urlpatterns </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> [ url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/2003/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.special_case_2003), </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">url(r'^articles/[0-9]{4}/$', views.year_archive),</span> <span style="margin:0px;padding:0px;line-height:1.5;"> url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/([0-9]{4})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>, views.year_archive), <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">no_named group</span> <span style="margin:0px;padding:0px;line-height:1.5;"> url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/([0-9]{4})/([0-9]{2})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.month_archive), url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.article_detail), ]</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><strong style="margin:0px;padding:0px;">Note:</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">5.2 Named groups</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      The above example used simple, <em style="margin:0px;padding:0px;">non-named</em> regular-expression groups (via parenthesis) to capture bits of the URL and pass them as <em style="margin:0px;padding:0px;">positional</em> arguments to a view. In more advanced usage, it’s possible to use <em style="margin:0px;padding:0px;">named</em> regular-expression groups to capture URL bits and pass them as <em style="margin:0px;padding:0px;">keyword</em> arguments to a view.</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       In Python regular expressions, the syntax for named regular-expression groups is <code class="docutils literal" style="margin:0px;padding:0px;">(?P<name>pattern)</code>, where <code class="docutils literal" style="margin:0px;padding:0px;">name</code> is the name of the group and <code class="docutils literal" style="margin:0px;padding:0px;">pattern</code> is some pattern to match.</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Here’s the above example URLconf, rewritten to use named groups:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">ready</span> </div> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.conf.urls <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> url </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> . <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> views urlpatterns </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> [ url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/2003/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.special_case_2003), url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/(?P<year>[0-9]{4})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.year_archive), url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.month_archive), url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, views.article_detail), ]</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       This accomplishes exactly the same thing as the previous example, with one subtle difference: The captured values are passed to view functions as keyword arguments rather than positional arguments.</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">5.3  Passing extra options to view functions</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       URLconfs have a hook that lets you pass extra arguments to your view functions, as a Python dictionary.</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">The <code class="xref py py-func docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">django.conf.urls.url()</span></code> function can take an optional third argument which should be a dictionary of extra keyword arguments to pass to the view function.</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">For example:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.conf.urls <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> url </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> . <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> views urlpatterns </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> [ url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^blog/(?P<year>[0-9]{4})/$</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>, views.year_archive, { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">foo</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">bar</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">}), ]</span></pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       In this example, for a request to <code class="docutils literal" style="margin:0px;padding:0px;">/blog/2005/</code>, Django will call <code class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">views.year_archive(request, <span class="pre" style="margin:0px;padding:0px;">year='2005',<span class="pre" style="margin:0px;padding:0px;">foo='bar')</span></span></span></code>.</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">This technique is used in the syndication framework to pass metadata and options to views.</p> <div class="admonition-dealing-with-conflicts admonition" style="margin:0px;padding:0px;"> <p class="first admonition-title" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Dealing with conflicts</p> <p class="last" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       It’s possible to have a URL pattern which captures named keyword arguments, and also passes arguments with the same names in its dictionary of extra arguments. When this happens, the arguments in the dictionary will be used instead of the arguments captured in the URL.</p> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">5.4 <span style="margin:0px;padding:0px;font-size:1em;line-height:1.5;">name param</span></h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">5.5 Including other URLconfs</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">At any point, your urlpatterns can “include” other URLconf modules. This</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> #</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">essentially “roots” a set of URLs below other ones.</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">For example, here’s an excerpt of the URLconf for the Django website itself.</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> #</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">It includes a number of other URLconfs:</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.conf.urls <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> include, url urlpatterns </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> [ url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^admin/</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">, admin.site.urls), url(r</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">^blog/</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>, include(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">blog.urls</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">)), ]</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">六 Django Views(视图函数)</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><img src="http://img.e-com-net.com/image/info8/8a68cb93eee446c693bfcffe34d1cc33.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">http请求中产生两个核心对象:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        http请求:HttpRequest对象</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        http响应:HttpResponse对象</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">所在位置:django.http</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">之前我们用到的参数request就是HttpRequest    检测方法:isinstance(request,HttpRequest)</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">1 HttpRequest对象的属性和方法:</h4> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">注意一个常用方法:<span style="margin:0px;padding:0px;color:rgb(0,128,128);">request.POST.getlist('')</span></h4> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">2 HttpResponse对象:</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">  对于HttpRequest对象来说,是由django自动创建的,但是,HttpResponse对象就必须我们自己创建。每个view请求处理方法必须返回一个HttpResponse对象。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">  HttpResponse类在django.http.HttpResponse</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">  在HttpResponse对象上扩展的常用方法:</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">页面渲染:         render()(推荐)<br>                 render_to_response(),</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">页面跳转:         redirect(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"路径"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">)</code> </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">locals():    可以直接将函数中所有的变量传给模板</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(51,51,153);">补充:</span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code <br style="margin:0px;padding:0px;"></span> </div> <h2 style="padding:8px 0px 8px 10px;font-size:18px;line-height:20px;background:rgb(0,104,139);color:#FFFFFF;font-family:Futura;height:20px;width:953.366px;text-align:center;margin-top:12px;margin-bottom:12px;">七 Template基础 </h2> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">模板系统的介绍</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">你可能已经注意到我们在例子视图中返回文本的方式有点特别。 也就是说,HTML被直接硬编码在 Python代码之中。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">def</span><span style="margin:0px;padding:0px;line-height:1.5;"> current_datetime(request): now </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> datetime.datetime.now() html </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;"><html><body>It is now %s.</body></html></span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> %<span style="margin:0px;padding:0px;line-height:1.5;"> now </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> HttpResponse(html)</pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">尽管这种技术便于解释视图是如何工作的,但直接将HTML硬编码到你的视图里却并不是一个好主意。 让我们来看一下为什么:</p> <ul class="simple" style="margin-bottom:0px;margin-left:30px;"> <li class="cn" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;"><p class="first cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">对页面设计进行的任何改变都必须对 Python 代码进行相应的修改。 站点设计的修改往往比底层 Python 代码的修改要频繁得多,因此如果可以在不进行 Python 代码修改的情况下变更设计,那将会方便得多。</p></li> </ul> <ul class="simple" style="margin-bottom:0px;margin-left:30px;"> <li class="cn" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;"><p class="first cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Python 代码编写和 HTML 设计是两项不同的工作,大多数专业的网站开发环境都将他们分配给不同的人员(甚至不同部门)来完成。 设计者和HTML/CSS的编码人员不应该被要求去编辑Python的代码来完成他们的工作。</p></li> </ul> <ul class="simple" style="margin-bottom:0px;margin-left:30px;"> <li class="cn" style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;"><p class="first cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">程序员编写 Python代码和设计人员制作模板两项工作同时进行的效率是最高的,远胜于让一个人等待另一个人完成对某个既包含 Python又包含 HTML 的文件的编辑工作。</p></li> </ul> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">基于这些原因,将页面的设计和Python的代码分离开会更干净简洁更容易维护。 我们可以使用 Django的 <em style="margin:0px;padding:0px;">模板系统</em> (Template System)来实现这种模式,这就是本章要具体讨论的问题。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">-<span style="margin:0px;padding:0px;color:rgb(128,0,128);">-----------------------------------------------------------------<span style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">模板语法-</strong></span>-----------------------------------------------------------------</span></p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">一模版的组成</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">组成:HTML代码+逻辑控制代码</strong></p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">二 逻辑控制代码的组成</h3> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">1  变量(使用双大括号来引用变量):</strong></h4> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">语法格式:       { {var_name}}</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p class="brush:csharp;gutter:true;" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;">------Template和Context对象</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> Django 模板解析非常快捷。 大部分的解析工作都是在后台通过对简短正则表达式一次性调用来完成。 这和基于 XML 的模板引擎形成鲜明对比,那些引擎承担了 XML 解析器的开销,且往往比 Django 模板渲染引擎要慢上几个数量级。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">推荐方式</span> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>深度变量的查找(万能的句点号)</strong></span></p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">在到目前为止的例子中,我们通过 context 传递的简单参数值主要是字符串,然而,模板系统能够非常简洁地处理更加复杂的数据结构,例如list、dictionary和自定义的对象。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">在 Django 模板中遍历复杂数据结构的关键是句点字符 (<tt class="docutils literal" style="margin:0px;padding:0px;">.</tt>)。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;font-size:14px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>变量的过滤器(filter)的使用</strong></span></p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">语法格式:      { {obj|filter:param}}</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">2 标签(tag)的使用(使用大括号和百分比的组合来表示使用tag)</h4> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">{% tags %}</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{% if %} 的使用</strong> </span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">{% if %}标签计算一个变量值,如果是“true”,即它存在、不为空并且不是false的boolean值,系统则会显示{% if %}和{% endif %}间的所有内容</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{% for %}的使用</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">{% for %}标签允许你按顺序遍历一个序列中的各个元素,每次循环模板系统都会渲染{% for %}和{% endfor %}之间的所有内容</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{%csrf_token%}:csrf_token标签</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     用于生成csrf_token的标签,用于防治跨站攻击验证。注意如果你在view的index里用的是render_to_response方法,不会生效</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     其实,这里是会生成一个input标签,和其他表单标签一起提交给后台的。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{% url %}:  引用路由配置的地址</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{% with %}:用更简单的变量名替代复杂的变量名</strong></span></p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">{% with total=fhjsaldfhjsdfhlasdfhljsdal %} { { total }} {% endwith %}</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>{% verbatim %}: 禁止render</strong></span></p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">{% verbatim %}</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">         </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">{ { hello }}</code> </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">{% endverbatim %}</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;">------</strong>{% load %}: 加载标签库</span> <br style="margin:0px;padding:0px;"></strong></p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">3<strong style="margin:0px;padding:0px;"> 自定义filter和<strong style="margin:0px;padding:0px;">simple_tag</strong></strong></h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>a、在app中创建templatetags模块(必须的)</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>b、创建任意 .py 文件,如:my_tags.py</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>c、在使用自定义simple_tag和filter的html文件中导入之前创建的 my_tags.py :{% load my_tags %}</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>d、使用simple_tag和filter(如何调用)</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>e、在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag.</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">注意:</span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">filter可以用在if等语句后,simple_tag不可以</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);"><strong style="margin:0px;padding:0px;">4 <strong style="margin:0px;padding:0px;">extend模板继承</strong></strong></h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;"><tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">------</strong>include</span></tt> </span>模板标签</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;font-size:14px;">在讲解了模板加载机制之后,我们再介绍一个利用该机制的内建模板标签: <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">include <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 。该标签允许在(模板中)包含其它的模板的内容。 标签的参数是所要包含的模板名称,可以是一个变量,也可以是用单/双引号硬编码的字符串。 每当在多个模板中出现相同的代码时,就应该考虑是否要使用 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">include <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 来减少重复。</span></p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;">------extend(继承)模板标签</strong></span></p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">到目前为止,我们的模板范例都只是些零星的 HTML 片段,但在实际应用中,你将用 Django 模板系统来创建整个 HTML 页面。 这就带来一个常见的 Web 开发问题: 在整个网站中,如何减少共用页面区域(比如站点导航)所引起的重复和冗余代码?</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">解决该问题的传统做法是使用 <em style="margin:0px;padding:0px;">服务器端的 includes</em> ,你可以在 HTML 页面中使用该指令将一个网页嵌入到另一个中。 事实上, Django 通过刚才讲述的 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">include <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 支持了这种方法。 但是用 Django 解决此类问题的首选方法是使用更加优雅的策略—— <em style="margin:0px;padding:0px;">模板继承</em> 。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">本质上来说,模板继承就是先构造一个基础框架模板,而后在其子模板中对它所包含站点公用部分和定义块进行重载。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">让我们通过修改 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime.html</tt> 文件,为 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime</tt> 创建一个更加完整的模板来体会一下这种做法:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">这看起来很棒,但如果我们要为 <tt class="docutils literal" style="margin:0px;padding:0px;">hours_ahead</tt> 视图创建另一个模板会发生什么事情呢?</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">很明显,我们刚才重复了大量的 HTML 代码。 想象一下,如果有一个更典型的网站,它有导航条、样式表,可能还有一些 JavaScript 代码,事情必将以向每个模板填充各种冗余的 HTML 而告终。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">解决这个问题的服务器端 include 方案是找出两个模板中的共同部分,将其保存为不同的模板片段,然后在每个模板中进行 include。 也许你会把模板头部的一些代码保存为 <tt class="docutils literal" style="margin:0px;padding:0px;">header.html</tt> 文件:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">你可能会把底部保存到文件 <tt class="docutils literal" style="margin:0px;padding:0px;">footer.html</tt> :</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">对基于 include 的策略,头部和底部的包含很简单。 麻烦的是中间部分。 在此范例中,每个页面都有一个<tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;"><h1>My <span class="pre" style="margin:0px;padding:0px;">helpful <span class="pre" style="margin:0px;padding:0px;">timestamp <span class="pre" style="margin:0px;padding:0px;">site</h1></span></span></span></span></tt> 标题,但是这个标题不能放在 <tt class="docutils literal" style="margin:0px;padding:0px;">header.html</tt> 中,因为每个页面的 <tt class="docutils literal" style="margin:0px;padding:0px;"><title></tt> 是不同的。 如果我们将 <tt class="docutils literal" style="margin:0px;padding:0px;"><h1></tt> 包含在头部,我们就不得不包含 <tt class="docutils literal" style="margin:0px;padding:0px;"><title></tt> ,但这样又不允许在每个页面对它进行定制。 何去何从呢?</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">Django 的模板继承系统解决了这些问题。 你可以将其视为服务器端 include 的逆向思维版本。 你可以对那些<em style="margin:0px;padding:0px;">不同</em> 的代码段进行定义,而不是 <em style="margin:0px;padding:0px;">共同</em> 代码段。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">第一步是定义 基础模板,该框架之后将由子模板所继承。 以下是我们目前所讲述范例的基础模板:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">这个叫做 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 的模板定义了一个简单的 HTML 框架文档,我们将在本站点的所有页面中使用。 子模板的作用就是重载、添加或保留那些块的内容。 (如果你一直按顺序学习到这里,保存这个文件到你的template目录下,命名为 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> .)</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">我们使用模板标签: <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 。 所有的 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签告诉模板引擎,子模板可以重载这些部分。 每个<tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt>标签所要做的是告诉模板引擎,该模板下的这一块内容将有可能被子模板覆盖。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">现在我们已经有了一个基本模板,我们可以修改 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime.html</tt> 模板来 使用它:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">再为 <tt class="docutils literal" style="margin:0px;padding:0px;">hours_ahead</tt> 视图创建一个模板,看起来是这样的:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">看起来很漂亮是不是? 每个模板只包含对自己而言 <em style="margin:0px;padding:0px;">独一无二</em> 的代码。 无需多余的部分。 如果想进行站点级的设计修改,仅需修改 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> ,所有其它模板会立即反映出所作修改。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <span style="margin:0px;padding:0px;color:rgb(0,0,128);"><strong style="margin:0px;padding:0px;"> 以下是其工作方式:</strong></span></p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      在加载 <tt class="docutils literal" style="margin:0px;padding:0px;">current_datetime.html</tt> 模板时,模板引擎发现了 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">extends <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签, 注意到该模板是一个子模板。 模板引擎立即装载其父模板,即本例中的 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 。此时,模板引擎注意到 <tt class="docutils literal" style="margin:0px;padding:0px;">base.html</tt> 中的三个 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签,并用子模板的内容替换这些 block 。因此,引擎将会使用我们在 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{ <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">title <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt> 中定义的标题,对 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">content <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt> 也是如此。 所以,网页标题一块将由<tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">title <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt>替换,同样地,网页的内容一块将由 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">content <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></span></tt>替换。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     注意由于子模板并没有定义 <tt class="docutils literal" style="margin:0px;padding:0px;">footer</tt> 块,模板系统将使用在父模板中定义的值。 父模板 <tt class="docutils literal" style="margin:0px;padding:0px;"><span class="pre" style="margin:0px;padding:0px;">{% <span class="pre" style="margin:0px;padding:0px;">block <span class="pre" style="margin:0px;padding:0px;">%}</span></span></span></tt> 标签中的内容总是被当作一条退路。继承并不会影响到模板的上下文。 换句话说,任何处在继承树上的模板都可以访问到你传到模板中的每一个模板变量。你可以根据需要使用任意多的继承次数。 使用继承的一种常见方式是下面的三层法:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;line-height:1.5;"> <1> 创建 base.html 模板,在其中定义站点的主要外观感受。 这些都是不常修改甚至从不修改的部分。 <2> 为网站的每个区域创建 base_SECTION.html 模板(例如, base_photos.html 和 base_forum.html )。这些模板对base.html 进行拓展,<br style="margin:0px;padding:0px;"> 并包含区域特定的风格与设计。 <3> 为每种类型的页面创建独立的模板,例如论坛页面或者图片库。 这些模板拓展相应的区域模板。</span></pre> </div> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      这个方法可最大限度地重用代码,并使得向公共区域(如区域级的导航)添加内容成为一件轻松的工作。</p> <p class="cn" style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">以下是使用模板继承的一些诀窍:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h2 style="padding:8px 0px 8px 10px;font-size:18px;line-height:20px;background:rgb(0,104,139);color:#FFFFFF;font-family:Futura;height:20px;width:953.366px;text-align:center;margin-top:12px;margin-bottom:12px;">八 Models</h2> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">数据库的配置</h3> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">1    django默认支持sqlite,mysql, oracle,postgresql数据库。</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">    <strong style="margin:0px;padding:0px;"> <1> sqlite</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            django默认使用sqlite的数据库,默认自带sqlite的数据库驱动 , 引擎名称:django.db.backends.sqlite3</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <strong style="margin:0px;padding:0px;"><2> mysql</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            引擎名称:django.db.backends.mysql</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">2    mysql驱动程序</h4> <ul style="margin-bottom:0px;margin-left:30px;"> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   MySQLdb(mysql python)</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   mysqlclient</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   MySQL</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">   PyMySQL(纯python的mysql驱动程序)</li> </ul> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">3     在django的项目中会默认使用sqlite数据库,在settings里有如下设置:</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/2280860010334f73ac25481db030ee7e.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">    </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">如果我们想要更改数据库,需要修改如下:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        <img src="http://img.e-com-net.com/image/info8/c4d97741a42149c6a560b9fc34f2d145.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">   </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><strong style="margin:0px;padding:0px;">注意:</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">ORM(对象关系映射)</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">用于实现面向对象编程语言里不同类型系统的数据之间的转换,换言之,就是用面向对象的方式去操作数据库的创建表以及增删改查等操作。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,102,0);">优点: 1 ORM使得我们的通用数据库交互变得简单易行,而且完全不用考虑该死的SQL语句。快速开发,由此而来。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          2 可以避免一些新手程序猿写sql语句带来的性能问题。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            比如 我们查询User表中的所有字段:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            <img src="http://img.e-com-net.com/image/info8/3406653de84a44c1aadb90816c261455.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">            新手可能会用select * from  auth_user,这样会因为多了一个匹配动作而影响效率的。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> <span style="margin:0px;padding:0px;color:rgb(255,102,0);">缺点:1  性能有所牺牲,不过现在的各种ORM框架都在尝试各种方法,比如缓存,延迟加载登来减轻这个问题。效果很显著。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          2  对于个别复杂查询,ORM仍然力不从心,为了解决这个问题,ORM一般也支持写raw sql。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">          3  通过QuerySet的query属性查询对应操作的sql语句</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">author_obj=models.Author.objects.filter(id=2)</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">print(author_obj.query)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     下面要开始学习Django ORM语法了,为了更好的理解,我们来做一个基本的 书籍/作者/出版商 数据库结构。 我们这样做是因为 这是一个众所周知的例子,很多SQL有关的书籍也常用这个举例。</p> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">表(模型)的创建:</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">实例:我们来假定下面这些概念,字段和关系</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">作者模型:一个作者有姓名。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">作者详细模型:把作者的详情放到详情表,包含性别,email地址和出生日期,作者详情模型和作者模型之间是一对一的关系(one-to-one)(类似于每个人和他的身份证之间的关系),在大多数情况下我们没有必要将他们拆分成两张表,这里只是引出一对一的概念。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">出版商模型:出版商有名称,地址,所在城市,省,国家和网站。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">书籍模型:书籍有书名和出版日期,一本书可能会有多个作者,一个作者也可以写多本书,所以作者和书籍的关系就是多对多的关联关系(many-to-many),一本书只应该由一个出版商出版,所以出版商和书籍是一对多关联关系(one-to-many),也被称作外键。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);">注意1:记得在settings里的INSTALLED_APPS中加入'app01',然后再同步数据库。</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,0,128);">注意2: models.ForeignKey("Publish") & models.ForeignKey(Publish)</span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">分析代码:</span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <1>  每个数据模型都是django.db.models.Model的子类,它的父类Model包含了所有必要的和数据库交互的方法。并提供了一个简介漂亮的定义数据库字段的语法。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <2>  每个模型相当于单个数据库表(多对多关系例外,会多生成一张关系表),每个属性也是这个表中的字段。属性名就是字段名,它的类型(例如CharField)相当于数据库的字段类型(例如varchar)。大家可以留意下其它的类型都和数据库里的什么字段对应。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <3>  模型之间的三种关系:一对一,一对多,多对多。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             一对一:实质就是在主外键(author_id就是foreign key)的关系基础上,给外键加了一个UNIQUE=True的属性;</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             一对多:就是主外键关系;(foreign key)</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">             多对多:(<span style="margin:0px;padding:0px;">ManyToManyField</span>) 自动创建第三张表(当然我们也可以自己创建第三张表:两个foreign key)</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <4>  <strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">模型常用的字段类型参数</span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <5>  <strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">Field重要参数</span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">表的操作(增删改查):</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">-------------------------------------</strong>增(create  ,  save) <strong style="margin:0px;padding:0px;">-------------------------------</strong></strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> app01.models <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span> * <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">create方式一: Author.objects.create(name='Alvin')</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">create方式二: Author.objects.create(**{"name":"alex"})</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">save方式一: author=Author(name="alvin")</span> <span style="margin:0px;padding:0px;line-height:1.5;"> author.save() </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">save方式二: author=Author()</span> author.name=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">alvin</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;"> author.save()</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">重点来了-------><span style="margin:0px;padding:0px;color:rgb(255,0,0);">那么如何创建存在一对多或多对多关系的一本书的信息呢?(如何处理外键关系的字段如一对多的<code class="python spaces" style="margin:0px;padding:0px;"></code><code class="python plain" style="margin:0px;padding:0px;">publisher和</code>多对多的authors)</span></span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(255,102,0);"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;">-----------------------------------------</strong>删(delete) <strong style="margin:0px;padding:0px;">---------------------------------------------</strong></span></strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">>>> Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">).delete() (</span>3, { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">app01.Book_authors</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: 2, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">app01.Book</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: 1})</pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">我们表面上删除了一条信息,实际却删除了三条,因为我们删除的这本书在Book_authors表中有两条相关信息,这种删除方式就是django默认的级联删除。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;">如果是多对多的关系: remove()和clear()方法:</strong></span> </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">正向</span> book = models.Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">) </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">删除第三张表中和女孩1关联的所有关联信息</span> book.author.clear() <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">清空与book中id=1 关联的所有数据</span> book.author.remove(2) <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">可以为id</span> book.author.remove(*[1,2,3,4]) <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">可以为列表,前面加*</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">反向</span> author = models.Author.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">) author.book_set.clear() </span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">清空与boy中id=1 关联的所有数据</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;"><strong style="margin:0px;padding:0px;">-----------------------------------------</strong>改(</strong>update和save) <strong style="margin:0px;padding:0px;">----------------------------------------</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">实例:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">    <img src="http://img.e-com-net.com/image/info8/058d31d08a474845a6acec3e2d9f7939.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意:</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><1> 第二种方式修改不能用get的原因是:update是QuerySet对象的方法,get返回的是一个model对象,它没有update方法,而filter返回的是一个QuerySet对象(filter里面的条件可能有多个条件符合,比如name='alvin',可能有两个name='alvin'的行数据)。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><2><span style="margin:0px;padding:0px;font-size:15px;"><tt class="docutils literal" style="margin:0px;padding:0px;">在“插入和更新数据”小节中,我们有提到模型的save()方法,这个方法会更新一行里的所有列。 而某些情况下,我们只需要更新行里的某几列。</tt></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">      在这个例子里我们可以看到Django的save()方法更新了不仅仅是title列的值,还有更新了所有的列。 若title以外的列有可能会被其他的进程所改动的情况下,只更改title列显然是更加明智的。更改某一指定的列,我们可以调用结果集(QuerySet)对象的update()方法,与之等同的SQL语句变得更高效,并且不会引起竞态条件。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">此外,update()方法对于任何结果集(QuerySet)均有效,这意味着你可以同时更新多条记录update()方法会返回一个整型数值,表示受影响的记录条数。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意,这里因为update返回的是一个整形,所以没法用query属性;对于每次创建一个对象,想显示对应的raw sql,需要在settings加上日志记录部分:</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">LOGGING</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;">注意:如果是多对多的改:</strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"> obj=Book.objects.filter(id=1<span style="margin:0px;padding:0px;line-height:1.5;">)[0] author</span>=Author.objects.filter(id__gt=2<span style="margin:0px;padding:0px;line-height:1.5;">) obj.author.clear() obj.author.add(</span>*author)</pre> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(0,51,102);"><strong style="margin:0px;padding:0px;">---------------------------------------查(filter,value等) <strong style="margin:0px;padding:0px;">-------------------------------------</strong></strong></span></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><strong style="margin:0px;padding:0px;">---------->查询API:</strong></span></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">补充:</strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">extra</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->惰性机制:</span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">所谓惰性机制:Publisher.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象),它并不会马上执行sql,而是当调用QuerySet的时候才执行。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">QuerySet特点:</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <1>  可迭代的</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <2>  可切片</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">QuerySet的高效使用:</strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->对象查询,单表条件查询,多表条件关联查询</span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">注意:条件查询即与对象查询对应,是指在filter,values等方法中的通过__来明确查询条件。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->聚合查询和分组查询</span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><1> aggregate(*args,**kwargs):</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   通过对QuerySet进行计算,返回一个聚合值的字典。aggregate()中每一个参数都指定一个包含在字典中的返回值。即在查询集上生成聚合。</p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><2> annotate(*args,**kwargs):</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   可以通过计算查询结果中每一个对象所关联的对象集合,从而得出总计值(也可以是平均值或总和),即为查询集的每一项生成聚合。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       查询alex出的书总价格 <strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">                  </span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/c933e563464f43c582153c505d2281e6.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">        查询各个作者出的书的总价格,这里就涉及到分组了,分组条件是authors__name</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/71b06489b22441debbae6ba4e9400f78.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;">    </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">         查询各个出版社最便宜的书价是多少</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">       <img src="http://img.e-com-net.com/image/info8/606020075b20420d9dba28e8620111b3.jpg" alt="" width="0" height="0" style="padding:0px;border:0px;max-width:900px;"></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);">---------->F查询和Q查询</span></strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;"><span style="margin:0px;padding:0px;color:rgb(128,0,128);"><span style="margin:0px;padding:0px;font-size:13px;color:rgb(51,51,153);">仅仅靠单一的关键字参数查询已经很难满足查询要求。此时Django为我们提供了F和Q查询:</span></span></strong></p> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <img class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" style="margin-bottom:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle;max-width:900px;" width="0" height="0">  <span class="cnblogs_code_collapse" style="margin:0px;padding:2px;border-width:1px;border-style:solid;border-color:#808080;background-color:rgb(255,255,255);line-height:1.5;">View Code</span> </div> <h4 style="margin-top:10px;margin-bottom:10px;padding:0px;font-size:14px;color:rgb(51,51,51);">raw sql</h4> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">django中models的操作,也是调用了ORM框架来实现的,pymysql 或者mysqldb,所以我们也可以使用原生的SQL语句来操作数据库!</p> <h3 style="padding:2px 0px 2px 10px;font-size:15px;line-height:24px;background:rgb(0,154,205);color:rgb(255,255,255);font-family:'comic sans ms', '微软雅黑', '宋体', '黑体', Arial;height:24px;width:680.98px;margin-top:12px;margin-bottom:12px;">九 admin的配置</h3> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">admin是django强大功能之一,它能共从数据库中读取数据,呈现在页面中,进行管理。默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,它已经够用,但是有时候,一些特殊的功能还需要定制,比如搜索功能,下面这一系列文章就逐步深入介绍如何定制适合自己的admin应用。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">如果你觉得英文界面不好用,可以在setting.py 文件中修改以下选项</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">LANGUAGE_CODE </code> <code class="python keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">=</code>  <code class="python string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'en-us'</code>   <code class="python comments" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,130,0);">#LANGUAGE_CODE = 'zh-hans'</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">一  认识ModelAdmin</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">   管理界面的定制类,如需扩展特定的model界面需从该类继承。</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">二 注册medel类到admin的两种方式:</strong></p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <1>   使用register的方法</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">admin.site.register(Book,MyAdmin)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">     <2>   使用register的装饰器</p> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter python" style="margin:1em 0px;padding:0px;width:1361.96px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;border:1px solid #C0C0C0;width:1361.82px;margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div></td> <td class="code" style="padding:3px;border-color:#C0C0C0;border-collapse:collapse;margin:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="python decorator" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">@admin</code> <code class="python plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">.register(Book)</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"><strong style="margin:0px;padding:0px;">三 掌握一些常用的设置技巧</strong></p> <ul style="margin-bottom:0px;margin-left:30px;"> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    list_display:     指定要显示的字段</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    search_fields:  指定搜索的字段</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    list_filter:        指定列表过滤器</li> <li style="margin-top:10px;margin-bottom:1em;margin-left:0px;padding:0px;list-style:square;">    ordering:       指定排序字段</li> </ul> <div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> <pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> django.contrib <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span><span style="margin:0px;padding:0px;line-height:1.5;"> admin </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">from</span> app01.models <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">import</span> * <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> Register your models here.</span> <span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;">#</span><span style="margin:0px;padding:0px;color:rgb(0,128,0);line-height:1.5;"> @admin.register(Book)#----->单给某个表加一个定制</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">class</span><span style="margin:0px;padding:0px;line-height:1.5;"> MyAdmin(admin.ModelAdmin): list_display </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">) search_fields </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">) list_filter </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">,) ordering </span>= (<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">,) fieldsets </span>=<span style="margin:0px;padding:0px;line-height:1.5;">[ (None, { </span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">fields</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">title</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">]}), (</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price information</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>, { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">fields</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">price</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>,<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">publisher</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>], <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">classes</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span>: [<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">collapse</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">]}), ] admin.site.register(Book,MyAdmin) admin.site.register(Publish) admin.site.register(Author)</span></pre> <div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"> <span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="http://img.e-com-net.com/image/info8/bd54bc823ad4473c8df81da2dd0e2402.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:900px;border:none;" width="0" height="0"></span> </div> </div> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;">参考文献:http://www.admin10000.com/document/2220.html</p> <p style="margin:10px auto;font-family:'Roboto Condensed', Tauri, 'Hiragino Sans GB', 'Microsoft YaHei', STHeiti, SimSun, 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Segoe UI', 'AppleSDGothicNeo-Medium', 'Malgun Gothic', Verdana, Tahoma, sans-serif;line-height:1.6;"> </p> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:20px 0px 0px;padding:0px;"> <div style="margin:0px 0px 10px;padding:0px;"></div> <div style="margin:20px 0px 0px;padding:0px;font-size:9pt;color:#808080;"></div> <div style="margin:0px;padding:0px;"> <div style="margin:10px 0px;padding:10px 0px;border:1px dashed #C0C0C0;font-size:12px;width:350px;text-align:center;"> 好文要顶  关注我  收藏该文  <img src="http://img.e-com-net.com/image/info8/302c4368f33946ac81a6c9b3e9b2a4f6.jpg" alt="" style="margin-bottom:0px;margin-left:5px;padding:0px;border:none;vertical-align:middle;" width="0" height="0">  <img src="http://img.e-com-net.com/image/info8/fad0f75d7e64488aa6ab06af2e85daab.jpg" alt="" style="margin-bottom:0px;margin-left:5px;padding:0px;border:medium none;width:24px;height:24px;vertical-align:middle;" width="0" height="0"> </div> <div style="margin:0px 0px 10px;padding:0px;float:left;width:280px;color:rgb(0,0,0);font-size:12px;"> <div class="author_profile_info" style="margin:0px;padding:0px;float:left;line-height:18px;"> <a href="http://img.e-com-net.com/image/info8/94f0358926ff439da3929ef1b2ba8998.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/94f0358926ff439da3929ef1b2ba8998.jpg" class="author_avatar" alt="" style="margin-right:5px;margin-bottom:0px;padding:5px 0px 0px 2px;border:0px;vertical-align:top;float:left;" width="48" height="48"></a> <div class="author_profile_info" style="margin:0px;padding:0px;float:left;line-height:18px;"> Yuan先生 <br style="margin:0px;padding:0px;"> 关注 - 0 <br style="margin:0px;padding:0px;"> 粉丝 - 1159 </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:0px;padding:0px;"></div> <div style="margin:0px;padding:0px;"> +加关注 </div> </div> <div style="margin:10px 30px 10px 0px;padding:0px;float:right;font-size:12px;width:125px;text-align:center;"> <div class="diggit" style="margin:2px 0px 0px;padding:5px 0px 0px;float:left;width:46px;height:52px;background:url("//static.cnblogs.com/images/upup.gif") no-repeat;"> <span class="diggnum" style="margin:0px;padding:0px;font-size:14px;color:rgb(7,93,179);font-family:Verdana;line-height:1.5em;">10</span> </div> <div class="buryit" style="margin:2px 0px 0px 20px;padding:5px 0px 0px;float:right;width:46px;height:52px;background:url("//static.cnblogs.com/images/downdown.gif") no-repeat;"> <span class="burynum" style="margin:0px;padding:0px;font-size:14px;color:rgb(7,93,179);font-family:Verdana;line-height:1.5em;">1</span> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div class="diggword" style="margin:5px 0px 0px;padding:0px;color:#808080;"></div> </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> <div style="margin:0px;padding:0px;line-height:1.8;font-size:12px;"> « 上一篇: 前端学习之jquery <br style="margin:0px;padding:0px;"> » 下一篇: Mysql与sql语句 <br style="margin:0px;padding:0px;"> </div> </div> </div> <div class="postDesc" style="margin:5px 0px 0px;padding:0px 5px 0px 0px;float:right;width:1368.95px;clear:both;text-align:right;color:rgb(102,102,102);"> posted @  <span style="margin:0px;padding:0px;">2016-11-20 20:36</span>  Yuan先生 阅读( <span style="margin:0px;padding:0px;">11158</span>) 评论( <span style="margin:0px;padding:0px;">9</span>)   编辑  收藏 </div> </div> </div> </div> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"></div> <br style="margin:0px;padding:0px;"> <div class="feedback_area_title" style="margin:20px 0px 10px;padding:0px 0px 0px 8px;font-weight:bold;border-bottom:1px solid rgb(51,51,51);"> 发表评论 </div> <div class="feedbackNoItems" style="margin:0px;padding:0px;"></div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #1楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-08-08 02:18</span> |  great_zhi    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 比alex的好多了 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(2) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #2楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-17 20:57</span> |  西雅图的邂逅    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 课讲的不错 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #3楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-19 09:26</span> |  define_null    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> @ great_zhi <br style="margin:0px;padding:0px;">敢黑我金角大王? </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #4楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-09-19 09:29</span> |  great_zhi    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> @ define_null <br style="margin:0px;padding:0px;">事实啊 他讲的太啰嗦了!我说的是事实!!! </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #5楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-11-09 10:47</span> |  深以为然    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 支持一下 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #6楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-11-10 03:30</span> |  其修远兮666    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 课讲的真不错,逻辑进度都挺好的,您讲的视频看过的都懂了。谢谢! </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #7楼   <span class="comment_date" style="margin:0px;padding:0px;">2017-12-18 16:31</span> |  战争热诚    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 老师您好,我在学习您博客的第一部分,什么是web框架的时候,运行您博客的step1 <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1300.5px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;width:1300px;margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 4 </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 5 </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 6 </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 7 </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 8 </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 9 </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 10 </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 11 </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 12 </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 13 </div></td> <td class="code" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">from</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">wsgiref.simple_server import make_server</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">def application(environ, start_response):</code> </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">start_response(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'200 OK'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, [(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'Content-Type'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'text/html'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">)])</code> </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">return</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">[b</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'<h1>Hello, web!</h1>'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">]</code> </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd = make_server(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">''</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, 8080, application)</code> </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;">   </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">print(</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'Serving HTTP on port 8000...'</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">)</code> </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp preprocessor" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#808080;"># 开始监听HTTP请求:</code> </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd.serve_forever()</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;">出现如下错误 <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;"> <div class="cnblogs_Highlighter sh-gutter" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="syntaxhighlighter csharp" style="margin:1em 0px;padding:0px;width:1300.5px;font-size:1em;background-color:rgb(255,255,255);"> <table border="0" style="border-spacing:0px;width:1300px;margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <tbody style="margin:0px;padding:0px;background:none;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <tr style="margin:0px;padding:0px;background:none;border-top:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <td class="gutter" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:35px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;color:rgb(175,175,175);"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 1 </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 2 </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 3 </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 4 </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 5 </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 6 </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 7 </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 8 </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 9 </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 10 </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 11 </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 12 </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px .5em;background:rgb(244,244,244) none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 13 </div> <div class="line number14 index13 alt1" style="margin:0px;padding:0px .5em;background-image:none;border-width:0px 2px 0px 0px;border-right-style:solid;border-right-color:rgb(108,226,108);float:none;height:auto;line-height:1.8em;text-align:right;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> 14 </div></td> <td class="code" style="margin:0px;padding:0px;background:none;border-width:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:12px;min-height:auto;"> <div class="container" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.1em;vertical-align:baseline;width:auto;min-height:auto;"> <div class="line number1 index0 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">Traceback (most recent call last):</code> </div> <div class="line number2 index1 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:/pycode/老男孩全栈工程师教育/server3.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 14, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);"><module></code> </div> <div class="line number3 index2 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">httpd = make_server(host=</code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">''</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">,port=8888,app =application)</code> </div> <div class="line number4 index3 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\wsgiref\simple_server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 153, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">make_server</code> </div> <div class="line number5 index4 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server = server_class((host, port), handler_class)</code> </div> <div class="line number6 index5 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\socketserver.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 453, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">__init__</code> </div> <div class="line number7 index6 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">self.server_bind()</code> </div> <div class="line number8 index7 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\wsgiref\simple_server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 50, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server_bind</code> </div> <div class="line number9 index8 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">HTTPServer.server_bind(self)</code> </div> <div class="line number10 index9 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\http\server.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 139, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">server_bind</code> </div> <div class="line number11 index10 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">self.server_name = socket.getfqdn(host)</code> </div> <div class="line number12 index11 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">  </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">File </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">"D:\python3\lib\socket.py"</code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">, line 673, </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">getfqdn</code> </div> <div class="line number13 index12 alt2" style="margin:0px;padding:0px 1em;background:rgb(244,244,244) none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp spaces" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;">    </code> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">hostname, aliases, ipaddrs = gethostbyaddr(name)</code> </div> <div class="line number14 index13 alt1" style="margin:0px;padding:0px 1em;background-image:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;min-height:auto;white-space:nowrap;"> <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">UnicodeDecodeError: </code> <code class="csharp string" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:#0000FF;">'utf-8'</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">codec can't decode </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">byte</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">0xcd </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">in</code>  <code class="csharp plain" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,0);">position 0: invalid continuation </code> <code class="csharp keyword" style="margin:0px;padding:0px;background:none;border:0px;float:none;height:auto;line-height:1.8em;vertical-align:baseline;width:auto;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;min-height:auto;white-space:nowrap;color:rgb(0,0,255);">byte</code> </div> </div></td> </tr> </tbody> </table> </div> </div> </div> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;"> <br style="margin:0px;padding:0px;">请问怎么解决 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #8楼   <span class="comment_date" style="margin:0px;padding:0px;">2018-01-08 16:06</span> |  Alan_chow    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 写的太好了,细致,实用。跳过了不少坑。 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(0) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div class="feedbackItem" style="margin:0px;padding:0px;"> <div class="feedbackListSubtitle" style="margin:0px;padding:0px;color:rgb(102,102,102);"> <div class="feedbackManage" style="margin:0px;padding:0px;width:220px;text-align:right;float:right;">    <span class="comment_actions" style="margin:0px;padding:0px;"></span> </div> #9楼   <span class="comment_date" style="margin:0px;padding:0px;">2018-01-15 16:43</span> |  雪竺轩    </div> <div class="feedbackCon" style="margin:0px 0px 1em;padding:15px 18px 10px 40px;border-bottom:1px solid rgb(204,204,204);background:url("/skins/summerGarden/images/comment.gif") no-repeat 5px 0px;min-height:35px;line-height:1.5em;width:1300.5px;"> <div class="blog_comment_body" style="margin:0px;padding:0px;"> 老师你博客的目录能开放吗 </div> <div class="comment_vote" style="margin:0px;padding:0px;text-align:right;"> 支持(10) 反对(0) </div> <br style="margin:0px;padding:0px;"> </div> </div> <div style="margin:0px;padding:0px;"></div> </div> <div class="commentform" style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"></div> <div style="margin:0px;padding:0px;text-align:right;"> <span style="margin:0px;padding:0px;"></span> 刷新评论 刷新页面 返回顶部 </div> <div style="margin:0px;padding:0px;"> (评论功能已被禁用) </div> <div class="ad_text_commentbox" style="margin:5px 0px;padding:0px;"></div> <div style="margin:5px 0px 0px;padding:0px;line-height:1.8;"> 【推荐】超50万VC++源码: 大型组态工控、电力仿真CAD与GIS源码库! <br style="margin:0px;padding:0px;"> 【推荐】华为云7大明星产品0元免费使用 <br style="margin:0px;padding:0px;"> 【推荐】腾讯云如何降低移动开发成本 <br style="margin:0px;padding:0px;"> 【大赛】2018首届“顶天立地”AI开发者大赛 <br style="margin:0px;padding:0px;"> </div> <div style="margin:0px;padding:0px;line-height:1.8;"></div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <a href="http://img.e-com-net.com/image/info8/88f22ea4f28e49949857e4d1babdf381.jpg" target="_blank"><img width="600" height="500" src="http://img.e-com-net.com/image/info8/88f22ea4f28e49949857e4d1babdf381.jpg" alt="Django 讲解_第2张图片" style="margin-bottom:0px;padding:0px;border:0px;;border:1px solid black;"></a> </div> <div style="margin:0px;padding:0px;"> <div class="itnews c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <strong style="margin:0px;padding:0px;">最新IT新闻</strong>: <br style="margin:0px;padding:0px;">·  任正非签发内部文:警惕“华为中年危机”! <br style="margin:0px;padding:0px;">·  《我的世界》国区用户破亿 官方宣布手游PC版数据互通 <br style="margin:0px;padding:0px;">·  风雨飘摇中的联想:贸工技与技工贸之争的答案 <br style="margin:0px;padding:0px;">·  经纬十年 张颖给自己打了69分 <br style="margin:0px;padding:0px;">·  陆奇“卸任”为何引发中国互联网人集体感伤? <br style="margin:0px;padding:0px;">»  更多新闻... </div> </div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <img src="http://img.e-com-net.com/image/info8/32e71ddcb0a44959a25eea5b2588cd37.jpg" alt="" style="margin-bottom:0px;padding:0px;border:0px;width:468px;height:60px;" width="0" height="0"> </div> <div style="margin:0px;padding:0px;"> <div class="itnews c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"> <strong style="margin:0px;padding:0px;">最新知识库文章</strong>: <br style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> ·  你可以把编程当做一项托付终身的职业 <br style="margin:0px;padding:0px;">·  评审的艺术——谈谈现实中的代码评审 <br style="margin:0px;padding:0px;">·  如何高效学习 <br style="margin:0px;padding:0px;">·  如何成为优秀的程序员? <br style="margin:0px;padding:0px;">·  菜鸟工程师的超神之路 -- 从校园到职场 <br style="margin:0px;padding:0px;"> </div>»  更多知识库文章... </div> </div> <div class="c_ad_block" style="margin:10px 0px 0px;padding:0px;line-height:1.5;"></div> </div> </div> </div> <div style="margin:0px;padding:16px 0px 0px 5px;width:230px;min-height:200px;float:left;color:rgb(136,134,111);"> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;width:229px;"></div> <div style="margin:0px;padding:0px;"> <div style="margin:0px;padding:0px;"> <div class="sidebar-block" style="margin:0px;padding:0px;"></div> </div> </div> </div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;"></div> </div> <div class="clear" style="margin:0px;padding:0px;clear:both;color:rgb(70,70,70);font-family:verdana, 'ms song', Arial, Helvetica, sans-serif;font-size:12px;background-color:rgb(250,247,239);"></div> </div> </div> </div> </div> </div> </div> <!--PC和WAP自适应版--> <div id="SOHUCS" sid="1461843791139909632"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- 文章页-底部 动态广告位 --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- 文章页-右侧1 动态广告位 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- 文章页-右侧2 动态广告位 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- 文章页-右侧3 动态广告位 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(django)</h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/1892350252057423872.htm" title="deepseek_各个版本django特性" target="_blank">deepseek_各个版本django特性</a> <span class="text-muted">终是蝶衣梦晓楼</span> <a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>以下是Django2.0至5.0的主要区别总结,按版本特性分类说明:1.Django2.0的主要变化Python支持仅支持Python3.4+,不再兼容Python2.x。路由系统弃用url(),引入path()和re_path()替代,path()默认不支持正则表达式,但提供内置转换器(如)进行参数类型匹配。支持更简洁的URL配置语法(例如path('articles//',views.year</div> </li> <li><a href="/article/1892314076684546048.htm" title="阿里云部署Django项目(超详细图文教程)—— Part3. Django settings修改、PostgreSQL配置" target="_blank">阿里云部署Django项目(超详细图文教程)—— Part3. Django settings修改、PostgreSQL配置</a> <span class="text-muted">马志峰的编程笔记</span> <a class="tag" taget="_blank" href="/search/Django%E9%83%A8%E7%BD%B2/1.htm">Django部署</a><a class="tag" taget="_blank" href="/search/postgresql/1.htm">postgresql</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/git/1.htm">git</a><a class="tag" taget="_blank" href="/search/nginx/1.htm">nginx</a><a class="tag" taget="_blank" href="/search/%E9%98%BF%E9%87%8C%E4%BA%91/1.htm">阿里云</a> <div>阿里云部署Django项目(超详细图文教程)Part3.Djangosettings修改、PostgreSQL配置前言:花了一个月的空闲时间,终于成功把Django网站部署到了阿里云ECS上,包含以下功能:不使用任何第三方工具,直接用网页连接阿里云ECS使用GIT进行源码控制和上传到服务器使用githooks实现自动部署用的是时下比较流行的一套部署方案——Nginx,Gunicorn,virtua</div> </li> <li><a href="/article/1892217062168784896.htm" title="Django框架全面指南" target="_blank">Django框架全面指南</a> <span class="text-muted">ivwdcwso</span> <a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91/1.htm">开发</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/sqlite/1.htm">sqlite</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a> <div>Django是一个高级的PythonWeb框架,它鼓励快速开发和清晰、实用的设计。本指南将全面介绍Django的核心概念和使用方法。1.Django简介Django遵循"batteriesincluded"哲学,提供了Web开发所需的几乎所有功能。它的主要特点包括:ORM(对象关系映射)URL路由模板引擎表单处理认证系统管理界面安全特性2.安装和项目设置安装Djangopipinstalldjan</div> </li> <li><a href="/article/1892204459266732032.htm" title="Django 5实用指南(二)项目结构与管理" target="_blank">Django 5实用指南(二)项目结构与管理</a> <span class="text-muted">网络风云</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>2.1Django5项目结构概述当你创建一个新的Django项目时,Django会自动生成一个默认的项目结构。这个结构是根据Django的最佳实践来设计的,以便开发者能够清晰地管理和维护项目中的各种组件。理解并管理好这些文件和目录结构是Django开发的基础。假设你使用django-adminstartprojectmyproject命令创建了一个新项目,下面是一个典型的Django5项目的文件结</div> </li> <li><a href="/article/1892172297465294848.htm" title="Django-Rules 开源项目安装与使用指南" target="_blank">Django-Rules 开源项目安装与使用指南</a> <span class="text-muted">尚虹卿</span> <div>Django-Rules开源项目安装与使用指南django-rulesAwesomeDjangoauthorization,withoutthedatabase项目地址:https://gitcode.com/gh_mirrors/dj/django-rules目录结构及介绍在Django-Rules的目录中,你可以看到以下主要文件和目录:django_rules:包含了核心逻辑以及权限管理的实现</div> </li> <li><a href="/article/1892167507691761664.htm" title="Django 5 实用指南(一)安装与配置" target="_blank">Django 5 实用指南(一)安装与配置</a> <span class="text-muted">网络风云</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>1.1Django5的背景与发展Django自从2005年由AdrianHolovaty和SimonWillison在LawrenceJournal-World新闻网站上首次发布以来,Django一直是Web开发领域最受欢迎的框架之一。Django框架经历了多个版本的演进,每次版本更新都引入了新功能、改进了性能、修复了安全漏洞,使其始终保持在Web开发框架的前沿。Django5作为最新的稳定版本,</div> </li> <li><a href="/article/1892020076119388160.htm" title="青少年编程与数学 02-009 Django 5 Web 编程 19课题、RESTful API开发" target="_blank">青少年编程与数学 02-009 Django 5 Web 编程 19课题、RESTful API开发</a> <span class="text-muted">明月看潮生</span> <a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/%E7%AC%AC02%E9%98%B6%E6%AE%B5/1.htm">第02阶段</a><a class="tag" taget="_blank" href="/search/%E9%9D%92%E5%B0%91%E5%B9%B4%E7%BC%96%E7%A8%8B/1.htm">青少年编程</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/restful/1.htm">restful</a> <div>青少年编程与数学02-009Django5Web编程19课题、RESTfulAPI开发一、RESTfulAPI核心概念特点设计原则应用场景优势挑战二、DRF核心特性使用场景优势示例代码安装DRF配置项目定义模型创建序列化器创建视图配置URLs三、创建API步骤1:创建Django项目和应用步骤2:安装DjangoRESTFramework步骤3:配置项目步骤4:定义模型步骤5:创建序列化器步骤6:</div> </li> <li><a href="/article/1892020077088272384.htm" title="青少年编程与数学 02-009 Django 5 Web 编程 23课题、安全性" target="_blank">青少年编程与数学 02-009 Django 5 Web 编程 23课题、安全性</a> <span class="text-muted">明月看潮生</span> <a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/%E7%AC%AC02%E9%98%B6%E6%AE%B5/1.htm">第02阶段</a><a class="tag" taget="_blank" href="/search/%E9%9D%92%E5%B0%91%E5%B9%B4%E7%BC%96%E7%A8%8B/1.htm">青少年编程</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/1.htm">网络安全</a> <div>青少年编程与数学02-009Django5Web编程23课题、安全性一、安全性安全性的定义安全性的关键方面安全性的实现方法安全性的挑战安全性的最佳实践二、安全漏洞1.注入漏洞2.跨站脚本(XSS)漏洞3.跨站请求伪造(CSRF)漏洞4.不安全的认证和会话管理5.安全配置错误6.不安全的反序列化7.使用含有已知漏洞的组件8.文件上传漏洞9.缓存区溢出10.信息泄露防范措施三、Django项目的安全性</div> </li> <li><a href="/article/1891836265989468160.htm" title="DRF框架使用djangorestframework-simplejwt实现自定义用户类的token校验" target="_blank">DRF框架使用djangorestframework-simplejwt实现自定义用户类的token校验</a> <span class="text-muted">lj907722644</span> <a class="tag" taget="_blank" href="/search/Python/1.htm">Python</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/jwt/1.htm">jwt</a><a class="tag" taget="_blank" href="/search/DRF/1.htm">DRF</a><a class="tag" taget="_blank" href="/search/simplejwt/1.htm">simplejwt</a> <div>1.安装simplejwt库并修改settings.py安装simplejwtpipinstalldjangorestframeworkpipinstalldjangorestframework-simplejwt修改settings.py注册应用INSTALLED_APPS=[...'rest_framework','rest_framework_simplejwt',]设置jwt鉴权REST_</div> </li> <li><a href="/article/1891836267327451136.htm" title="DRF框架使用djangorestframework-simplejwt实现自定义用户类的登录逻辑" target="_blank">DRF框架使用djangorestframework-simplejwt实现自定义用户类的登录逻辑</a> <span class="text-muted">lj907722644</span> <a class="tag" taget="_blank" href="/search/Python/1.htm">Python</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/simplejwt/1.htm">simplejwt</a><a class="tag" taget="_blank" href="/search/token/1.htm">token</a> <div>1.token校验生成并校验token,参考上一篇文章:https://blog.csdn.net/u013071014/article/details/144562716?spm=1001.2014.3001.55022.新建role、menu子模块新建role、menu这两个子模块在PyCharm->Tools->Runmanage.pyTask中执行命令#创建role子模块startappr</div> </li> <li><a href="/article/1891831346683244544.htm" title="CSRF verification failed. Request aborted." target="_blank">CSRF verification failed. Request aborted.</a> <span class="text-muted">悟空空心</span> <a class="tag" taget="_blank" href="/search/csrf/1.htm">csrf</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a> <div>Forbidden(403)CSRFverificationfailed.Requestaborted.HelpReasongivenforfailure:CSRFtokenmissing.Ingeneral,thiscanoccurwhenthereisagenuineCrossSiteRequestForgery,orwhenDjango’sCSRFmechanismhasnotbeenuse</div> </li> <li><a href="/article/1891550160618582016.htm" title="python的django后台管理_python3 django-admin 初始化后台管理项目(mysql)" target="_blank">python的django后台管理_python3 django-admin 初始化后台管理项目(mysql)</a> <span class="text-muted">weixin_39582737</span> <div>环境和工具python3djangomysqlPyCharm$python3--versionPython3.7.0$django-admin--version2.2python3和django安装django初始化项目使用django-admin来初始化一个项目$django-adminstartprojectmydjango$cdmydjango$tree.├──manage.py#与该Dja</div> </li> <li><a href="/article/1891545863424110592.htm" title="Django之admin后台管理" target="_blank">Django之admin后台管理</a> <span class="text-muted">除却巫山不是云@</span> <a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>admin配置步骤:1.创建后台管理账号输入命令pythonmanage.pycreatesuperuser之后,设置用户名,密码,邮箱。输入命令pythonmanage.pyrunserver启动项目,打开127.0.0.1:8000/admin/进入后台管理界面,截图如下:输入设置的用户名和密码即可登录。如下所示:</div> </li> <li><a href="/article/1891540941089599488.htm" title="Django中的超级管理员相关操作" target="_blank">Django中的超级管理员相关操作</a> <span class="text-muted">胜天半月子</span> <a class="tag" taget="_blank" href="/search/Web/1.htm">Web</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>超级管理员操作场景描述添加超级管理员删除超级管理员更改超级管理员名称场景描述在进行管理员操作的时候,密码忘记,导致超级管理员无法使用,因此网上搜索相关操作,进行总结记录相关操作都是在控制台完成Terminal添加超级管理员pythonmanage.pycreatesuperuser(就是createsuperuser连接在一起)删除超级管理员pythonmanage.pyshellfromdjan</div> </li> <li><a href="/article/1891538167668994048.htm" title="Django后台新建管理员" target="_blank">Django后台新建管理员</a> <span class="text-muted">木制品123</span> <a class="tag" taget="_blank" href="/search/Django/1.htm">Django</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/sqlite/1.htm">sqlite</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a> <div>在Django中,新建管理员用户通常涉及使用Django自带的命令行工具manage.py。以下是具体步骤:前提条件Django项目已创建:确保你已经创建了一个Django项目和应用。数据库已迁移:确保你已经运行了pythonmanage.pymigrate来应用所有数据库迁移。步骤启动开发服务器(可选):虽然这一步不是必须的,但启动开发服务器可以帮助你确认项目是否正常运行。bash复制代码pyt</div> </li> <li><a href="/article/1891475667967602688.htm" title="从零开始:Django + MySQL + Vue 打造在线Demo下载平台" target="_blank">从零开始:Django + MySQL + Vue 打造在线Demo下载平台</a> <span class="text-muted">阮懿同</span> <div>从零开始:Django+MySQL+Vue打造在线Demo下载平台【下载地址】DjangoMySQLVue从零开始打造在线Demo下载平台Django+MySQL+Vue从零开始打造在线Demo下载平台本仓库提供了一套完整的前后端实战项目源码,带你全面学习和掌握在Django后端框架和Vue前端框架下,如何协作开发并部署一个功能完备的在线Demo下载网站项目地址:https://gitcode.c</div> </li> <li><a href="/article/1891369448250601472.htm" title="asp.net javascrip获取session的值_一篇文章搞定 Django Cookie 与 Session" target="_blank">asp.net javascrip获取session的值_一篇文章搞定 Django Cookie 与 Session</a> <span class="text-muted">weixin_39962285</span> <a class="tag" taget="_blank" href="/search/asp.net/1.htm">asp.net</a><a class="tag" taget="_blank" href="/search/html%E8%8E%B7%E5%8F%96session%E7%9A%84%E5%80%BC/1.htm">html获取session的值</a><a class="tag" taget="_blank" href="/search/session%E4%BF%9D%E5%AD%98%E5%AF%86%E7%A0%81/1.htm">session保存密码</a><a class="tag" taget="_blank" href="/search/session%E5%A4%B1%E6%95%88/1.htm">session失效</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF%E6%80%8E%E4%B9%88%E8%8E%B7%E5%8F%96session%E7%9A%84%E5%80%BC/1.htm">前端怎么获取session的值</a> <div>cookieCookie的由来大家都知道HTTP协议是无状态的。无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后面的请求响应情况。一句有意思的话来描述就是人生只如初见,对服务器来说,每次的请求都是全新的。状态可以理解为客户端和服务器在某次会话中产生的数据,那无状态的就以为这些数据不会被保留。会话中产生的数</div> </li> <li><a href="/article/1891259214886662144.htm" title="大数据知识图谱之深度学习——基于BERT+LSTM+CRF深度学习识别模型医疗知识图谱问答可视化系统_bert+lstm" target="_blank">大数据知识图谱之深度学习——基于BERT+LSTM+CRF深度学习识别模型医疗知识图谱问答可视化系统_bert+lstm</a> <span class="text-muted">2301_76348014</span> <a class="tag" taget="_blank" href="/search/%E7%A8%8B%E5%BA%8F%E5%91%98/1.htm">程序员</a><a class="tag" taget="_blank" href="/search/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/1.htm">深度学习</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE/1.htm">大数据</a><a class="tag" taget="_blank" href="/search/%E7%9F%A5%E8%AF%86%E5%9B%BE%E8%B0%B1/1.htm">知识图谱</a> <div>文章目录大数据知识图谱之深度学习——基于BERT+LSTM+CRF深度学习识别模型医疗知识图谱问答可视化系统一、项目概述二、系统实现基本流程三、项目工具所用的版本号四、所需要软件的安装和使用五、开发技术简介Django技术介绍Neo4j数据库Bootstrap4框架Echarts简介NavicatPremium15简介Layui简介Python语言介绍MySQL数据库深度学习六、核心理论贪心算法A</div> </li> <li><a href="/article/1891215839458291712.htm" title="8.12 orm-聚合查询" target="_blank">8.12 orm-聚合查询</a> <span class="text-muted">yangshiting84</span> <a class="tag" taget="_blank" href="/search/%E6%9A%91%E6%9C%9F%E8%A7%84%E5%88%92/1.htm">暑期规划</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>8.12orm-聚合查询聚合查询(aggregate)聚合查询函数是对一组值执行计算,并返回单个值。Django使用聚合查询前要先从django.db.models引入Avg、Max、Min、Count、Sum(首字母大写)。fromdjango.db.modelsimportAvg,Max,Min,Count,Sum#引入函数聚合查询返回值的数据类型是字典。聚合函数aggregate()是Que</div> </li> <li><a href="/article/1891215208785965056.htm" title="Django-ORM 单表查询" target="_blank">Django-ORM 单表查询</a> <span class="text-muted">本木夕丶Lu</span> <a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>文章目录数据准备一、查询关键字1.1QuerySet对象first方法last方法values方法values_list方法count()exists()1.2all()1.3filter(**kwargs)1.4get(**kwargs)--不推荐使用1.5exclude(**kwargs)1.6order_by(\*field)1.7reverse()1.8distinct()二、基于双下划线</div> </li> <li><a href="/article/1891210164418179072.htm" title="06 Django-orm-多表操作" target="_blank">06 Django-orm-多表操作</a> <span class="text-muted">lzplum619</span> <a class="tag" taget="_blank" href="/search/Django/1.htm">Django</a><a class="tag" taget="_blank" href="/search/Linux%E7%9B%B8%E5%85%B3/1.htm">Linux相关</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>一创建模型实例:我们来假定下面这些概念,字段和关系作者模型:一个作者有姓名和年龄。作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息。作者详情模型和作者模型之间是一对一的关系(one-to-one)出版商模型:出版商有名称,所在城市以及email。书籍模型:书籍有书名和出版日期,一本书可能会有多个作者,一个作者也可以写多本书,所以作者和书籍的关系就是多对多的关联关系(many-</div> </li> <li><a href="/article/1891205120897642496.htm" title="Django ORM - 聚合查询" target="_blank">Django ORM - 聚合查询</a> <span class="text-muted">lsx202406</span> <a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>DjangoORM-聚合查询引言Django是一个强大的PythonWeb框架,它拥有一个功能丰富的对象关系映射(Object-RelationalMapping,简称ORM)。ORM允许开发者以Python类和对象的交互方式来操作数据库,而不是直接编写SQL查询。本文将深入探讨DjangoORM中的聚合查询功能,包括其基本用法、常用函数以及在实际开发中的应用场景。聚合查询简介聚合查询,顾名思义,</div> </li> <li><a href="/article/1891033273690288128.htm" title="关于django __str__ 与 __unicode__ 问题" target="_blank">关于django __str__ 与 __unicode__ 问题</a> <span class="text-muted">weixin_40105587</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>因为所有字符串都作为Unicode字符串从数据库返回,基于字符的模型字段(CharField、TextField、URLField等)在Django从数据库检索数据时将包含Unicode值。即使数据可以放入ASCII字节字符串,也总是如此。您可以在创建模型或填充字段时传入bytestring,并且Django会在需要时将其转换为Unicode。¶选择__str__()和__unicode__()请</div> </li> <li><a href="/article/1891030370376282112.htm" title="django上传文件" target="_blank">django上传文件</a> <span class="text-muted">大得369</span> <a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/sqlite/1.htm">sqlite</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>1、settings.py配置#静态文件配置STATIC_URL='/static/'STATICFILES_DIRS=[BASE_DIR/'static',]上传文件#定义一个视图函数,该函数接收一个request参数fromdjango.shortcutsimportrender#必备引入importjsonfromdjango.views.decorators.httpimportrequi</div> </li> <li><a href="/article/1891023435866370048.htm" title="青少年编程与数学 02-009 Django 5 Web 编程 14课题、命名空间" target="_blank">青少年编程与数学 02-009 Django 5 Web 编程 14课题、命名空间</a> <span class="text-muted">明月看潮生</span> <a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/%E7%AC%AC02%E9%98%B6%E6%AE%B5/1.htm">第02阶段</a><a class="tag" taget="_blank" href="/search/%E9%9D%92%E5%B0%91%E5%B9%B4%E7%BC%96%E7%A8%8B/1.htm">青少年编程</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%95%B0%E5%AD%A6/1.htm">编程与数学</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>青少年编程与数学02-009Django5Web编程14课题、命名空间一、命名空间命名空间的作用如何定义和使用命名空间定义命名空间使用命名空间命名空间的注意事项二、命名空间的好处1.**避免URL名称冲突**2.**提高代码的可读性和可维护性**3.**增强应用的可重用性**4.**支持复杂的URL结构**5.**便于团队协作和文档编写**三、练习步骤1:创建项目和应用步骤2:定义模型`blog/</div> </li> <li><a href="/article/1891022173833850880.htm" title="python flask django在线投票系统 md14i" target="_blank">python flask django在线投票系统 md14i</a> <span class="text-muted">专注分享bishe530</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/flask/1.htm">flask</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a> <div>文章目录具体实现截图项目技术介绍论文写作思路核心代码部分展示可定制开发功能创新亮点django项目示例源码/演示视频获取方式具体实现截图项目技术介绍Python版本:python3.7以上框架支持:flask/django开发软件:PyCharm数据库:mysql数据库工具:Navicat浏览器:谷歌浏览器(PycharmFlaskDjangoVuemysql)论文写作思路第一部分绪论,主要介绍所</div> </li> <li><a href="/article/1890967070233915392.htm" title="ubuntu20.04中vscode配置django" target="_blank">ubuntu20.04中vscode配置django</a> <span class="text-muted">Galaxy_1229</span> <a class="tag" taget="_blank" href="/search/vscode/1.htm">vscode</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/ide/1.htm">ide</a> <div>1.下载插件我用的是这两个2.配置环境Ubuntu20.04创建虚拟环境python3-mvenv.venv没有venv的记得装一下sudoaptinstallpython3.8-venv装好之后,会出现.venv的文件夹找一下activate,我的在bin里按照提示sourcebin/activate完成后会显示(.venv)的虚拟环境,然后我们安装一下djangopipinstalldjang</div> </li> <li><a href="/article/1890927088047812608.htm" title="vue和Django快速创建项目" target="_blank">vue和Django快速创建项目</a> <span class="text-muted">CCSBRIDGE</span> <a class="tag" taget="_blank" href="/search/%E4%BA%86%E8%A7%A3Vue/1.htm">了解Vue</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a> <div>一、VUE1.创建Vue3+JavaScript项目npmcreatevite@latest项目名称----templatevue创建Vue3+TypeScript项目npmcreatevite@latest项目名称----templatevue-ts2.然后cd项目名称npminstallnpminstallaxios#发送API请求npminstallpinia#Vue3推荐的状态管理库npm</div> </li> <li><a href="/article/1890837945800257536.htm" title="500道Python毕业设计题目推荐,附源码" target="_blank">500道Python毕业设计题目推荐,附源码</a> <span class="text-muted">Java老徐</span> <a class="tag" taget="_blank" href="/search/Python/1.htm">Python</a><a class="tag" taget="_blank" href="/search/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/1.htm">毕业设计</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1/1.htm">课程设计</a><a class="tag" taget="_blank" href="/search/notepad%2B%2B/1.htm">notepad++</a><a class="tag" taget="_blank" href="/search/Python%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1%E9%A2%98%E7%9B%AE/1.htm">Python毕业设计题目</a><a class="tag" taget="_blank" href="/search/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1%E9%A2%98%E7%9B%AE%E6%8E%A8%E8%8D%90/1.htm">毕业设计题目推荐</a><a class="tag" taget="_blank" href="/search/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1%E9%A2%98%E7%9B%AE/1.htm">毕业设计题目</a> <div>博主介绍:✌Java老徐、7年大厂程序员经历。全网粉丝12w+、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌文末获取源码联系精彩专栏推荐订阅不然下次找不到哟Java基于微信小程序的校园外卖平台设计与实现,附源码Python基于Django的微博热搜、微博舆论可视化系统,附源码Java基于SpringBoot+Vue的学生宿舍管理系统感兴趣</div> </li> <li><a href="/article/1890811096684621824.htm" title="Django中间件的使用及功能" target="_blank">Django中间件的使用及功能</a> <span class="text-muted">.明天的自己</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E4%B8%AD%E9%97%B4%E4%BB%B6/1.htm">中间件</a> <div>Django中间件的使用及功能什么是中间件中间件应用于request与服务端之间和服务端与response之间,客户端发起请求到服务端接收可以通过中间件,服务端返回响应与客户端接收响应可以通过中间件,也就是说中间件可以处理request和response。中间件是Django请求/响应处理的钩子框架,也是一个非常重要的插件,用于改变Djang全局的输入以及输出.每个中间件都负责执行一些特定的功能.</div> </li> <li><a href="/article/81.htm" title="java短路运算符和逻辑运算符的区别" target="_blank">java短路运算符和逻辑运算符的区别</a> <span class="text-muted">3213213333332132</span> <a class="tag" taget="_blank" href="/search/java%E5%9F%BA%E7%A1%80/1.htm">java基础</a> <div> /* * 逻辑运算符——不论是什么条件都要执行左右两边代码 * 短路运算符——我认为在底层就是利用物理电路的“并联”和“串联”实现的 * 原理很简单,并联电路代表短路或(||),串联电路代表短路与(&&)。 * * 并联电路两个开关只要有一个开关闭合,电路就会通。 * 类似于短路或(||),只要有其中一个为true(开关闭合)是</div> </li> <li><a href="/article/208.htm" title="Java异常那些不得不说的事" target="_blank">Java异常那些不得不说的事</a> <span class="text-muted">白糖_</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/exception/1.htm">exception</a> <div>一、在finally块中做数据回收操作 比如数据库连接都是很宝贵的,所以最好在finally中关闭连接。 JDBCAgent jdbc = new JDBCAgent(); try{ jdbc.excute("select * from ctp_log"); }catch(SQLException e){ ... }finally{ jdbc.close(); </div> </li> <li><a href="/article/335.htm" title="utf-8与utf-8(无BOM)的区别" target="_blank">utf-8与utf-8(无BOM)的区别</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/PHP/1.htm">PHP</a> <div>BOM——Byte Order Mark,就是字节序标记   在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输 字符"ZERO WIDTH NO-BREAK SPACE"。这样如</div> </li> <li><a href="/article/462.htm" title="JAVA Annotation之定义篇" target="_blank">JAVA Annotation之定义篇</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E6%B3%A8%E8%A7%A3/1.htm">注解</a><a class="tag" taget="_blank" href="/search/annotation/1.htm">annotation</a><a class="tag" taget="_blank" href="/search/%E5%85%A5%E9%97%A8/1.htm">入门</a><a class="tag" taget="_blank" href="/search/%E6%B3%A8%E9%87%8A/1.htm">注释</a> <div>    Annotation: 译为注释或注解 An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, pa</div> </li> <li><a href="/article/589.htm" title="tomcat的多域名、虚拟主机配置" target="_blank">tomcat的多域名、虚拟主机配置</a> <span class="text-muted">g21121</span> <a class="tag" taget="_blank" href="/search/tomcat/1.htm">tomcat</a> <div>众所周知apache可以配置多域名和虚拟主机,而且配置起来比较简单,但是项目用到的是tomcat,配来配去总是不成功。查了些资料才总算可以,下面就跟大家分享下经验。 很多朋友搜索的内容基本是告诉我们这么配置: 在Engine标签下增面积Host标签,如下: <Host name="www.site1.com" appBase="webapps"</div> </li> <li><a href="/article/716.htm" title="Linux SSH 错误解析(Capistrano 的cap 访问错误 Permission )" target="_blank">Linux SSH 错误解析(Capistrano 的cap 访问错误 Permission )</a> <span class="text-muted">510888780</span> <a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/capistrano/1.htm">capistrano</a> <div> 1.ssh -v hdfs@192.168.18.133 出现 Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 错误 运行状况如下: OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuratio</div> </li> <li><a href="/article/843.htm" title="log4j的用法" target="_blank">log4j的用法</a> <span class="text-muted">Harry642</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/log4j/1.htm">log4j</a> <div>一、前言:     log4j 是一个开放源码项目,是广泛使用的以Java编写的日志记录包。由于log4j出色的表现,     当时在log4j完成时,log4j开发组织曾建议sun在jdk1.4中用log4j取代jdk1.4 的日志工具类,但当时jdk1.4已接近完成,所以sun拒绝使用log4j,当在java开发中</div> </li> <li><a href="/article/970.htm" title="mysql、sqlserver、oracle分页,java分页统一接口实现" target="_blank">mysql、sqlserver、oracle分页,java分页统一接口实现</a> <span class="text-muted">aijuans</span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/jave/1.htm">jave</a> <div> 定义:pageStart 起始页,pageEnd 终止页,pageSize页面容量 oracle分页:     select * from ( select mytable.*,rownum num from (实际传的SQL) where rownum<=pageEnd) where num>=pageStart sqlServer分页:  </div> </li> <li><a href="/article/1097.htm" title="Hessian 简单例子" target="_blank">Hessian 简单例子</a> <span class="text-muted">antlove</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/Web/1.htm">Web</a><a class="tag" taget="_blank" href="/search/service/1.htm">service</a><a class="tag" taget="_blank" href="/search/hessian/1.htm">hessian</a> <div>hello.hessian.MyCar.java package hessian.pojo; import java.io.Serializable; public class MyCar implements Serializable { private static final long serialVersionUID = 473690540190845543</div> </li> <li><a href="/article/1224.htm" title="数据库对象的同义词和序列" target="_blank">数据库对象的同义词和序列</a> <span class="text-muted">百合不是茶</span> <a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/%E5%BA%8F%E5%88%97/1.htm">序列</a><a class="tag" taget="_blank" href="/search/%E5%90%8C%E4%B9%89%E8%AF%8D/1.htm">同义词</a><a class="tag" taget="_blank" href="/search/ORACLE%E6%9D%83%E9%99%90/1.htm">ORACLE权限</a> <div>回顾简单的数据库权限等命令; 解锁用户和锁定用户 alter user scott account lock/unlock; //system下查看系统中的用户 select * dba_users; //创建用户名和密码 create user wj identified by wj; identified by //授予连接权和建表权 grant connect to </div> </li> <li><a href="/article/1351.htm" title="使用Powermock和mockito测试静态方法" target="_blank">使用Powermock和mockito测试静态方法</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/1.htm">持续集成</a><a class="tag" taget="_blank" href="/search/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/1.htm">单元测试</a><a class="tag" taget="_blank" href="/search/mockito/1.htm">mockito</a><a class="tag" taget="_blank" href="/search/Powermock/1.htm">Powermock</a> <div>        实例: package com.bijian.study; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Before; import org.junit.Test; import or</div> </li> <li><a href="/article/1478.htm" title="精通Oracle10编程SQL(6)访问ORACLE" target="_blank">精通Oracle10编程SQL(6)访问ORACLE</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/plsql/1.htm">plsql</a> <div>/* *访问ORACLE */ --检索单行数据 --使用标量变量接收数据 DECLARE v_ename emp.ename%TYPE; v_sal emp.sal%TYPE; BEGIN select ename,sal into v_ename,v_sal from emp where empno=&no; dbms_output.pu</div> </li> <li><a href="/article/1605.htm" title="【Nginx四】Nginx作为HTTP负载均衡服务器" target="_blank">【Nginx四】Nginx作为HTTP负载均衡服务器</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/nginx/1.htm">nginx</a> <div> Nginx的另一个常用的功能是作为负载均衡服务器。一个典型的web应用系统,通过负载均衡服务器,可以使得应用有多台后端服务器来响应客户端的请求。一个应用配置多台后端服务器,可以带来很多好处:   负载均衡的好处 增加可用资源 增加吞吐量 加快响应速度,降低延时 出错的重试验机制 Nginx主要支持三种均衡算法: round-robin l</div> </li> <li><a href="/article/1732.htm" title="jquery-validation备忘" target="_blank">jquery-validation备忘</a> <span class="text-muted">白糖_</span> <a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a><a class="tag" taget="_blank" href="/search/css/1.htm">css</a><a class="tag" taget="_blank" href="/search/F%23/1.htm">F#</a><a class="tag" taget="_blank" href="/search/Firebug/1.htm">Firebug</a> <div>留点学习jquery validation总结的代码:   function checkForm(){ validator = $("#commentForm").validate({// #formId为需要进行验证的表单ID errorElement :"span",// 使用"div"标签标记错误, 默认:&</div> </li> <li><a href="/article/1859.htm" title="solr限制admin界面访问(端口限制和http授权限制)" target="_blank">solr限制admin界面访问(端口限制和http授权限制)</a> <span class="text-muted">ronin47</span> <a class="tag" taget="_blank" href="/search/%E9%99%90%E5%AE%9AIp%E8%AE%BF%E9%97%AE/1.htm">限定Ip访问</a> <div>solr的管理界面可以帮助我们做很多事情,但是把solr程序放到公网之后就要限制对admin的访问了。 可以通过tomcat的http基本授权来做限制,也可以通过iptables防火墙来限制。 我们先看如何通过tomcat配置http授权限制。 第一步: 在tomcat的conf/tomcat-users.xml文件中添加管理用户,比如: <userusername="ad</div> </li> <li><a href="/article/1986.htm" title="多线程-用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1" target="_blank">多线程-用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%A4%9A%E7%BA%BF%E7%A8%8B/1.htm">多线程</a> <div> public class IncDecThread { private int j=10; /* * 题目:用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1 * 两个问题: * 1、线程同步--synchronized * 2、线程之间如何共享同一个j变量--内部类 */ public static </div> </li> <li><a href="/article/2113.htm" title="买房历程" target="_blank">买房历程</a> <span class="text-muted">cfyme</span> <div>    2015-06-21: 万科未来城,看房子   2015-06-26: 办理贷款手续,贷款73万,贷款利率5.65=5.3675   2015-06-27: 房子首付,签完合同   2015-06-28,央行宣布降息 0.25,就2天的时间差啊,没赶上。   首付,老婆找他的小姐妹接了5万,另外几个朋友借了1-</div> </li> <li><a href="/article/2240.htm" title="[军事与科技]制造大型太空战舰的前奏" target="_blank">[军事与科技]制造大型太空战舰的前奏</a> <span class="text-muted">comsci</span> <a class="tag" taget="_blank" href="/search/%E5%88%B6%E9%80%A0/1.htm">制造</a> <div>        天气热了........空调和电扇要准备好..........        最近,世界形势日趋复杂化,战争的阴影开始覆盖全世界..........        所以,我们不得不关</div> </li> <li><a href="/article/2367.htm" title="dateformat" target="_blank">dateformat</a> <span class="text-muted">dai_lm</span> <a class="tag" taget="_blank" href="/search/DateFormat/1.htm">DateFormat</a> <div> "Symbol Meaning Presentation Ex." "------ ------- ------------ ----" "G era designator (Text) AD" "y year</div> </li> <li><a href="/article/2494.htm" title="Hadoop如何实现关联计算" target="_blank">Hadoop如何实现关联计算</a> <span class="text-muted">datamachine</span> <a class="tag" taget="_blank" href="/search/mapreduce/1.htm">mapreduce</a><a class="tag" taget="_blank" href="/search/hadoop/1.htm">hadoop</a><a class="tag" taget="_blank" href="/search/%E5%85%B3%E8%81%94%E8%AE%A1%E7%AE%97/1.htm">关联计算</a> <div>    选择Hadoop,低成本和高扩展性是主要原因,但但它的开发效率实在无法让人满意。     以关联计算为例。     假设:HDFS上有2个文件,分别是客户信息和订单信息,customerID是它们之间的关联字段。如何进行关联计算,以便将客户名称添加到订单列表中?   &nbs</div> </li> <li><a href="/article/2621.htm" title="用户模型中修改用户信息时,密码是如何处理的" target="_blank">用户模型中修改用户信息时,密码是如何处理的</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/yii/1.htm">yii</a> <div>当我添加或修改用户记录的时候对于处理确认密码我遇到了一些麻烦,所有我想分享一下我是怎么处理的。 场景是使用的基本的那些(系统自带),你需要有一个数据表(user)并且表中有一个密码字段(password),它使用 sha1、md5或其他加密方式加密用户密码。 面是它的工作流程: 当创建用户的时候密码需要加密并且保存,但当修改用户记录时如果使用同样的场景我们最终就会把用户加密过的密码再次加密,这</div> </li> <li><a href="/article/2748.htm" title="中文 iOS/Mac 开发博客列表" target="_blank">中文 iOS/Mac 开发博客列表</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/Blog/1.htm">Blog</a> <div>  本博客列表会不断更新维护,如果有推荐的博客,请到此处提交博客信息。 本博客列表涉及的文章内容支持 定制化Google搜索,特别感谢 JeOam 提供并帮助更新。 本博客列表也提供同步更新的OPML文件(下载OPML文件),可供导入到例如feedly等第三方定阅工具中,特别感谢 lcepy 提供自动转换脚本。这里有导入教程。 </div> </li> <li><a href="/article/2875.htm" title="js去除空格,去除左右两端的空格" target="_blank">js去除空格,去除左右两端的空格</a> <span class="text-muted">蕃薯耀</span> <a class="tag" taget="_blank" href="/search/%E5%8E%BB%E9%99%A4%E5%B7%A6%E5%8F%B3%E4%B8%A4%E7%AB%AF%E7%9A%84%E7%A9%BA%E6%A0%BC/1.htm">去除左右两端的空格</a><a class="tag" taget="_blank" href="/search/js%E5%8E%BB%E6%8E%89%E6%89%80%E6%9C%89%E7%A9%BA%E6%A0%BC/1.htm">js去掉所有空格</a><a class="tag" taget="_blank" href="/search/js%E5%8E%BB%E9%99%A4%E7%A9%BA%E6%A0%BC/1.htm">js去除空格</a> <div>js去除空格,去除左右两端的空格 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>&g</div> </li> <li><a href="/article/3002.htm" title="SpringMVC4零配置--web.xml" target="_blank">SpringMVC4零配置--web.xml</a> <span class="text-muted">hanqunfeng</span> <a class="tag" taget="_blank" href="/search/springmvc4/1.htm">springmvc4</a> <div>servlet3.0+规范后,允许servlet,filter,listener不必声明在web.xml中,而是以硬编码的方式存在,实现容器的零配置。 ServletContainerInitializer:启动容器时负责加载相关配置 package javax.servlet; import java.util.Set; public interface ServletContainer</div> </li> <li><a href="/article/3129.htm" title="《开源框架那些事儿21》:巧借力与借巧力" target="_blank">《开源框架那些事儿21》:巧借力与借巧力</a> <span class="text-muted">j2eetop</span> <a class="tag" taget="_blank" href="/search/%E6%A1%86%E6%9E%B6/1.htm">框架</a><a class="tag" taget="_blank" href="/search/UI/1.htm">UI</a> <div>同样做前端UI,为什么有人花了一点力气,就可以做好?而有的人费尽全力,仍然错误百出?我们可以先看看几个故事。 故事1:巧借力,乌鸦也可以吃核桃 有一个盛产核桃的村子,每年秋末冬初,成群的乌鸦总会来到这里,到果园里捡拾那些被果农们遗落的核桃。 核桃仁虽然美味,但是外壳那么坚硬,乌鸦怎么才能吃到呢?原来乌鸦先把核桃叼起,然后飞到高高的树枝上,再将核桃摔下去,核桃落到坚硬的地面上,被撞破了,于是,</div> </li> <li><a href="/article/3256.htm" title="JQuery EasyUI 验证扩展" target="_blank">JQuery EasyUI 验证扩展</a> <span class="text-muted">可怜的猫</span> <a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a><a class="tag" taget="_blank" href="/search/easyui/1.htm">easyui</a><a class="tag" taget="_blank" href="/search/%E9%AA%8C%E8%AF%81/1.htm">验证</a> <div>  最近项目中用到了前端框架-- EasyUI,在做校验的时候会涉及到很多需要自定义的内容,现把常用的验证方式总结出来,留待后用。   以下内容只需要在公用js中添加即可。   使用类似于如下: <input class="easyui-textbox" name="mobile" id="mobile&</div> </li> <li><a href="/article/3383.htm" title="架构师之httpurlconnection----------读取和发送(流读取效率通用类)" target="_blank">架构师之httpurlconnection----------读取和发送(流读取效率通用类)</a> <span class="text-muted">nannan408</span> <div>1.前言.    如题. 2.代码. /* * Copyright (c) 2015, S.F. Express Inc. All rights reserved. */ package com.test.test.test.send; import java.io.IOException; import java.io.InputStream</div> </li> <li><a href="/article/3510.htm" title="Jquery性能优化" target="_blank">Jquery性能优化</a> <span class="text-muted">r361251</span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a><a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a> <div>一、注意定义jQuery变量的时候添加var关键字 这个不仅仅是jQuery,所有javascript开发过程中,都需要注意,请一定不要定义成如下: $loading = $('#loading'); //这个是全局定义,不知道哪里位置倒霉引用了相同的变量名,就会郁闷至死的 二、请使用一个var来定义变量 如果你使用多个变量的话,请如下方式定义: . 代码如下: var page </div> </li> <li><a href="/article/3637.htm" title="在eclipse项目中使用maven管理依赖" target="_blank">在eclipse项目中使用maven管理依赖</a> <span class="text-muted">tjj006</span> <a class="tag" taget="_blank" href="/search/eclipse/1.htm">eclipse</a><a class="tag" taget="_blank" href="/search/maven/1.htm">maven</a> <div>概览: 如何导入maven项目至eclipse中 建立自有Maven  Java类库服务器 建立符合maven代码库标准的自定义类库 Maven在管理Java类库方面有巨大的优势,像白衣所说就是非常“环保”。 我们平时用IDE开发都是把所需要的类库一股脑的全丢到项目目录下,然后全部添加到ide的构建路径中,如果用了SVN/CVS,这样会很容易就 把</div> </li> <li><a href="/article/3764.htm" title="中国天气网省市级联页面" target="_blank">中国天气网省市级联页面</a> <span class="text-muted">x125858805</span> <a class="tag" taget="_blank" href="/search/%E7%BA%A7%E8%81%94/1.htm">级联</a> <div>1、页面及级联js <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> &l</div> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong>按字母分类:</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/">首页</a> - <a target="_blank" href="/custom/about.htm">关于我们</a> - <a target="_blank" href="/search/Java/1.htm">站内搜索</a> - <a target="_blank" href="/sitemap.txt">Sitemap</a> - <a target="_blank" href="/custom/delete.htm">侵权投诉</a> </div> <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved. <!-- <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>--> </div> </div> </footer> <!-- 代码高亮 --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html>