未加星标

Django 1.11官方教程翻译

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

一直以来由于英语废所以极端抵触看英文文档。感觉人还是要逼一下自己,所以就有了这篇翻译。如果有翻译错误的地方欢迎在评论中指正。

Let’s learn by example.

Throughout this tutorial, we’ll walk you through the creation of a basic poll application.

让我们通过样例来学习Django吧。

本教程将通过一个简单的投票应用来让您熟悉Django。

It’ll consist of two parts:

A public site that lets people view polls and vote in them. An admin site that lets you add, change, and delete polls.

这个项目包括两个部分:

一个对外显示的网站,以供人们投票。 一个管理网站,您可以在其中对结果增删改查。

We’ll assume you haveDjango installedalready. You can tell Django is installed and which version by running the following command:

我们假定你已经 安装Django 了(如果你已经安装了pip,也可以通过pip install Django来安装),你可以通过如下命令来查看你是否安装了Django。

$python -m django --version

If Django is installed, you should see the version of your installation. If it isn’t, you’ll get an error telling “No module named django”.

如果你已经安装了Django,那么你应该会看到Django的版本号,反之,你将会看到错误提示“No module named django”。

This tutorial is written for Django 1.11 and Python 3.4 or later. If the Django version doesn’t match, you can refer to the tutorial for your version of Django by using the version switcher at the bottom right corner of this page, or update Django to the newest version. If you are still using Python 2.7, you will need to adjust the code samples slightly, as described in comments.

本教程适用于Django1.11及以上,python版本要求为python3.4及以上,如果版本不匹配,你可以尝试下载更新版本的Django或者寻找之前版本的教程,如果你使用的是python2.7,那么你可能需要稍微调整你的代码,我们会把改动写在注释中。

Creating a project 创建项目

If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Djangoproject a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

如果这是你第一次使用Django,你需要做一些初始化工作,也就是说我们会自动生成一些代码来帮你建立一个Django 项目 。主要是一些配置信息,包括数据库配置、Django选项和特定的应用程序设置。

From the command line,cdinto a directory where you’d like to store your code, then run the following command:

首先,在命令行切换到你想储存你Django代码的位置,执行接下来的命令:

$django-adminstartprojectmysite

This will create amysitedirectory in your current directory. If it didn’t work, seeProblems running django-admin.

它会在当前目录创建一个名为mysite的文件夹,如果没有,请查看 帮助 。

Note

You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names likedjango(which will conflict with Django itself) ortest(which conflicts with a built-in Python package).

需要注意的是 ,你的项目名称尽量避免和python和Django的某些组件重名,例如django(和Django重名)和test(与python包重名)

Where should this code live?

If your background is in plain oldphp (with no use of modern frameworks), you’re probably used to putting code under the Web server’s document root (in a place such as/var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.

Put your code in some directory outside of the document root, such as/home/mycode.

Django代码应该放在什么地方?

如果你使用的是老式的PHP(没有现代框架)你可能习惯于把代码放在Web服务器根目录下(例如/var/www)。而在Django中不建议你这么做,因为这是一个不好的习惯,因为这增加了人们通过网络直接查看你代码的可能性,这会降低网站的安全性。

把你的代码放在 别的 地方,例如/home/mycode

Let’s look at whatstartprojectcreated:

我们来看看 startproject 创建了什么:

mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py

These files are:

这些文件的含义如下: The outermysite/root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

外部的mysite仅仅是你项目的容器而已,它的名字不会影响到Django的运行,你可以把它改成你喜欢的名字。

py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details aboutmanage.pyindjango-admin and manage.py.

manage.py:是一个供你在命令行使用的管理工具,你可以通过它来管控你的项目,你可以点击 这里 查看更多细节。

The innermysite/directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g.urls).

内部的mysite目录才是你的项目本体,它的名字就是你的包名,如果你需要引用它以及内部的模块,你可以直接import它(例如:mysite.urls)。

mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, readin the official Python docs.

mysite/__init__.py:一个空文件,它的作用只是向python表明这是一个python包。如果你还是不太清楚,建议你先阅读 python文档中包的部分 。

mysite/settings.py: Settings/configuration for this Django project.Django settingswill tell you all about how settings work.

mysite/settings.py:设置/配置这个Django项目,点击 这里 查看配置是如何工作的。

mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs inURL dispatcher.

mysite/urls.py:这是这个项目的url声明,也是你网站的目录,也可以查看 url调度 获取更多细节。

mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. SeeHow to deploy with WSGIfor more details.

mysite/wsgi.py:一个兼容WSGI入口点的Web服务器为您服务。参阅 如何使用WSGI 。

The development server 开发服务器

Let’s verify your Django project works. Change into the outermysitedirectory, if you haven’t already, and run the following commands:

让我们来验证你的服务器能否正常运作吧。切换到外层的mysite目录,输入以下命令:

$pythonmanage.py runserver

You’ll see the following output on the command line:

你将会看到如下输出:

Performingsystemchecks... Systemcheckidentifiednoissues (0 silenced). Youhaveunappliedmigrations; yourappmaynot workproperlyuntiltheyareapplied. Run 'python manage.py migrate' to applythem. October 06, 2016 - 15:50:53 Djangoversion 1.11, usingsettings 'mysite.settings' Startingdevelopmentserverathttp://127.0.0.1:8000/ QuittheserverwithCONTROL-C. Note

Ignore the warning about unapplied database migrations for now; we’ll deal with the database shortly.

注意

我们现在暂时忽略数据库迁移的警告,稍后我们会进行处理。

You’ve started the Django development server, a lightweight Web server written purely in Python. We’ve included this with Django so you can develop things rapidly, without having to deal with configuring a production server such as Apache until you’re ready for production.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Django 1.11官方教程翻译
本站链接:http://www.codesec.net/view/484267.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(34)