{"title":"FAQ: 使用Django","version":"5.2","locale":"zh-hans","docname":"faq/usage","url":"/zh-hans/5.2/faq/usage/","canonical":"https://djangodocs.dev/zh-hans/5.2/faq/usage/","summary":"为什么我在导入 DJANGO_SETTINGS_MODULE 时收到错误提示？ Link to this heading # 保证： 环境变量 DJANGO_SETTINGS_MODULE 设置为完全限定的 Python 模块（即 mysite.settings ）。 所述模块在 sys.path (需引入 import…","html":"<h1>FAQ: 使用Django<a class=\"heading-anchor\" href=\"#faq-using-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"why-do-i-get-an-error-about-importing-django-settings-module\">\n<h2>为什么我在导入 <span class=\"target\" id=\"index-2\"></span><a class=\"reference internal\" href=\"/zh-hans/5.2/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> 时收到错误提示？<a class=\"heading-anchor\" href=\"#why-do-i-get-an-error-about-importing-django-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>保证：</p>\n<ul class=\"simple\">\n<li><p>环境变量 <span class=\"target\" id=\"index-4\"></span><a class=\"reference internal\" href=\"/zh-hans/5.2/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> 设置为完全限定的 Python 模块（即 <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.settings</span></code> ）。</p></li>\n<li><p>所述模块在 <code class=\"docutils literal notranslate\"><span class=\"pre\">sys.path</span></code> (需引入 <code class=\"docutils literal notranslate\"><span class=\"pre\">import</span> <span class=\"pre\">mysite.settings</span></code> 才会工作）</p></li>\n<li><p>该模块不包含语法错误。</p></li>\n</ul>\n</section>\n<section id=\"i-can-t-stand-your-template-language-do-i-have-to-use-it\">\n<h2>我不喜欢 Django 的模板语言，能不能不使用它？<a class=\"heading-anchor\" href=\"#i-can-t-stand-your-template-language-do-i-have-to-use-it\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>我们认为我们的的模板引擎是最好的，但是我们意识到选择一个模板语言和信仰宗教相似。Django 没有什么必须的模板语言，如果你愿意使用 Jinja2、Mako 或其他的，请随意使用。</p>\n</section>\n<section id=\"do-i-have-to-use-your-model-database-layer\">\n<h2>我是否必须使用你的模型/数据库层？<a class=\"heading-anchor\" href=\"#do-i-have-to-use-your-model-database-layer\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>不。就像模板系统，模型/数据库层是与框架的其余部分分离的。</p>\n<p>唯一例外的是：如果你使用其他的数据库库，你没办法使用 Django 自动生成的后台界面。这个应用是和 Django 数据库层耦合的。</p>\n</section>\n<section id=\"how-do-i-use-image-and-file-fields\">\n<h2>我要怎么使用图片和文件字段？<a class=\"heading-anchor\" href=\"#how-do-i-use-image-and-file-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在模型中使用 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileField</span></code></a> 或 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ImageField</span></code></a>  ，你还需要完成如下步骤：</p>\n<ol class=\"arabic simple\">\n<li><p>在你的配置文件中，你需要将 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> 定义为你希望 Django 存储上传文件的目录的完整路径。（为了提高性能，这些文件不会存储在数据库中。）将 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> 定义为该目录的基本公共 URL。确保这个目录是可以被网络服务器的用户账户写入的。</p></li>\n<li><p>在你的模型中添加 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileField</span></code></a> 或者 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ImageField</span></code></a> ，可以通过定义 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.FileField.upload_to\" title=\"django.db.models.FileField.upload_to\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">upload_to</span></code></a> 在 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> 中明确一个子目录用来上传文件。</p></li>\n<li><p>所有将被储存在数据库中的文件路径相同（相对于 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a>）。你很想用由 Django 提供的 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.fields.files.FieldFile.url\" title=\"django.db.models.fields.files.FieldFile.url\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">url</span></code></a>，比如， 如果 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ImageField</span></code></a> 被叫做 <code class=\"docutils literal notranslate\"><span class=\"pre\">mug_shot</span></code>， 你就可以得到 <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">object.mug_shot.url</span> <span class=\"pre\">}}</span></code> 图片模板的绝对路径。</p></li>\n</ol>\n</section>\n<section id=\"how-do-i-make-a-variable-available-to-all-my-templates\">\n<h2>我如何让一个变量可用于我的所有模板之中？<a class=\"heading-anchor\" href=\"#how-do-i-make-a-variable-available-to-all-my-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>有时候你的模板会需要相同的东西。一个常见的例子是动态生成的菜单。乍一看，在模板上下文中添加一个通用字典似乎是合乎逻辑的。</p>\n<p>最好方法是使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code>。关于如何使用它请查看：<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/templates/api/#subclassing-context-requestcontext\"><span class=\"std std-ref\">使用 RequestContext</span></a>。</p>\n</section>","rootId":"faq-using-django","toc":[{"title":"为什么我在导入 DJANGO_SETTINGS_MODULE 时收到错误提示？","anchor":"why-do-i-get-an-error-about-importing-django-settings-module","children":[]},{"title":"我不喜欢 Django 的模板语言，能不能不使用它？","anchor":"i-can-t-stand-your-template-language-do-i-have-to-use-it","children":[]},{"title":"我是否必须使用你的模型/数据库层？","anchor":"do-i-have-to-use-your-model-database-layer","children":[]},{"title":"我要怎么使用图片和文件字段？","anchor":"how-do-i-use-image-and-file-fields","children":[]},{"title":"我如何让一个变量可用于我的所有模板之中？","anchor":"how-do-i-make-a-variable-available-to-all-my-templates","children":[]}],"breadcrumbs":[{"docname":"faq/index","title":"Django FAQ","url":"/zh-hans/5.2/faq/"}],"prev":{"docname":"faq/install","title":"FAQ：安装","url":"/zh-hans/5.2/faq/install/"},"next":{"docname":"faq/help","title":"FAQ：获取帮助","url":"/zh-hans/5.2/faq/help/"},"formats":{"html":"/zh-hans/5.2/faq/usage/","markdown":"/zh-hans/5.2/faq/usage.md","json":"/zh-hans/5.2/faq/usage.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/faq/usage.txt","official":"https://docs.djangoproject.com/zh-hans/5.2/faq/usage/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}