{"title":"FAQ: Using Django","version":"6.0","locale":"en","docname":"faq/usage","url":"/en/6.0/faq/usage/","canonical":"https://djangodocs.dev/en/6.0/faq/usage/","summary":"Why do I get an error about importing DJANGO_SETTINGS_MODULE ? Link to this heading # Make sure that: The environment variable DJANGO_SETTINGS_MODULE is set to a…","html":"<h1>FAQ: Using 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>Why do I get an error about importing <span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/en/6.0/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>Make sure that:</p>\n<ul class=\"simple\">\n<li><p>The environment variable <span class=\"target\" id=\"index-1\"></span><a class=\"reference internal\" href=\"/en/6.0/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> is set to a\nfully-qualified Python module (i.e. <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.settings</span></code>).</p></li>\n<li><p>Said module is on <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> should work).</p></li>\n<li><p>The module doesn’t contain syntax errors.</p></li>\n</ul>\n</section>\n<section id=\"i-can-t-stand-your-template-language-do-i-have-to-use-it\">\n<h2>I can’t stand your template language. Do I have to use it?<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>We happen to think our template engine is the best thing since chunky bacon,\nbut we recognize that choosing a template language runs close to religion.\nThere’s nothing about Django that requires using the template language, so\nif you’re attached to Jinja2, Mako, or whatever, feel free to use those.</p>\n</section>\n<section id=\"do-i-have-to-use-your-model-database-layer\">\n<h2>Do I have to use your model/database layer?<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>Nope. Just like the template system, the model/database layer is decoupled from\nthe rest of the framework.</p>\n<p>The one exception is: If you use a different database library, you won’t get to\nuse Django’s automatically-generated admin site. That app is coupled to the\nDjango database layer.</p>\n</section>\n<section id=\"how-do-i-use-image-and-file-fields\">\n<h2>How do I use image and file fields?<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>Using a <a class=\"reference internal\" href=\"/en/6.0/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> or an\n<a class=\"reference internal\" href=\"/en/6.0/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> in a model takes a few steps:</p>\n<ol class=\"arabic simple\">\n<li><p>In your settings file, you’ll need to define <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> as\nthe full path to a directory where you’d like Django to store uploaded\nfiles. (For performance, these files are not stored in the database.)\nDefine <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> as the base public URL of that directory.\nMake sure that this directory is writable by the web server’s user\naccount.</p></li>\n<li><p>Add the <a class=\"reference internal\" href=\"/en/6.0/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> or\n<a class=\"reference internal\" href=\"/en/6.0/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> to your model, defining the\n<a class=\"reference internal\" href=\"/en/6.0/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> option to specify a\nsubdirectory of <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> to use for uploaded files.</p></li>\n<li><p>All that will be stored in your database is a path to the file\n(relative to <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a>). You’ll most likely want to use the\nconvenience <a class=\"reference internal\" href=\"/en/6.0/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> attribute\nprovided by Django. For example, if your\n<a class=\"reference internal\" href=\"/en/6.0/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> is called <code class=\"docutils literal notranslate\"><span class=\"pre\">mug_shot</span></code>, you can get\nthe absolute path to your image in a template with\n<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>How do I make a variable available to all my templates?<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>Sometimes your templates all need the same thing. A common example would be\ndynamically generated menus. At first glance, it seems logical to add a common\ndictionary to the template context.</p>\n<p>The best way to do this in Django is to use a <code class=\"docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code>. Details on\nhow to do this are here: <a class=\"reference internal\" href=\"/en/6.0/ref/templates/api/#subclassing-context-requestcontext\"><span class=\"std std-ref\">Using RequestContext</span></a>.</p>\n</section>","rootId":"faq-using-django","toc":[{"title":"Why do I get an error about importing DJANGO_SETTINGS_MODULE?","anchor":"why-do-i-get-an-error-about-importing-django-settings-module","children":[]},{"title":"I can’t stand your template language. Do I have to use it?","anchor":"i-can-t-stand-your-template-language-do-i-have-to-use-it","children":[]},{"title":"Do I have to use your model/database layer?","anchor":"do-i-have-to-use-your-model-database-layer","children":[]},{"title":"How do I use image and file fields?","anchor":"how-do-i-use-image-and-file-fields","children":[]},{"title":"How do I make a variable available to all my templates?","anchor":"how-do-i-make-a-variable-available-to-all-my-templates","children":[]}],"breadcrumbs":[{"docname":"faq/index","title":"Django FAQ","url":"/en/6.0/faq/"}],"prev":{"docname":"faq/install","title":"FAQ: Installation","url":"/en/6.0/faq/install/"},"next":{"docname":"faq/help","title":"FAQ: Getting Help","url":"/en/6.0/faq/help/"},"formats":{"html":"/en/6.0/faq/usage/","markdown":"/en/6.0/faq/usage.md","json":"/en/6.0/faq/usage.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/faq/usage.txt","official":"https://docs.djangoproject.com/en/6.0/faq/usage/","inVersions":["dev","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","1.11","1.10","1.9","1.8"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}