django项目几个主要文件

来源:互联网 发布:网络机柜标准 编辑:程序博客网 时间:2024/05/29 11:22

经过网上查询和自己思考,把django项目主要几个文件写出来

1.wsgi.py

官网解释:

When the WSGI server loads your application, Django needs to import the settings module — that’s where your entire application is defined.

Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings module. It must contain the dotted path to the settings module. You can use a different value for development and production; it all depends on how you organize your settings.

If this variable isn’t set, the default wsgi.py sets it to mysite.settings, where mysite is the name of your project. That’s how runserver discovers the default settings file by default.

When the WSGI server loads your application, Django needs to import the settings module — that’s where your entire application is defined.

Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings module. It must contain the dotted path to the settings module. You can use a different value for development and production; it all depends on how you organize your settings.

If this variable isn’t set, the default wsgi.py sets it to mysite.settings, where mysite is the name of your project. That’s how runserver discovers the default settings file by default.


翻译:

使用WSGI进行部署的关键概念是application应用程序服务器用于与您的代码进行通信可调用代码。它通常作为一个application在服务器可访问的Python模块中命名的对象提供

startproject命令创建一个<project_name>/wsgi.py包含这种application可调用的文件 

它用于Django的开发服务器和生产WSGI部署。

WSGI服务器application从其配置获取可调用的路径Django的内置服务器,即runserver 命令,从WSGI_APPLICATION设置中读取它默认情况下,它被设置为<project_name>.wsgi.application,指向application 可调用的<project_name>/wsgi.py

当WSGI服务器加载你的应用程序时,Django需要导入设置模块 - 这是你的整个应用程序被定义的地方。

Django使用 DJANGO_SETTINGS_MODULE环境变量来定位适当的设置模块。它必须包含设置模块的虚线路径。您可以使用不同的价值进行开发和生产; 这一切都取决于你如何组织你的设置。

如果未设置此变量,则默认wsgi.py将其设置为 mysite.settings,其中mysite是项目的名称。这是runserver默认情况下如何 发现默认设置文件

总结:

          有小伙伴说:妹的,看上去这么多,看的我头都大了,能简单点吗,其实:WSGI理解为你写的【python代码块】和【Web服务器接口】 的配置文件。

       

2.settins.py


  对,仔细的小伙伴会在上面官方解释wsgi.py的时候看到settings 。

官方解释:


靠,没解释,只有让你看里面设置

那怎么办,怎么解释好,??  

看下图


总结:基本上所有的设置都在这个setting文件(我只能这样解释了)


3.urls.py

惯例,官网解释

Overview¶

To design URLs for an app, you create a Python module informally called a URLconf (URL configuration). This module is pure Python code and is a simple mapping between URL patterns (simple regular expressions) to Python functions (your views).

This mapping can be as short or as long as needed. It can reference other mappings. And, because it’s pure Python code, it can be constructed dynamically.

Django also provides a way to translate URLs according to the active language. See the internationalization documentationfor more information.

How Django processes a request¶

When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:

  1. Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of theROOT_URLCONF setting.
  2. Django loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
  3. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
  4. Once one of the regexes matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:
    • An instance of HttpRequest.
    • If the matched regular expression returned no named groups, then the matches from the regular expression are provided as positional arguments.
    • The keyword arguments are made up of any named groups matched by the regular expression, overridden by any arguments specified in the optional kwargs argument to django.conf.urls.url().
  5. If no regex matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.
翻译:

概述

要为应用程序设计URL,您可以非正式地创建一个名为URLconf(URL配置)的Python模块 这个模块是纯Python代码,是一个简单的Python模式(简单的正则表达式)到Python函数(您的视图)之间的映射。

这种映射可以尽可能短或只要需要。它可以引用其他映射。而且,因为它是纯Python代码,所以可以动态地构建它。

Django还提供了根据活动语言翻译URL的方法。请参阅国际化文档以获取更多信息。

