碧玉妆成一树高,万条垂下绿丝绦。 不知细叶谁裁出,二月春风似剪刀。

    

原文尽在:http://djangobook.com/ 转载请注明出处:http://www.cnblogs.com/A-FM/p/6721382.html

Django Views and URLconfs(Django的视图和URL配置)

In the previous chapter, I explained how to set up a Django project and run the Django development server. In this chapter, you’ll learn the basics of creating dynamic Web pages with Django.(在前面的章节,我解释了怎样去创建一个Django工程和运行Django开发服务器,在这一节,在这一节你会学到创建Django的Web页面的基本语法。)

Your First Django-Powered Page: Hello World(你的第一个Django:Hello World)

As our first goal, let’s create a Web page that outputs that famous example message: “Hello world.” If you were publishing a simple “Hello world” Web page without a Web framework, you’d simply type “Hello world” into a text file, call it “hello.html", and upload it to a directory on a Web server somewhere. Notice in that process you’ve specified two key pieces of information about that Web page: its contents (the string "Hello world") and its URL (e.g. http://www.example.com/hello.html). With Django, you specify those same two things, but in a different way. The contents of the page are produced by a view function, and the URL is specified in a URLconf. First, let’s write our “Hello world” view function.(作为我们的第一个目标,让我们创建一个Web page 输出著名的信息“Hello World”,如果你已经做过一个简单的“Hello World” Web page 并且没有使用Web框架,你应该只是简单的把“Hello world”输入你的文本文件,命名为“hello.html”,并且上传他到任何一个Web 服务器的目录,注意在这个工程中你已经制定两个关键的信息关于这个Web page :他包括这个字符串“Hello World”,and他的url 例如http://www.example.com/hello.html,用Django的话你会指定这两个相同的事情,但是是用不同的方法,他会包括这个页面在视图函数,and 这个URL在URL配置中制定,让我们写一个自己的“Hello World”视图函数吧!

Your First View(你的第一份视图)

Within the mysite directory that we created in the last chapter, create an empty file called views.py.
This Python module will contain our views for this chapter. Our “Hello
world” view is simple. Here’s the entire function, plus import
statements, which you should type into the views.py file:( 在我们的mysite目录里面就是我们上一节最后创建的那个,创建一个空目录命名为views.py。Python的模块将会包含我们这一节的视图。我们的“Hello World”是非常简单的。这里是完整的函数和import 状态,你需要将他输入到views.py里面。)

from django.http import HttpResponse  

def hello(request):
return HttpResponse("Hello world")

Let’s step through this code one line at a time:(现在让我们一步步的分析这个代码)

  • First, we import the class HttpResponse, which lives in the django.http module. We need to import this class because it’s used later in our code.(首先我们import了 这个HttpResponse累,他就在django.http 模块里面我们需要import这个模块因为我们一会会用到它。)
  • Next, we define a function called hello– the view function.(然后我们定义了一个名为hello 的视图函数。)
  • Each view function takes at least one parameter, called request by convention. This is an object that contains information about the current Web request that has triggered this view, and is an instance of the class django.http.HttpRequest.(每个视图至少要有一个参数,习惯上称为request。这个对象包含了当前Web request信息,这是一个触发试图,他是django.http.HttpRequest的一个实例函数----其实这个我翻译不清楚)

In this example, we don’t do anything with request, but it must be the first parameter of the view nonetheless. Note that the name of the view function doesn’t matter; it doesn’t have to be named in a certain way in order for Django to recognize it. We’re calling it hello here, because that name clearly indicates the gist of the view, but it could just as well be named hello_wonderful_beautiful_world, or something equally revolting. The next section, “Your First URLconf”, will shed light on how Django finds this function.(在这个例子里面,我们对request什么都没做,但是他必须是第一个参数在这个视图里面,注意这个视图函数的名字是不重要的,他不需要特意的以某种方式命名才能让Django识别他。我们在这里称他为 hello 因为这个名字清楚的表明这个函数主旨,当然他也可以被命名为 hello_wonderful_beautiful_world ,或者是同样令人讨厌的其他名字,下一部分“你的第一个URL配置” 将会告诉你如何让django 发现这个函数。   shed light 不会翻译。。)。

