登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 电子信息类 > 通信工程 > 正文

利用Django框架开发的书籍协同制作平台外文翻译资料

 2022-07-25 01:07  

Writing your first Django app, part 1

Letrsquo;s learn by example.

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

Itrsquo;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.

Wersquo;ll assume you have Django installed already. You can tell Django is installed and which version by running the following command in a shell prompt (indicated by the $ prefix):

$ python -m django --version

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

This tutorial is written for Django 1.11 and Python 3.4 or later. If the Django version doesnrsquo;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.

See How to install Django for advice on how to remove older versions of Django and install a newer one.

Where to get help:

If yoursquo;re having trouble going through this tutorial, please post a message to django-users or drop by #django on irc.freenode.net to chat with other Django users who might be able to help.

Creating a project

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

From the command line, cd into a directory where yoursquo;d like to store your code, then run the following command:

$ django-admin startproject mysite

This will create a mysite directory in your current directory. If it didnrsquo;t work, see Problems running django-admin.

Note

Yoursquo;ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itself) or test (which conflicts with a built-in Python package).

Where should this code live?

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

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

Letrsquo;s look at what startproject created:

mysite/

manage.py

mysite/

__init__.py

settings.py

urls.py

wsgi.py

These files are:

  • The outer mysite/ root directory is just a container for your project. Its name doesnrsquo;t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name yoursquo;ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If yoursquo;re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • 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 in URL dispatcher.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

The development server

Letrsquo;s verify your Django project works. Change into the outer mysite directory, if you havenrsquo;t already, and run the following commands:

$ python manage.py runserver

Yoursquo;ll see the following output on the command line:

Performing system checks...

System

全文共24571字,剩余内容已隐藏,支付完成后下载完整资料

第一部分,编写你的第一个Django应用程序

让我们通过例子学习。

通过这份教程,我们将带你学习如何创建一个基本的民意调查应用程序。

它由两部分组成:

  1. 一个能让人们看到民意调查结果并能投票的公共网站。
  2. 一个能让你添加,改变和删除民意调查结果的管理网站。

我们假设你已经安装了Django。你能通过执行以下命令检查是否安装了Django:

$ python -m django –version

如果已经安装了Django,你会看到它的版本;否则,你会得到一个错误:“No module named django”。

这份教程适用于Django 1.10和Python 3.4或者更新的版本。如果Django的版本不匹配,你可以使用网页右下角的版本切换器(官方网站的网页中有)根据你的Django的版本选择相应版本的教程,或者将Django升级至最新的版本。如果你仍然使用Python 2.7,你将需要根据注释稍微地调整代码示例。

要想了解如何卸载老版本的Django和如何安装新版本的Django,请参见如何安装Django。

去哪里获取帮助?

如果你在学习这份教程的过程中遇到了困难,请咨询Django用户或者前往#django on irc.freenode.net去和其他Django用户交流,他们可能对你有所帮助。

创建一个工程

如果你是第一次使用Django,你就必须注意一些初始设置。也就是说,你需要自动生成一些构成Django工程的代码。Django工程是一个Django实例的配置的集合,包含数据库配置、与Django相关的选项和与具体应用相关的设置。

从命令行,使用cd进入你想用来存储代码的目录,然后运行一下命令:

$ django-admin startproject mysite

这将在当前目录中创建一个mysite目录。如果它没有起作用,请参见使用django-admin时遇到的错误。

请注意

你需要避免工程与Python或者Django的内置组件重名。尤其,你应该避免使用django这样的名字(这可能与Django本身有冲突)或者test这样的名字(这可能与Python的内置包产生冲突)。

应该把代码放在哪里?

如果你使用老版本的纯PHP(没有使用现代的框架),你可能习惯于将代码放到Web服务器的根目录下(比如/var/www)。如果使用Django,你就不能那样做。将任何Python代码放在Web服务器的根目录下都不是一种好的方式,因为这可能导致人们能够通过Web查看你的代码。这对安全来说并不好。

不要将代码放在根目录中,比如/home/mycode。

让我们观察startproject创建了什么:

mysite/

manage.py

mysite/

__init__.py

settings.py

urls.py

wsgi.py

这些文件是:

  1. 外层的mysite/根目录只是工程的容器。对Django来说,它的名字到底是什么并没有关系;你可以将它命名为任何你喜欢的名字。
  2. manage.py:一个能使你与Django工程通过多种方式进行交互的命令行工具。
  3. 内层的mysite/目录实际上是你的工程的Python包。目录名就是Python包的名字,你将使用它导入Python包内的任何内容(比如mysite.urls)。
  4. mysite/__init__.py:它是一个空的文件,其作用是告诉Python这个目录应该被当作一个Python包。
  5. mysite/settings.py:Django工程的配置文件。
  6. mysite/urls.py:Django工程的URL声明。
  7. mysite/wsgi.py:是承载Django工程的与WSGI兼容的万维网服务器的入口。

用于开发的服务器

让我们验证你的Django工程是否能正常工作。进入外层的mysite目录(如果不在这个目录中),然后执行下面的命令:

$ python manage.py runserver

你将在命令行中看到下面的输出:

正在执行系统检查...

系统检查没有发现任何问题 (0 silenced).

