Difference between revisions of "Images in models and uploading images"
(Created page with "=Settings.py= In your 'settings.py' file you will need to add the following: <syntaxhighlight lang=python> MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')...") |
(→Urls.py=) |
||
Line 7: | Line 7: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | =Urls.py | + | =Urls.py= |
Now in your 'urls.py' we need to add to your URL Patterns, so after the 'urlpatterns' add the following line: | Now in your 'urls.py' we need to add to your URL Patterns, so after the 'urlpatterns' add the following line: | ||
Revision as of 08:32, 9 December 2019
Settings.py
In your 'settings.py' file you will need to add the following:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Urls.py
Now in your 'urls.py' we need to add to your URL Patterns, so after the 'urlpatterns' add the following line:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your Model
If you have already followed the tutorials on django, you would have created a model (probably for products). We now need to edit this model to add an 'ImageField' for the product.
product_pic = models.ImageField(upload_to = 'media/', default = 'media/None/no-img.jpg')
The parameters above will set the location and also a default image to use if no image is uploaded.