| 251 | | |
|---|
| | 251 | |
|---|
| | 252 | Using a `FieldField` or an ``ImageField`` (see below) in a model takes a few |
|---|
| | 253 | steps: |
|---|
| | 254 | |
|---|
| | 255 | 1. In your settings file, you'll need to define ``MEDIA_ROOT``as the |
|---|
| | 256 | full path to a directory where you'd like Django to store uploaded |
|---|
| | 257 | files. (For performance, these files are not stored in the database.) |
|---|
| | 258 | Define ``MEDIA_URL`` as the base public URL of that directory. Make |
|---|
| | 259 | sure that this directory is writable by the Web server's user |
|---|
| | 260 | account. |
|---|
| | 261 | |
|---|
| | 262 | 2. Add the ``FileField`` or ``ImageField`` to your model, making sure |
|---|
| | 263 | to define the ``upload_to`` option to tell Django to which |
|---|
| | 264 | subdirectory of ``MEDIA_ROOT`` it should upload files. |
|---|
| | 265 | |
|---|
| | 266 | 3. All that will be stored in your database is a path to the file |
|---|
| | 267 | (relative to ``MEDIA_ROOT``). You'll must likely want to use the |
|---|
| | 268 | convenience ``get_<fieldname>_url`` function provided by Django. For |
|---|
| | 269 | example, if your ``ImageField`` is called ``mug_shot``, you can get |
|---|
| | 270 | the absolute URL to your image in a template with ``{{ |
|---|
| | 271 | object.get_mug_shot_url }}``. |
|---|
| | 272 | |
|---|