The function is a simple one-liner: it merely returns an HttpResponse object that has been instantiated with the text "Hello world".(这个函数只有简单的一行:他仅仅返回了一个用“Hello World”实例化的一个HttpResponse 对象)

The main lesson here is this: a view is just a Python function that takes an HttpRequest as its first parameter and returns an instance of HttpResponse. In order for a Python function to be a Django view, it must do these two things. (There are exceptions, but we’ll get to those later.) (在这里最重要的一课是:一个视图就是一个Python函数,他接收一个HttpRequest作为第一个参数,并且返回一个HttpResponse的实例化对象。为了让一个Python function函数成为一个 django view 他必须做这两件事情,他又例外,但是我们会在以后去接触它。)

Your First URLconf(你的第一个URL配置)

If, at this point, you ran python manage.py runserver again, you’d still see the “Welcome to Django” message, with no trace of our “Hello world” view anywhere. That’s because our mysite
project doesn’t yet know about the hello view; we need to
tell Django explicitly that we’re activating this view at a particular
URL. Continuing our previous analogy of publishing static HTML files, at
this point we’ve created the HTML file but haven’t uploaded it to a
directory on the server yet.(如果现在你运行python manage.py runserver,你应该会依然看到 “Welcome to Django”信息, 没有一点我们“hello world”的踪迹,这是因为我们的mysite工程还是不知道关于 hello 试图的信息;我们需要明确的告诉告诉 Django 我们声明了这个URL,继续类别我们以前的静态HTML 文件,现在我们创建了这个HTMl 文件但是我们没有 上传他到服务器目录中。---意思就是和以前的笨方法类比的话我们还缺一步)

To hook a view function to a particular URL with Django, we use a URLconf.
A URLconf is like a table of contents for your Django-powered Web site.
Basically, it’s a mapping between URLs and the view functions that
should be called for those URLs. It’s how you tell Django, “For this
URL, call this code, and for that URL, call that code. (为了将这个视图函数的URL和Django 挂钩,我们需要用URL 配置。)

For example, when somebody visits the URL /foo/, call the view function foo_view(), which lives in the Python module views.py. When you executed django-admin startproject in the previous chapter, the script created a URLconf for you automatically: the file urls.py.(例如当某人访问 URL /foo/,称这个 视图函数为foo_view(),他就在Python的 views.py 模块里面。在上一节我们执行 django-admin startproject 的时候,该脚本自动为你创建了一个URL 配置:urls.py)

By default, it looks something like this: (一般来说,他也就长的这个吊样。)

