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/1835408447806468096.htm" title="Ubuntu18.04 Docker部署Kinship(Django)项目过程" target="_blank">Ubuntu18.04 Docker部署Kinship(Django)项目过程</a> <span class="text-muted">Dante617</span> <div>1Docker的安装https://blog.csdn.net/weixin_41735055/article/details/1003551792下载镜像dockerpullprogramize/python3.6.8-dlib下载的镜像里包含python3.6.8和dlib19.17.03启动镜像dockerrun-it--namekinship-p7777:80-p3307:3306-p55</div> </li> <li><a href="/article/1835259969000271872.htm" title="软件测试/测试开发/全日制 |利用Django REST framework构建微服务" target="_blank">软件测试/测试开发/全日制 |利用Django REST framework构建微服务</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/%E5%BE%AE%E6%9C%8D%E5%8A%A1/1.htm">微服务</a><a class="tag" taget="_blank" href="/search/sqlite/1.htm">sqlite</a> <div>霍格沃兹测试开发学社推出了《Python全栈开发与自动化测试班》。本课程面向开发人员、测试人员与运维人员,课程内容涵盖Python编程语言、人工智能应用、数据分析、自动化办公、平台开发、UI自动化测试、接口测试、性能测试等方向。为大家提供更全面、更深入、更系统化的学习体验,课程还增加了名企私教服务内容,不仅有名企经理为你1v1辅导,还有行业专家进行技术指导,针对性地解决学习、工作中遇到的难题。让找</div> </li> <li><a href="/article/1835210184419536896.htm" title="锋哥写一套前后端分离Python权限系统 基于Django5+DRF+Vue3.2+Element Plus+Jwt 视频教程 ,帅呆了~~" target="_blank">锋哥写一套前后端分离Python权限系统 基于Django5+DRF+Vue3.2+Element Plus+Jwt 视频教程 ,帅呆了~~</a> <span class="text-muted">java1234_小锋</span> <a class="tag" taget="_blank" href="/search/Python/1.htm">Python</a><a class="tag" taget="_blank" href="/search/%E6%9D%83%E9%99%90%E7%B3%BB%E7%BB%9F/1.htm">权限系统</a><a class="tag" taget="_blank" href="/search/django%E6%9D%83%E9%99%90%E7%B3%BB%E7%BB%9F/1.htm">django权限系统</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/web%E6%9D%83%E9%99%90%E7%B3%BB%E7%BB%9F/1.htm">web权限系统</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/DRF/1.htm">DRF</a><a class="tag" taget="_blank" href="/search/VUE%E6%9D%83%E9%99%90/1.htm">VUE权限</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>大家好,我是java1234_小锋老师,最近写了一套【前后端分离Python权限系统基于Django5+DRF+Vue3.2+ElementPlus+Jwt】视频教程,持续更新中,计划月底更新完,感谢支持。视频在线地址:打造前后端分离Python权限系统基于Django5+DRF+Vue3.2+ElementPlus+Jwt视频教程(火爆连载更新中..)_哔哩哔哩_bilibili项目介绍本课程采</div> </li> <li><a href="/article/1835178538907561984.htm" title="2.8.5Django --8.2 单表操作" target="_blank">2.8.5Django --8.2 单表操作</a> <span class="text-muted">寒暄_HX</span> <div>Django目录:https://www.jianshu.com/p/dc36f62b3dc5Yuan先生-Django模型层(1)Django与SQLAlchemy的ORM操作本质上是一样的,但是语法略有不同,如果是用Django进行开发最好使用原生的ORM或者直接使用原生SQL。创建表app06创建模型在app06中的models.py文件内,新建一个模板。one_exa.app06.mode</div> </li> <li><a href="/article/1835165891906596864.htm" title="关于django中几个重要的gunicorn worker的配置" target="_blank">关于django中几个重要的gunicorn worker的配置</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/gunicorn/1.htm">gunicorn</a> <div>关于django中几个重要的gunicornworker的配置一、worker_classworker_class是Gunicorn的配置参数之一,它指定了工作进程(worker)的类型。不同的worker_class提供了不同的并发模型,适合不同类型的应用场景。sync和gevent是两种常见的worker_class,它们的作用和区别如下:1.sync(同步worker)默认值:如果没有指定w</div> </li> <li><a href="/article/1835165892321832960.htm" title="gunicorn未重启,导致代码没拉到最新问题。" target="_blank">gunicorn未重启,导致代码没拉到最新问题。</a> <span class="text-muted">给我起把狙</span> <a class="tag" taget="_blank" href="/search/gunicorn/1.htm">gunicorn</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a> <div>问题描述:我的测试环境的django项目是用gunicorn启动的,然后我本地使用pythonmanagy.py启动的,新写了个接口,在本地测试没问题,在测试环境报404。我发现发了最新代码到远端,然后我在测试环境pull下来的时候出问题了,代码不是最新的,后来我的经理告诉我说要重启gunicorn的主进程给了我一条指令"kill-HUPgunicorn_pid(最小的)”,针对这个问题我大致做了</div> </li> <li><a href="/article/1835019650895802368.htm" title="python基于django/flask的NBA球员大数据分析与可视化python+java+node.js" target="_blank">python基于django/flask的NBA球员大数据分析与可视化python+java+node.js</a> <span class="text-muted">QQ_511008285</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/flask/1.htm">flask</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/boot/1.htm">boot</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/1.htm">数据分析</a> <div>前端开发框架:vue.js数据库mysql版本不限后端语言框架支持:1java(SSM/springboot)-idea/eclipse2.Nodejs+Vue.js-vscode3.python(flask/django)--pycharm/vscode4.php(thinkphp/laravel)-hbuilderx数据库工具:Navicat/SQLyog等都可以本文针对NBA球员的大数据进行</div> </li> <li><a href="/article/1835019524521422848.htm" title="Java基于spring boot的国产电影数据分析与可视化python+java+node.js" target="_blank">Java基于spring boot的国产电影数据分析与可视化python+java+node.js</a> <span class="text-muted">QQ_511008285</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/boot/1.htm">boot</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/1.htm">数据分析</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/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/flask/1.htm">flask</a> <div>前端开发框架:vue.js数据库mysql版本不限后端语言框架支持:1java(SSM/springboot)-idea/eclipse2.Nodejs+Vue.js-vscode3.python(flask/django)--pycharm/vscode4.php(thinkphp/laravel)-hbuilderx数据库工具:Navicat/SQLyog等都可以  该系统使用进行大数据处理和</div> </li> <li><a href="/article/1834965554214039552.htm" title="分布式框架Celery七(Django-Celery-Flower实现异步和定时爬虫及其监控邮件告警)" target="_blank">分布式框架Celery七(Django-Celery-Flower实现异步和定时爬虫及其监控邮件告警)</a> <span class="text-muted">yjjpp2301</span> <a class="tag" taget="_blank" href="/search/Celery/1.htm">Celery</a><a class="tag" taget="_blank" href="/search/%E5%88%86%E5%B8%83%E5%BC%8F/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>Django中集成方式安装模块pipinstallDjango==3.2.22pipinstallcelerypipinstallredispipinstalleventlet#在windows环境下需要安装eventlet包-----------pipinstalldjango-celery-beatpipinstalldjango-celery-resultspipinstalldjango-</div> </li> <li><a href="/article/1834906052047499264.htm" title="基于django+vue代驾管理系统【开题报告+程序+论文】-计算机毕设" target="_blank">基于django+vue代驾管理系统【开题报告+程序+论文】-计算机毕设</a> <span class="text-muted">zhjie102</span> <a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1/1.htm">课程设计</a> <div>本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着城市化进程的加速和人们生活水平的提高,私家车数量急剧增加,但随之而来的酒驾问题也日益严峻,严重威胁着道路交通安全与公众生命财产安全。为了有效遏制酒驾行为,代驾服务应运而生并迅速普及。然而,当前市场上的代驾服务大多依赖于电话预约、人工调度等传统方式,存在效率低下</div> </li> <li><a href="/article/1834897607902720000.htm" title="Python+Django毕业设计校园易购二手交易平台(程序+LW+部署)" target="_blank">Python+Django毕业设计校园易购二手交易平台(程序+LW+部署)</a> <span class="text-muted">Python、JAVA毕设程序源码</span> <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/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a> <div>项目运行环境配置:Jdk1.8+Tomcat7.0+Mysql+HBuilderX(Webstorm也行)+Eclispe(IntelliJIDEA,Eclispe,MyEclispe,Sts都支持)。项目技术:SSM+mybatis+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.ID</div> </li> <li><a href="/article/1834880085786718208.htm" title="python毕业设计作品:python闲置物品二手交易平台系统设计与实现毕业设计源代码(Django框架)" target="_blank">python毕业设计作品:python闲置物品二手交易平台系统设计与实现毕业设计源代码(Django框架)</a> <span class="text-muted">黄菊华老师</span> <a class="tag" taget="_blank" href="/search/%E6%AF%95%E8%AE%BE%E8%B5%84%E6%96%99/1.htm">毕设资料</a><a class="tag" taget="_blank" href="/search/python%E4%BA%8C%E6%89%8B%E4%BA%A4%E6%98%93%E5%B9%B3%E5%8F%B0%E7%B3%BB%E7%BB%9F/1.htm">python二手交易平台系统</a> <div>博主介绍:黄菊华老师《Vue.js入门与商城开发实战》《微信小程序商城开发》图书作者,CSDN博客专家,在线教育专家,CSDN钻石讲师;专注大学生毕业设计教育和辅导。所有项目都配有从入门到精通的基础知识视频课程,学习后应对毕业设计答辩。项目配有对应开发文档、开题报告、任务书、PPT、论文模版等项目都录了发布和功能操作演示视频;项目的界面和功能都可以定制,包安装运行!!!如果需要联系我,可以在CSD</div> </li> <li><a href="/article/1834793356543225856.htm" title="由于篇幅和复杂性限制,我无法在这里直接为你提供一个完整的、用多种编程语言实现的购物商城代码。但是,我可以为你概述如何使用几种流行的编程语言(如Python, JavaScript/Node.js, J" target="_blank">由于篇幅和复杂性限制,我无法在这里直接为你提供一个完整的、用多种编程语言实现的购物商城代码。但是,我可以为你概述如何使用几种流行的编程语言(如Python, JavaScript/Node.js, J</a> <span class="text-muted">NewmanEdwarda2</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/node.js/1.htm">node.js</a> <div>Python(后端,使用Flask或Django)Flask示例(后端API)gjijg.compythonfromflaskimportFlask,request,jsonifyapp=Flask(name)假设的数据库商品列表products=[{“id”:1,“name”:“苹果”,“price”:10.0},{“id”:2,“name”:“香蕉”,“price”:5.0},]@app.ro</div> </li> <li><a href="/article/1834739283336982528.htm" title="Django 安装指南" target="_blank">Django 安装指南</a> <span class="text-muted">lly202406</span> <a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>Django安装指南Django是一个高级的PythonWeb框架,它鼓励快速开发和干净、实用的设计。本指南将详细介绍如何在不同的操作系统上安装Django,包括Windows、macOS和Linux。在Windows上安装Django先决条件Python:Django要求Python3.8或更高版本。可以从Python官网下载适用于Windows的Python安装程序。pip:Python的包管</div> </li> <li><a href="/article/1834573267847966720.htm" title="基于Python爬虫四川成都二手房数据可视化系统设计与实现(Django框架) 研究背景与意义、国内外研究现状_django商品房数据分析论文(1)" target="_blank">基于Python爬虫四川成都二手房数据可视化系统设计与实现(Django框架) 研究背景与意义、国内外研究现状_django商品房数据分析论文(1)</a> <span class="text-muted">莫莫Android开发</span> <a class="tag" taget="_blank" href="/search/%E4%BF%A1%E6%81%AF%E5%8F%AF%E8%A7%86%E5%8C%96/1.htm">信息可视化</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E7%88%AC%E8%99%AB/1.htm">爬虫</a> <div>3.国外研究现状在国外,二手房数据可视化也是一个热门的研究领域。以美国为例,有很多公司和网站提供了专门的二手房数据可视化工具,如Zillow、Redfin等。这些工具通常提供房价趋势图、房价分布图、房源信息等功能,帮助用户更好地了解房市动态。综上所述,虽然国内外在二手房数据可视化方面已经有了一些研究成果,但对于四川成都地区的二手房市场还没有相关的研究和可视化系统。因此,本研究旨在设计并实现一个基于</div> </li> <li><a href="/article/1834551847789096960.htm" title="django中F()和Q()的用法" target="_blank">django中F()和Q()的用法</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>django中F()和Q()的用法一、F()表达式的详细介绍1.基本概念:F()表达式用于引用模型中的字段,允许你直接在数据库层面进行计算、比较或更新操作,而不需要将数据加载到Python内存中进行操作。这样可以提高性能,避免racecondition(数据竞争)。2.常见用法:1.字段间运算:你可以使用F()对同一记录中的不同字段进行运算。比如,你有一个模型Product,其中包含price和d</div> </li> <li><a href="/article/1834550710818140160.htm" title="Django 开发实战 2-2 模型 -创建模型类" target="_blank">Django 开发实战 2-2 模型 -创建模型类</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/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/1.htm">项目实战</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/ubuntu/1.htm">ubuntu</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C/1.htm">网络</a> <div>python开发实战-创建模型类一、介绍:二、根据迁移文件生成映射书库据表。三、查看数据库是否根据牵引文件的需求生成数据库,因此返回终端去连接`filmdatabase`数据库。四、最后,了解一些数据库的知识说明。一、介绍:  模型类被创建在"应用目录/models.py"文件中。模型类必须继承自Model类,位于包dango.db.models中。接下来首先以"影片-人物"管理为例进行演示。1定</div> </li> <li><a href="/article/1834550457662533632.htm" title="Vue + Django的人脸识别系统" target="_blank">Vue + Django的人脸识别系统</a> <span class="text-muted">DXSsssss</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/DRF/1.htm">DRF</a><a class="tag" taget="_blank" href="/search/tensorflow/1.htm">tensorflow</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB/1.htm">人脸识别</a> <div>最近在研究机器学习,刚好最近看了vue+Djangodrf的一些课程,学以致用,做了一个人脸识别系统。项目前端使用Vue框架,用到了elementui组件,写起来真是方便。比之前传统的dtl方便了太多。后端使用了drf,识别知识刚开始打算使用opencv+tensorflow,但是发现吧识别以后的结果返回到浏览器当中时使用opencv比较麻烦(主要是我太菜,想不到比较好的方法),因此最终使用了tf</div> </li> <li><a href="/article/1834550454785241088.htm" title="Django 创建好的模块怎么在后台显示" target="_blank">Django 创建好的模块怎么在后台显示</a> <span class="text-muted">u010373106</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/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/sqlite/1.htm">sqlite</a> <div>1、配置模型及其需要显示的数据刚才创建好的tests的增删改查,在后台是不显示的,所以需要进行配置,在刚才创建好的模块里找到admin.py文件,在里面进行如下配置fromdjango.contribimportadminfrom.importmodelsfrom.modelsimportTests#Registeryourmodelshere.classTestsAdmin(admin.Mode</div> </li> <li><a href="/article/1834541635233476608.htm" title="【django】创建模型类(已更新)" target="_blank">【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/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/ORM%E6%A1%86%E6%9E%B6/1.htm">ORM框架</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>ORM框架一、创建模型类二、字段类型说明三、字段选项说明四、外键五、迁移六、添加测试数据a、数据库:需要提前手动创建数据库b、数据表:与ORM框架中的模型类一一对应c、字段:模型类中的类属性(Field子类)d、记录:类似于模型类的多个实例一、创建模型类模型类创建在应用目录/models.py文件中。模型类必须继承Model类,位于包django.db.models中。接下来首先以“影片-人物”管</div> </li> <li><a href="/article/1834535205646462976.htm" title="Django+Vue基于OpenCV的人脸识别系统的设计与实现" target="_blank">Django+Vue基于OpenCV的人脸识别系统的设计与实现</a> <span class="text-muted">赵广陆</span> <a class="tag" taget="_blank" href="/search/project/1.htm">project</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/opencv/1.htm">opencv</a> <div>目录1项目介绍2项目截图3核心代码3.1需要的环境3.2Django接口层3.3实体类3.4config.ini3.5启动类3.5Vue4数据库表设计5文档参考6计算机毕设选题推荐7源码获取1项目介绍博主个人介绍:CSDN认证博客专家,CSDN平台Java领域优质创作者,全网30w+粉丝,超300w访问量,专注于大学生项目实战开发、讲解和答疑辅导,对于专业性数据证明一切!主要项目:javaweb、</div> </li> <li><a href="/article/1834400467820572672.htm" title="Django核心面试题" target="_blank">Django核心面试题</a> <span class="text-muted">闲人编程</span> <a class="tag" taget="_blank" href="/search/%E7%A8%8B%E5%BA%8F%E5%91%98%E9%9D%A2%E8%AF%95/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/%E9%9D%A2%E8%AF%95/1.htm">面试</a> <div>Django核心面试题Django核心面试题Django核心面试题1.Django的MVT架构是什么?2.如何创建Django项目和应用?3.DjangoORM是什么?4.什么是Django的middleware?举例说明。5.Django中的静态文件如何处理?6.如何定义Django模型?7.什么是Django的QuerySet?如何使用?8.如何进行Django的数据库迁移?9.Django中</div> </li> <li><a href="/article/1834367946206572544.htm" title="Django——多apps目录情况下的app注册" target="_blank">Django——多apps目录情况下的app注册</a> <span class="text-muted">叫我DPT</span> <a class="tag" taget="_blank" href="/search/Django%E6%A1%86%E6%9E%B6%E7%9F%A5%E8%AF%86%E7%82%B9/1.htm">Django框架知识点</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>文章目录多apps目录下的app注册方式1-添加python导包路径方式2-修改AppConfig类名多apps目录下的app注册方式1-添加python导包路径importsyssys.path.insert(0,str(BASE_DIR/"apps"))print(sys.path)INSTALLED_APPS=['django.contrib.admin','django.contrib.a</div> </li> <li><a href="/article/1834367819840581632.htm" title="Django项目中log日志的配置" target="_blank">Django项目中log日志的配置</a> <span class="text-muted">叫我DPT</span> <a class="tag" taget="_blank" href="/search/Django%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6/1.htm">Django项目中常用组件</a><a class="tag" taget="_blank" href="/search/%E8%84%9A%E6%9C%AC/1.htm">脚本</a><a class="tag" taget="_blank" href="/search/%E5%8A%9F%E8%83%BD/1.htm">功能</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/logging/1.htm">logging</a> <div>文章目录日志配置模板日志处理器Handlers日志配置模板#日志LOGGING={'version':1,#使用的日志模块的版本,目前官方提供的只有版本1,但是官方有可能会升级,为了避免升级出现的版本问题,所以这里固定为1'disable_existing_loggers':False,#是否禁用其他的已经存在的日志功能?肯定不能,有可能有些第三方模块在调用,所以禁用了以后,第三方模块无法捕获自身</div> </li> <li><a href="/article/1834285986738171904.htm" title="Django:Python高级Web框架详解及参数设置" target="_blank">Django:Python高级Web框架详解及参数设置</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/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%89%8D%E7%AB%AF/1.htm">前端</a> <div>Django是一个高级的PythonWeb框架,它鼓励快速开发和简洁实用的设计。Django遵循MVC设计模式,提供了一套完整的解决方案,用于构建复杂的、数据库驱动的网站。Django的主要特点自动管理数据库:通过ORM(对象关系映射)自动管理数据库。自动生成站点地图:支持搜索引擎优化(SEO)。用户身份认证:内置用户认证系统。中间件支持:强大的中间件支持,可以处理请求和响应。跨站请求伪造(CSR</div> </li> <li><a href="/article/1834190698161729536.htm" title="Python轻量级ORM框架——peewee" target="_blank">Python轻量级ORM框架——peewee</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/python/1.htm">python</a> <div>这里写目录标题Python中常用的ORM框架peewee使用经验从数据库中导出模型查询统计类查询优化子查询参考文章Python中常用的ORM框架SQLALchemy:重量级框架,适合企业级开发,容易与任意Web框架集成,学习成本更高DjangoORM:Django框架中内嵌的ORM模块,易学易用,只适合Django项目使用peewee:轻量级框架,适合个人开发,容易与任意Web框架集成,API类似</div> </li> <li><a href="/article/1833937451014516736.htm" title="Ubuntu系统nginx和uwsgi常用命令(部署网站相关命令)" target="_blank">Ubuntu系统nginx和uwsgi常用命令(部署网站相关命令)</a> <span class="text-muted">我不是大佬zvj</span> <a class="tag" taget="_blank" href="/search/ubuntu/1.htm">ubuntu</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a> <div>以下是我自己在学习使用Ubuntu操作系统部署pythonflask和django网站时常用的命令,作为学习笔记,记录一下,写的比较简单,还有啥需要补充的话欢迎留言。路径相关#进入项目路径cd /var/www/html/Podcaster#nginx配置文件路径‏/etc/nginx/sites-enabled/‌‍default防火墙#查看已经开放的端口sudoufwstatus#打开端口su</div> </li> <li><a href="/article/1833814523505635328.htm" title="2025毕业设计指南:如何用Hadoop构建超市进货推荐系统?大数据分析助力精准采购" target="_blank">2025毕业设计指南:如何用Hadoop构建超市进货推荐系统?大数据分析助力精准采购</a> <span class="text-muted">计算机编程指导师</span> <a class="tag" taget="_blank" href="/search/Java%E5%AE%9E%E6%88%98%E9%9B%86/1.htm">Java实战集</a><a class="tag" taget="_blank" href="/search/Python%E5%AE%9E%E6%88%98%E9%9B%86/1.htm">Python实战集</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AE%9E%E6%88%98%E9%9B%86/1.htm">大数据实战集</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/hadoop/1.htm">hadoop</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/1.htm">数据分析</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/boot/1.htm">boot</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E8%BF%9B%E8%B4%A7/1.htm">进货</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>✍✍计算机编程指导师⭐⭐个人介绍:自己非常喜欢研究技术问题!专业做Java、Python、小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。⛽⛽实战项目:有源码或者技术上的问题欢迎在评论区一起讨论交流!⚡⚡Java实战|SpringBoot/SSMPython实战项目|Django微信小程序/安卓实战项目大数据实战项目⚡⚡文末获取源码文章目录⚡⚡文末获取源码基于hadoop的超市进货推荐系</div> </li> <li><a href="/article/1833706099132428288.htm" title="Python Web 框架篇:Flask、Django、FastAPI介绍及其核心技术" target="_blank">Python Web 框架篇:Flask、Django、FastAPI介绍及其核心技术</a> <span class="text-muted">Switch616</span> <a class="tag" taget="_blank" href="/search/Python/1.htm">Python</a><a class="tag" taget="_blank" href="/search/Web/1.htm">Web</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/flask/1.htm">flask</a><a class="tag" taget="_blank" href="/search/fastapi/1.htm">fastapi</a><a class="tag" taget="_blank" href="/search/django/1.htm">django</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a> <div>PythonWeb框架篇:Flask、Django、FastAPI介绍及其核心技术目录FlaskFlask核心概念(路由、视图函数、模板渲染)FlaskBlueprint模块化应用Flask扩展(Flask-SQLAlchemy、Flask-WTF、Flask-Migrate等)DjangoDjango项目架构DjangoORM及模型定义Django中间件Django信号机制DjangoForm和</div> </li> <li><a href="/article/1833702066061668352.htm" title="python selenium post,是否可以在Selenium中捕获POST数据?" target="_blank">python selenium post,是否可以在Selenium中捕获POST数据?</a> <span class="text-muted">weixin_39600328</span> <a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/selenium/1.htm">selenium</a><a class="tag" taget="_blank" href="/search/post/1.htm">post</a> <div>I'mworkingwiththeSeleniumWebDriverToolandamwonderingifthistoolprovidesameansforcapturingthePOSTdatageneratedwhensubmittingaform.I'musingthedjangotestframeworktotestthatmydataisprocessedcorrectlyontheb</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>