你有没有应用过的迁移;你的应用可能不会正常工作,直到这些迁移被应用。

执行#39;python manage.py migrate#39;以应用它们。

March 31, 2017 - 15:50:53

Django version 1.10,使用设置#39;mysite.settings#39;

启动开发服务器在http://127.0.0.1:8000/

使用CONTROL-C来推出服务器。

注意

现在忽略关于未利用的数据库迁移;我们将马上学习有关数据库的知识。

你已经开启了Django中用于开发的服务器,它是一个完全用Python编写的轻量的万维网服务器。我们将它包含在Django中,所以你能快速地开发,而不用必须配置一个承载产品的服务器,直到你已经开发好了产品。

现在服务器正在运行,通过浏览器访问http://127.0.0.1:8000/。你将看到一个“Welcome to Django”网页。它成功了。

改变端口

默认情况下,runserver命令会在内部IP的8000端口启动开发环境中的服务器。

如果你想改变服务器的端口,将端口作为命令行的参数。例如,这个命令在8000端口启动服务器

$ python manage.py runserver 8080

如果你想改变服务器的IP,将它写在端口的旁边。举个例子,为了在所有可获得的公共端口上监听,使用:

$ python manage.py runserver 0:8000

0 是0.0.0.0的简写。

自动重载runserver

开发服务器自动重载Python代码,当收到每一个请求的 不 时候。你不需要在代码改变的时候重启服务器以使代码生效。然而,一些比如添加文件的行为不会出发重启,所以在这些情况下你必须重启服务器。

创建polls应用程序

现在你的开发环境已经建立好了,你将开始工作了。

你用Django创建的每一个应用程序由一个遵循某种具体规则的Python包构成。Django自带一个自动生成应用程序的基本目录结构的工具,所以你你可以专注于写代码而不是创建目录。

工程与应用程序

一个工程与一个应用程序的区别是什么?一个应用程序是一个做某些事情的网络应用程序,比如博客系统,公共记录的数据库或者一个简单的民意调查应用程序。一个工程是配置和一些应用程序的集合。一个工程可以包含多个应用程序。一个应用程序可以在多个工程中。

在这份教程中,我们将在managy.py文件旁边创建polls应用程序,因此被导入时它能够作为自己的顶层模块,而不是作为mysite的子模块。

在创建应用程序之前,确保你与managy.py在同一个目录下,然后执行以下命令:

$ python manage.py startapp polls

那将创建一个polls目录,它展开后,如下所示:

polls/

__init__.py

admin.py

apps.py

migrations/

__init__.py

models.py

tests.py

views.py

这个目录结构将容纳polls应用程序。

编写你的第一个视图

让我们编写第一个视图。打开polls/views.py,然后将下面的代码放入其中:

polls/views.py

from django.http import HttpResponse

def index(request):

return HttpResponse('Hel\

lo, world. You#39;re at the polls index.')

这是Django中最简单的视图。为了调用这个视图,我们需要将其映射至一个URL,因此需要一个URLconf。

为了在polls的目录中创建URLconf,创建文件urls.py。你的应用程序目录现在看起来是这样的:

polls/

__init__.py

admin.py

apps.py

migrations/

__init__.py

models.py

tests.py

urls.py

views.py

文件polls/urls.py包含以下代码:

polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [

url(r#39;^$#39;, views.index, name=#39;index#39;),

]

下一步是在模块polls.urls中指出根URLconf。在mysite/urls.py中,导入django.conf.urls.include,然后往列表urlpatterns插入一个include(),最后结果如下所示:

mysite/urls.py

from django.conf.urls import include, url

from django.contrib import admin

urlpatterns = [

url(r#39;^polls/#39;, include(#39;polls.urls#39;)),

url(r#39;^admin/#39;, admin.site.urls),

]

函数include()应该能够引用其他的URLconf。请注意用于include()的正则表达式没有$(用来匹配字符串的末尾),但是其末尾有一个斜线。无论Django何时遇到include(),它都会去掉URL中与正则表达式匹配的部分,然后将剩下的部分发送给include()包含的URLconf进行进一步的处理。

include()的目的是更简单地实现URL的即插即用。

何时使用include()

当包含其他url模式时,你应当总是使用include()。admin.site.urls是这份教程的唯一例外。

没有匹配你看见的东西

如果你看见了include(admin.site.urls)而不仅仅是admin.site.urls,你可能使用了一个老版本的Django,所以与这份教程不匹配。你可以切换至老版本的Django或者新版本的Django。

现在你已经将视图index接入了URLconf。执行以下命令,以验证URLconf是否能正常工作:

$ python manage.py runserver

在浏览器中输入http://localhost:8000/polls/,你应该能看到自己在视图index中定义的“Hello, world. Yoursquo;re at the polls index”。

函数url()可以传递四个参数,两个必须的:regex和view,两个可选的:kwargs和name。在这里,你值得回头看看这些参数到底有什么用。

第二部分,编写你的第一个Django应用程序

这份教程从教程1终止的地方开始。我们将设置数据库,创建你的第一个模型,简单地了解Django自动生成的管理网站。

数据库设置

现在打开mysite/settings.py。它是一个普通的Python模块,有模块级别的代表Django设置的变量。

lt;

全文共12138字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[145031],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图