"""mysite URL Configuration

The urlpatterns list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]

If we ignore the documentation comments at the top of the file, here’s the essence of a URLconf:  (如果我们不看文件最上面的 文档说明的话,这里就是URL配置文件的本质。)

from django.conf.urls import include, url
from django.contrib import admin urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]

Let’s step through this code one line at a time:(现在让我们一步步的分析这些代码)

  • The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.(第一行导入了两个函数 从 django.conf.urls模块:include他允许你include一个完整的python import 路径到另一个 URL 配置模块,and url 用正则表达式去匹配你的浏览器请求在你的Django 工程里面)
  • The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.(第二行 叫做admin的函数从django.contrib模块导入,这个函数被include 导入到url中,用于Django的管理员站点。)
  • The third line is urlpatterns – a simple list of url() instances.(第三行urlpatterns 一个简单的url 列表实例化)

The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here’s how to hook in our hello view:(这里最重要的东西就是urlpatterns 这个变量,哪里有Django中你期望发现的URL配置模块,这个变量用url和正则表达式代码写成map的格式去处理url、如果要添加url 视图,只需要添加一个map视图用URL正则表达式的方法,这里是如何添加我们hello的实例。)

from django.conf.urls import include, url
from django.contrib import admin
from mysite.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', hello),
]

We made two changes here:(我们在这里做出来两处改变)

  • First, we imported the hello view from its module – mysite/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite/views.py is on your Python path.)(首先我们导入了hello视图函数从 mysite/views.py 里面,他在Python的语法里面被翻译成mysite.views,(这里嘉定mysite/views.py是你的Python路径))
  • Next, we added the line url(r'^hello/$', hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the url that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well, which we’ll cover in more depth in Chapter 7.(接下来,我么添加了一行url(r'^hello/$', hello),urlpatterns. 这一行被添加作为URLpattern,url()函数告诉Django如何处理这个url用你的配置。第一个参数是正则表达式字符串,and第二个参数是视图函数用于模型---实在翻译不懂。。url()也可以用其他可选的参数,我们将在第七节深入讲解。)

One more important detail we’ve introduced here is that ‘r’ character in front of the regular expression string. This tells Python that the string is a “raw string” – its contents should not interpret backslashes.(一个很重要的细节我们要在这里进行讲解,就是在正则表达式前面的“r”字符,他告诉Python这个字符串是“未加工字符串”,他的内容不应该解释反斜杠)

In normal Python strings, backslashes are used for escaping special characters – such as in the string “\n”, which is a one-character string containing a newline. When you add the r to make it a raw string, Python does not apply its backslash escaping – so, “r'\n'” is a two-character string containing a literal backslash and a lowercase “n”. (一般来说Python的字符串反斜杠用于解释一些特殊的字符,例如“\n”,这是 一个字符 -回车 。当你添加r 让他成为 未加工的字符串的时候,Python不会用反斜杠去进行转义,因此 r“\n” 是两个字符串也就是字面上的意思,包含一个反斜杠和一个字符“n”)

There’s a natural collision between Python’s usage of backslashes and the backslashes that are found in regular expressions, so it’s best practice to use raw strings any time you’re defining a regular expression in Django.(这是自然的反斜杠在Python之中的用法,所以最好在练习的时候就用原始字符串,当你在Django中定义正则表达式的时候。)

In a nutshell, we just told Django that any request to the URL /hello/ should be handled by the hello view function.(简而言之,我们仅仅告诉Django所有的/hello/一类的URL请求 都让hello函数去完成)

It’s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL /hello/, the pattern looks a bit different than that. Here’s why:(讨论一下URLpattern的语法是非常有意义的,因为他不是显而易见的,尽管我们希望匹配到URL /hello/ ,但是这个公式看上去有那么一点点的差别,下面是为什么。)

  • Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn’t include the leading slash in /hello/.
    At first, this may seem unintuitive, but this requirement simplifies
    things – such as the inclusion of URLconfs within other URLconfs, which
    we’ll cover in Chapter 7.(Django移除了每个URL前面的斜杠当检查URLpattern的时候,这意味着我们的URLpattern不包括开头的斜杠在/hello/前面。这样看起来可能会不是很直观,但是这样的要求简化了许多事情---例如URL模式内嵌,我们将会在第七节详细讲解。)
  • The pattern includes a caret (^) and a dollar sign ($). These are
    regular expression characters that have a special meaning: the caret
    means “require that the pattern matches the start of the string,”
    and the dollar sign means “require that the pattern matches the end of the string.” (模式包含了一个脱字符和一个dollar符,这些都是正字表达式字符,他们有特殊的意义:脱字符表示 从表达式头部开始匹配,dollar表示这是匹配的结尾)

This concept is best explained by example.(这个概念最好用例子来解释)
If we had instead used the pattern ^hello/ (without a dollar sign at the end), then any URL starting with /hello/ would match, such as /hello/foo and /hello/bar, not just /hello/.Similarly, if we had left off the initial caret character (i.e., hello/$), Django would match any URL that ends with hello/, (如果我们用这一种^hello/模式的话 (结尾没有dollar 符号),所有以 /hello/开头的字符串都会被匹配到 --- 例如 /hello/foo and /hello/bar,不仅仅只有/hello/。同样的,如果左边没有脱字符 例如 hello/$,Django将会匹配任何以hello/结尾的网址。
such as /foo/bar/hello/.If we had simply used hello/, without a caret or dollar sign, then any URL containing hello/ would match, such as /foo/hello/bar.Thus, we use both the caret and dollar sign to ensure that only the URL /hello/ matches
– nothing more, nothing less. Most of your URLpatterns will start with
carets and end with dollar signs, but it’s nice to have the flexibility
to perform more sophisticated matches.(例如/foo/bar/hello/,如果我们简单的用hello/ 没有脱字符和dollar符号,然后只要URL中包含 /hello的话就会被匹配,例如/foo/hello/bar 。因此我们同时使用 脱字符和dollar符 用以确定不多不少只匹配/hello/。你的大部分URLpattern都会以脱字符开始,dollar结束,但是他会很漂亮的处理复杂的匹配式)

You may be wondering what happens if someone requests the URL /hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn’t match a URLpattern and doesn’t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).(你可能会觉得疑惑如果某一个request是 /hello 没有斜杠结尾的话 会发生什么?因为我们的URLpattern 需要 以斜杠结尾,所以这话个URL无法匹配,然而,默认情况下对尾部没有斜杠的任何URL将会重定向带相同的带有斜杠的URL那里。这个规则是被Django的setting 文件中的APPEND_SLASH 决定的不喜欢的话可以自己更改。)

The other thing to note about this URLconf is that we’ve passed the hello
view function as an object without calling the function. This is a key
feature of Python (and other dynamic languages): functions are
first-class objects, which means you can pass them around just like any
other variables. Cool stuff, eh?(另外需要注意的事情是关于URLconf,我们将hello 函数作为对象传递 而不是作为函数进行调用。这是Python很关键的特色(和其他动态语言的),函数是一级对象,也就是说你可以传递他们就像传递其他变量那样。酷吧?)

To test our changes to the URLconf, start the Django development server, as you did in Chapter 1, by running the command python manage.py runserver.
(If you left it running, that’s fine, too. The development server
automatically detects changes to your Python code and reloads as
necessary, so you don’t have to restart the server between changes.) The
server is running at the address http://127.0.0.1:8000/, so open up a Web browser and go to http://127.0.0.1:8000/hello/. You should see the text “Hello world” – the output of your Django view (Figure 2-1).,(为了测试我们对URLconf的改变,现在开始运行Django开发服务器,正如你在第一节所做的那样运行这个命令 :python manage.py runserver 如果运行成功的话,那就很好,开发服务器会自动检查你的Python代码改变,有需要的话会自动重启开发服务器,所以你不需要自己重启开发服务器在做出改变之后,服务运行在 http://127.0.0.1:8000/  所以打开你的浏览器进入http://127.0.0.1:8000/hello/ 你应该会看到 “Hello World” - 由你的Django视图输出的文本。)

Figure 2-1: Hooray! Your first Django view. (万岁,你第一个Django视图)

Regular Expressions (正则表达式)

Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL matching, you’ll probably only use a few regex symbols in practice. Table 2-1 lists a selection of common symbols.(正则表达式是紧凑的文本指定模式,Django允许任何正则表达式去做强有力的URL 匹配,你可能只会用很少的正则表达式符号去实践,表2-1是一些常见的正则表达式符号。)

Table 2-1: Common regex symbols

Symbol Matches
. (dot) Any single character 任何单一 的字符
\d Any single digit  任何单一的 数字
[A-Z] Any character between A and Z (uppercase)   在A-Z之间任何一个字符(大写的)
[a-z] Any character between a and z (lowercase)    在a-z之间任何一个字符(小写的)
[A-Za-z] Any character between a and z (case-insensitive) 任何一个英文字母,不区分大小写
+ One or more of the previous expression (e.g., \d+ matches one or more digits) 一个或者多个前面的表达 (例如 \d+ 表达匹配一个或者多个数字)
[^/]+ One or more characters until (and not including) a forward slash    一个或者多个不为 /的字符
? Zero or one of the previous expression (e.g., \d? matches zero or one digits)  一个或者零个 前面的字符
* Zero or more of the previous expression (e.g., \d* matches zero, one or more than one digit)  没有或者多个前面的字符
{1,3} Between one and three (inclusive) of the previous expression (e.g., \d{1,3} matches one, two or three digits) 1-3 之间个前面的字符。

For more on regular expressions, see the Python regex documentation.

A Quick Note About 404 Errors  (404错误的快速参考)

At this point, our URLconf defines only a single URLpattern: the one that handles requests to the URL /hello/. What happens when you request a different URL? To find out, try running the Django development server and visiting a page such as http://127.0.0.1:8000/goodbye/. You should see a “Page not found” message (Figure 2-2). Django displays this message because you requested a URL that’s not defined in your URLconf.(这时候我们的URLconf近定义一个URLpattern:一个处理hello请求的函数,当我们访问一个没有定义的URL的时候会发生什么呢?为了解决这个问题,我们运行开发服务器并且访问一个网址例如:http://127.0.0.1:8000/goodbye/. 你应该会看到“page not found” 信息 (图中的2-2)Django展示这个信息因为您并没有在视图函数中定义这个URLconf)

Figure 2-2: Django’s 404 page

The utility of this page goes beyond the basic 404 error message. It also tells you precisely which URLconf Django used and every pattern in that URLconf. From that information, you should be able to tell why the requested URL threw a 404.(这个页面实际上已经超过了普通的404错误信息(给你很多可以Debug的信息),他精确的告诉了你Django的每一个URLconf 的信息。从信息中可以知道,你应该已经知道了为什么他会抛出404错误信息)

Naturally, this is sensitive information intended only for you, the Web developer. If this were a production site deployed live on the Internet, you wouldn’t want to expose that information to the public. For that reason, this “Page not found” page is only displayed if your Django project is in debug mode.(当然,这个敏感的信息只应该展现给你,作为Web的开发者,如果要将这个站点放到因特网上,你一定不想暴露出这个信息给大众,这个“Page not found”页面,只在Django工程Debug的时候在展现出来)

I’ll explain how to deactivate debug mode later. For now, just know that every Django project is in debug mode when you first create it, and if the project is not in debug mode, Django outputs a different 404 response.(我一会会解释如何禁用这个Debug模式,现在只需要知道每一个新建的Django peoject 都时在Debug模式下面的。and 如果该peoject不再Dubug模式下面,Django会输出一个不一样的404响应)

A Quick Note About the Site Root(网站根目录相关快速查询手册)

As explained in the last section, you’ll see a 404 error message if you view the site root – http://127.0.0.1:8000/. Django doesn’t magically add anything to the site root; that URL is not special-cased in any way.(正如在文章结尾处看到的一样,你会看到404错误信息,如果你访问网站根目录 – http://127.0.0.1:8000/. Django不会添加任何信息到网站根目录,URL任何时候不会出处于特殊情况。)

It’s up to you to assign it to a URLpattern, just like every other entry in your URLconf. The URLpattern to match the site root is a bit unintuitive, though, so it’s worth mentioning.(这完全取决于你,就像其他的网站URL入口一样,这个用于匹配网站根目录的URLpattern有一点点的不直观因此我们有必要去考虑一下它)

When you’re ready to implement a view for the site root, use the URLpattern “^$”,(当你准备实现一个网站根目录的视图的时候,就用这个URLpattern ---- “^$”)
which matches an empty string. For example:(他将匹配所有的空字符串,例如:)

from mysite.views import hello, my_homepage_view

urlpatterns = [
url(r'^$', my_homepage_view),
# ...

How Django Processes a Request (Django 是如何处理一个请求的呢?)

Before continuing to our second view function, let’s pause to learn a little more about how Django works. Specifically, when you view your “Hello world” message by visiting http://127.0.0.1:8000/hello/ in your Web browser, what does Django do behind the scenes? It all starts with the settings file.(在我们开始我们第二个视图函数之前,让我们暂停一下去学习一点关于Django的工作流程。具体地说,当你通过浏览器访问http://127.0.0.1:8000/hello/看你的“Hello world”信息的时候,Django在后台做了些什么呢?----所有的一切都开始于那个setting file)

When you run python manage.py runserver, the script looks for a file called settings.py in the inner mysite directory. This file contains all sorts of configuration for this particular Django project,(当你运行python manage.py runserver的时候,Django会去mysite 目录里面寻找一个叫做setting.py 的文件,这个文件包含了各种各样的Django配置信息。)
all in uppercase: TEMPLATE_DIRS, DATABASES, etc. The most important setting is called ROOT_URLCONF. ROOT_URLCONF tells Django which Python module should be used as the URLconf for this Web site.(都是大写的: TEMPLATE_DIRS, DATABASES, etc.最重要的设置被称为ROOT_URLCONF,他告诉Django那个Python的模块应该被用作URLconf作为她的Web站点)

Remember when django-admin startproject created the files settings.py and urls.py? The auto-generated settings.py contains a ROOT_URLCONF setting that points to the auto-generated urls.py.
Open the settings.py file and see for yourself; it should look like this: (还记得当我们创建工程的时候,自动生成的文件s settings.py and urls.py么? 这个自动生成的settings.py 包含ROOT_URLCONF用于指向自动生成的urls.py)

ROOT_URLCONF = 'mysite.urls'

This corresponds to the file mysite/urls.py. When a request comes in for a particular URL – say, a request for /hello/ – Django loads the URLconf pointed to by the ROOT_URLCONF setting. Then it checks each of the URLpatterns in that URLconf, in order, comparing the requested URL with the patterns one at a time, until it finds one that matches.(对应的文件是 mysite/urls.py,当请求进入一个特定的URL - 例如,一个对/hello/的请求,Django找到setting中的ROOT_URLCONF指向的文件,然后检查每个文件中的URLpattern,将其中的URL和request URL对比,知道发现一个匹配的。

When it finds one that matches, it calls the view function associated with that pattern, passing it an HttpRequest object as the first parameter. (We’ll cover the specifics of HttpRequest later.) As we saw in our first view example, a view function must return an HttpResponse. (当找到匹配的是开始调用相对应的 视图函数将HttpRequest作为第一个对象(我们稍后详细讲解HttpRequest)正如我们第一次看到的视图函数,一个试图函数必须返回一个HttpResponse。)

Once it does this, Django does the rest, converting the Python object to a proper Web response with the appropriate HTTP headers and body (i.e., the content of the Web page). In summary:(一旦这样,Django就可以休息了,然后转换Python对象到适当的Web response(一个Web页面)总而言之。)

  1. A request comes in to /hello/. (请求进入转入 /hello/)
  2. Django determines the root URLconf by looking at the ROOT_URLCONF setting. (Django 通过ROOT_URLCONF设置来确定 根URLconf)
  3. Django looks at all of the URLpatterns in the URLconf for the first one that matches /hello/. (Django 在所有的 URLpattern中寻找第一个和 /hello/ 匹配的URLconf)
  4. If it finds a match, it calls the associated view function. (如果发现了匹配的URLconf,就调用对应的视图函数)
  5. The view function returns an HttpResponse. (视图函数返回一个 HttpResponse)
  6. Django converts the HttpResponse to the proper HTTP response, which results in a Web page. (Django 转化HttpResponse 到 Http response,也就是一个网页的页面)

You now know the basics of how to make Django-powered pages. It’s quite simple, really – just write view functions and map them to URLs via URLconfs. (你现在应该知道怎样去用Django 做一个基本的页面了,他很简单,只需要写视图函数和 URLs 并通过 URLconfs 将其 map)

Django Views and URLconfs的更多相关文章

  1. Django views 中 View decorators

    decorators(装饰器) 1. require_http_methods 在django.views.decorators.http中,可以用来限制请求的权限. require_http_met ...

  2. django views视图函数返回值 return redirect httpresponse总结

    django views视图函数返回值 return redirect  render httpresponse总结

  3. django views视图函数

    Django views.py视图文件 一. 创建views.py文件,在工程文件夹根目录创建views.py视图文件,其实任意文件名都可以,使用views是为了遵循传统. 注:所有的views函数都 ...

  4. Django Views Decorator

    Django的试图函数的装饰器主要有: HTTP请求方法 条件视图处理 GZip压缩 改变页眉 缓存 官网文档 HTTP请求方法 该装饰器是设置允许访问HTTP协议的方法,装饰器在django.vie ...

  5. Django—views系统:views基础

    Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...

  6. Django~Views

    In Django, web pages and other content are delivered by views. To get from a URL to a view, Django u ...

  7. Django Views: Dynamic Content

    世味年来薄似纱,谁令骑马客京华. 小楼一夜听春雨,深巷明朝卖杏花. 矮纸斜行闲作草,晴窗细乳戏分茶. 素衣莫起风尘叹,犹及清明可到家. Your Second View: Dynamic Conten ...

  8. Django -- Views and URLconf

    1 创建工程 django-admin startproject mysite && cd mysite 2 创建应用 python manage.py startapp blog 3 ...

  9. Django Views(视图函数)

    http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpR ...

随机推荐

  1. 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结

    史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...

  2. nginx-伤心的事

    今天测试接口,总是出问题,测试了两天,整个流程就卡在最后一步. 每次采用curl,request等请求访问我的服务器都会返回403状态码,网上找了很多资料 有权限的,有静态文件的,然而很多都没有什么卵 ...

  3. linux led子系统(一)

    就像学编程第一个范例helloworld一样,学嵌入式,单片机.fpga之类的第一个范例就是点亮一盏灯.对于庞大的linux系统,当然可以编写一个字符设备驱动来实现我们需要的led灯,也可以直接利用g ...

  4. C语言socket send()数据缓存问题

    send()函数默认情况下会使用Nagle算法.Nagle算法通过将未确认的数据存入缓冲区直到积攒到一定数量一起发送的方法.来降低主机发送零碎小数据包的数目.所以假设send()函数发送数据过快的话, ...

  5. sendmessage传递数组

    1.在初始化数组尤其是需要每次都初始化的时候,很多同学使用循环来进行,这样不但速度慢,而且写起来也很长.所以现在提供一个函数来实现这个功能... 原型:extern void *memset(void ...

  6. javascript模块化编程:CommonJS和AMD规范

    AMD规范,异步模块定义.与CommonJS规范齐名并列. 作用都是利于JavaScript的模块化编程. 模块化编程的好处就是: 1.可重用 2.独立 3.能解决加载的依赖性问题 4.能解决重复加载 ...

  7. Axure Base 09 带遮罩层的弹出框

    示例原型下载:小楼Axure原创元件-带遮罩层的弹出框 实现目标: 1.   点击按钮弹出带遮罩层的对话框: 2.   页面上下左右滚动时,弹出的对话框水平和垂直始终居中. 实现步骤如下: 1. 拖入 ...

  8. 使用双引擎,让kbmmw 的客户端访问更方便(既给浏览器做服务,也给桌面程序做服务)

    前面我们一直都讲了如何使用kbmmw smarthttpservice 给客户端提供REST  服务.主要都是返回给 浏览器访问的,如果我们使用delphi 开发桌面应用,如何使用这些服务呢?其实一切 ...

  9. VC FTP服务器程序分析(一)

    想在QT上移植一个FTP服务器程序,先学习windows下的FTP服务器例子,然后随便动手写点东西. 在pudn上搜索 "FTP服务器端和客户端实现 VC“这几个关键字,就可以搜到下面要分析 ...

  10. WinDbg 在64位系统下转储32位进程

    在64位系统下,首先要判断进程是32位,还是64位 在Win8之前,进程名后带星号(*)则是32位进程.但Win8.1后,则不显示星号.需要选出“平台”列,来确认32位,还是64位. 在64位系统下的 ...