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:
- Django determines the root URLconf module to use. Ordinarily, this is the value of the
ROOT_URLCONF
setting, but if the incomingHttpRequest
object has aurlconf
attribute (set by middleware), its value will be used in place of theROOT_URLCONF
setting. - Django loads that Python module and looks for the variable
urlpatterns
. This should be a Python list ofdjango.conf.urls.url()
instances. - Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
- 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 todjango.conf.urls.url()
.
- An instance of
- 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代码:
- Django确定要使用的根URLconf模块。通常,这是
ROOT_URLCONF
设置的值,但是如果传入HttpRequest
对象具有一个urlconf
属性(由中间件设置),则将使用其值来代替ROOT_URLCONF
设置。 - Django加载Python模块并查找变量
urlpatterns
。这应该是一个Pythondjango.conf.urls.url()
实例列表。 - Django按顺序遍历每个URL模式,并停在与请求的URL匹配的第一个URL模式。
- 一旦一个正则表达式匹配,Django就会导入并调用给定的视图,这是一个简单的Python函数(或基于类的视图)。该视图通过了以下参数:
- 一个实例
HttpRequest
。 - 如果匹配的正则表达式没有返回任何命名组,则来自正则表达式的匹配作为位置参数提供。
- 关键字参数由正则表达式匹配的任何命名组组成,由可选
kwargs
参数to 指定的任何参数重写django.conf.urls.url()
。
- 一个实例
- 如果没有正则表达式相匹配,或者在这个过程中的任何一点引发异常,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’ssettings.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 --settings
command 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-admin
和manage.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上显示,为空文件夹,里面啥都没有
- django项目几个主要文件
- 获取磁盘剩余空间 Django项目几个主要文件功能的解释
- django生成项目文件
- u-boot中几个主要文件及作用
- 配置Django项目的静态文件路径
- Django项目文件分布及解释
- django 项目的html加载css文件
- 地理信息系统几个主要概念
- struts2几个主要标签
- USB几个主要结构
- ffmpeg 几个主要数据结构
- 几个主要的期刊杂志
- 二极管几个主要特性
- Django几个小错误
- struts-config.xml中配置的几个主要项目的顺序
- Django项目
- 在C#.NET中,如何生成PDF文件?主要有以下几个途径
- 在C#.NET中,如何生成PDF文件?主要有以下几个途径:
- springboot程序无法访问静态资源
- Extra Workloads
- Spring各jar包的作用
- Tensorflow实战学习(三十四)【实现Word2Vec】
- RecyclerView网络获取信息
- django项目几个主要文件
- 变量x,y,z赋初值2.5,然后在屏幕上打印这些变量的值。
- Tensorflow实战学习(三十五)【实现基于LSTM语言模型】
- deep learning
- OpenCV人脸识别之一:数据收集和预处理
- 解决oracle中插入大字段时,报ORA-01461错。
- 十分钟教你配置frp实现内网穿透
- 从学习 Paddle 开始学习深度学习
- QT signals and slots