2016-01-03 11:56:22 +01:00
|
|
|
=================
|
2008-08-24 00:25:40 +02:00
|
|
|
FAQ: Using Django
|
|
|
|
=================
|
|
|
|
|
2020-04-30 12:12:05 +02:00
|
|
|
Why do I get an error about importing :envvar:`DJANGO_SETTINGS_MODULE`?
|
|
|
|
=======================================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
Make sure that:
|
|
|
|
|
2020-04-30 12:12:05 +02:00
|
|
|
* The environment variable :envvar:`DJANGO_SETTINGS_MODULE` is set to a
|
2022-03-10 10:20:25 +01:00
|
|
|
fully-qualified Python module (i.e. ``mysite.settings``).
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* Said module is on ``sys.path`` (``import mysite.settings`` should work).
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2020-05-01 14:37:21 +02:00
|
|
|
* The module doesn't contain syntax errors.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
I can't stand your template language. Do I have to use it?
|
2016-01-03 11:56:22 +01:00
|
|
|
==========================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
We happen to think our template engine is the best thing since chunky bacon,
|
|
|
|
but we recognize that choosing a template language runs close to religion.
|
|
|
|
There's nothing about Django that requires using the template language, so
|
2014-09-21 17:30:18 +02:00
|
|
|
if you're attached to Jinja2, Mako, or whatever, feel free to use those.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
Do I have to use your model/database layer?
|
2016-01-03 11:56:22 +01:00
|
|
|
===========================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
Nope. Just like the template system, the model/database layer is decoupled from
|
|
|
|
the rest of the framework.
|
|
|
|
|
|
|
|
The one exception is: If you use a different database library, you won't get to
|
|
|
|
use Django's automatically-generated admin site. That app is coupled to the
|
|
|
|
Django database layer.
|
|
|
|
|
|
|
|
How do I use image and file fields?
|
2016-01-03 11:56:22 +01:00
|
|
|
===================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2009-03-30 19:07:43 +02:00
|
|
|
Using a :class:`~django.db.models.FileField` or an
|
2008-09-02 19:33:51 +02:00
|
|
|
:class:`~django.db.models.ImageField` in a model takes a few steps:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
#. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as
|
|
|
|
the full path to a directory where you'd like Django to store uploaded
|
|
|
|
files. (For performance, these files are not stored in the database.)
|
|
|
|
Define :setting:`MEDIA_URL` as the base public URL of that directory.
|
2021-07-23 08:48:16 +02:00
|
|
|
Make sure that this directory is writable by the web server's user
|
2011-10-14 02:12:01 +02:00
|
|
|
account.
|
|
|
|
|
|
|
|
#. Add the :class:`~django.db.models.FileField` or
|
2013-10-11 14:07:25 +02:00
|
|
|
:class:`~django.db.models.ImageField` to your model, defining the
|
|
|
|
:attr:`~django.db.models.FileField.upload_to` option to specify a
|
|
|
|
subdirectory of :setting:`MEDIA_ROOT` to use for uploaded files.
|
2011-10-14 02:12:01 +02:00
|
|
|
|
|
|
|
#. All that will be stored in your database is a path to the file
|
|
|
|
(relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
|
2013-01-01 14:12:42 +01:00
|
|
|
convenience :attr:`~django.db.models.fields.files.FieldFile.url` attribute
|
|
|
|
provided by Django. For example, if your
|
|
|
|
:class:`~django.db.models.ImageField` is called ``mug_shot``, you can get
|
|
|
|
the absolute path to your image in a template with
|
|
|
|
``{{ object.mug_shot.url }}``.
|
2009-04-01 01:34:03 +02:00
|
|
|
|
|
|
|
How do I make a variable available to all my templates?
|
2016-01-03 11:56:22 +01:00
|
|
|
=======================================================
|
2009-04-01 01:34:03 +02:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
Sometimes your templates all need the same thing. A common example would be
|
|
|
|
dynamically generated menus. At first glance, it seems logical to add a common
|
|
|
|
dictionary to the template context.
|
2009-04-01 01:34:03 +02:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
The best way to do this in Django is to use a ``RequestContext``. Details on
|
|
|
|
how to do this are here: :ref:`subclassing-context-requestcontext`.
|