码迷,mamicode.com
首页 > 其他好文 > 详细

Django教程(2)

时间:2020-01-23 19:39:22      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:djang   databases   轻量   一个   deploy   它的   include   das   setting   

from Django official document;

Django 最初被设计用于具有快速开发需求的新闻类站点,目的是要实现简单快捷的网站开发。

from 编写你的第一个 Django 应用,第 1 部分.

the first Django startProject

1. create our project "mysite"

让我们看看 startproject 创建了些什么:

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

这些目录和文件的用处是:

  • The outer mysite/ root directory is a container for your project. Its name doesn‘t matter to Django; you can rename it to anything you like.
  • manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。你可以阅读 django-admin and manage.py 获取所有 manage.py 的细节。
  • 里面一层的 mysite/ 目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如 mysite.urls).
  • mysite/__init__.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。
  • mysite/settings.py:Django 项目的配置文件。如果你想知道这个文件是如何工作的,请查看 Django settings 。
  • mysite/urls.py:Django 项目的 URL 声明,就像你网站的“目录”。阅读 URL dispatcher 文档来获取更多关于 URL 的内容。
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • mysite/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。阅读 如何使用 WSGI 进行部署 了解更多细节。

 Django 自带的用于开发的简易服务器,它是一个用纯 Python 写的轻量级的 Web 服务器。我们将这个服务器内置在 Django 中是为了让你能快速的开发出想要的东西,因为你不需要进行配置生产级别的服务器(比如 Apache)方面的工作,除非你已经准备好投入生产环境了。

现在是个提醒你的好时机:千万不要 将这个服务器用于和生产环境相关的任何地方。这个服务器只是为了开发而设计的。(我们在 Web 框架方面是专家,在 Web 服务器方面并不是。)

2. create your app

Projects vs. apps

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

In this tutorial, we’ll create our "poll" app right next to your manage.py file so that it can be imported as its own top-level module.

That’ll create a directory polls, which is laid out like this:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

在mysite/setting.py里, INSTALLED_APPS里面添加新增的应用.

3. create your first view

可以在app/目录下添加urls.py,然后使用include()函数将app/urls.py添加到global 的urls.py中.

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path(polls/, include(polls.urls)),
    path(admin/, admin.site.urls),
]

我们设计 include() 的理念是使其可以即插即用。因为投票应用有它自己的 URLconf( polls/urls.py ),他们能够被放在 "/polls/" , "/fun_polls/" ,"/content/polls/",或者其他任何路径下,这个应用都能够正常工作。

4. configure your database

from 编写你的第一个 Django 应用,第 2 部分.

现在,打开 mysite/settings.py 。这是个包含了 Django 项目设置的 Python 模块。通常,这个配置文件使用 SQLite 作为默认数据库。

如果你想使用其他数据库,你需要安装合适的 database bindings ,然后改变设置文件中 DATABASES ‘default‘ 项目中的一些键值:

  • ENGINE -- 可选值有 ‘django.db.backends.sqlite3‘‘django.db.backends.postgresql‘‘django.db.backends.mysql‘,或 ‘django.db.backends.oracle‘。其它 可用后端
  • NAME - 数据库的名称。如果使用的是 SQLite,数据库将是你电脑上的一个文件,在这种情况下, NAME 应该是此文件的绝对路径,包括文件名。默认值 os.path.join(BASE_DIR, ‘db.sqlite3‘) 将会把数据库文件储存在项目的根目录。

如果你不使用 SQLite,则必须添加一些额外设置,比如 USER 、 PASSWORD 、 HOST 等等。想了解更多数据库设置方面的内容,请看文档:DATABASES 。

$ python manage.py migrate

The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app (we‘ll cover those later). You‘ll see a message for each migration it applies. If you‘re interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MariaDB, MySQL), .schema (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.

5. create your model

在 Django 里写一个数据库驱动的 Web 应用的第一步是定义模型 - 也就是数据库结构设计和附加的其它元数据。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Django教程(2)

标签:djang   databases   轻量   一个   deploy   它的   include   das   setting   

原文地址:https://www.cnblogs.com/dulun/p/12231060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!