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