Django是如何处理一个请求

当用户从Django支持的站点请求页面时,系统会遵循这个算法来确定要执行的Python代码:

  1. Django确定要使用的根URLconf模块。通常,这是ROOT_URLCONF设置的值,但是如果传入 HttpRequest对象具有一个urlconf 属性(由中间件设置),则将使用其值来代替 ROOT_URLCONF设置。
  2. Django加载Python模块并查找变量 urlpatterns这应该是一个Python django.conf.urls.url() 实例列表
  3. Django按顺序遍历每个URL模式,并停在与请求的URL匹配的第一个URL模式。
  4. 一旦一个正则表达式匹配,Django就会导入并调用给定的视图,这是一个简单的Python函数(或基于类的视图)。该视图通过了以下参数:
    • 一个实例HttpRequest
    • 如果匹配的正则表达式没有返回任何命名组,则来自正则表达式的匹配作为位置参数提供。
    • 关键字参数由正则表达式匹配的任何命名组组成,由可选kwargs参数to 指定的任何参数重写django.conf.urls.url()
  5. 如果没有正则表达式相匹配,或者在这个过程中的任何一点引发异常,Django调用一个合适的错误处理视图。下面的错误处理
官网就是字多,直接总结:

        urls.py就是URL配置文件


4.init.py

   惯例官网,不好意思,我这边没找到官网资料,

   总结:init.py就是空文件,网上说是声明模块,就是一个包。具体啥用,不知道。


5.manage.py

惯例上官网

django-admin and manage.py

django-admin is Django’s command-line utility for administrative tasks. This document outlines all it can do.

In addition, manage.py is automatically created in each Django project. manage.py does the same thing as django-admin but takes care of a few things for you:

  • It puts your project’s package on sys.path.
  • It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.

The django-admin script should be on your system path if you installed Django via its setup.py utility. If it’s not on your path, you can find it in site-packages/django/bin within your Python installation. Consider symlinking it from some place on your path, such as /usr/local/bin.

For Windows users, who do not have symlinking functionality available, you can copy django-admin.exe to a location on your existing path or edit the PATH settings (under Settings - Control Panel - System - Advanced -Environment...) to point to its installed location.

Generally, when working on a single Django project, it’s easier to use manage.py than django-admin. If you need to switch between multiple Django settings files, use django-admin with DJANGO_SETTINGS_MODULE or the --settingscommand line option.

The command-line examples throughout this document use django-admin to be consistent, but any example can use manage.py or python -m django just as well.


翻译:

django-adminmanage.py

django-admin是Django用于管理任务的命令行工具。本文件概述了它可以做的所有事情。

另外,manage.py在每个Django项目中都会自动创建。 manage.py做同样的事情,django-admin但为你照顾几件事情:

  • 它把你的项目的包放到sys.path
  • 它设置 DJANGO_SETTINGS_MODULE环境变量,以便它指向您的项目的settings.py文件。

django-admin如果您通过其实用setup.py程序安装了Django 脚本应该位于系统路径中如果不在你的路径上,你可以site-packages/django/bin在你的Python安装中找到它考虑从你的路径上的某个地方进行符号链接,比如/usr/local/bin

对于没有可用的符号链接功能的Windows用户,您可以将其复制django-admin.exe到现有路径上的某个位置,或编辑PATH设置(在)下指向其安装的位置。Settings - Control Panel - System - Advanced - Environment...

一般来说,在单个Django项目上工作时,manage.py使用更容易 django-admin如果您需要多个Django配置文件之间切换,使用django-admin与 DJANGO_SETTINGS_MODULE--settings命令行选项。

本文档中使用的命令行的例子django-admin是一致的,但任何例如可以使用manage.py或者 一样好。python -mdjango

总结:管理你的django项目


6.templates

    没惯例了,也是个空文件,文件自动新建,在windows上显示,为空文件夹,里面啥都没有





    


原创粉丝点击