Difference between revisions of "Images in models and uploading images"
(→Urls.py=) |
|||
Line 1: | Line 1: | ||
+ | You will need to ensure a module called 'pillow' is installed. | ||
+ | |||
=Settings.py= | =Settings.py= | ||
In your 'settings.py' file you will need to add the following: | In your 'settings.py' file you will need to add the following: | ||
Line 21: | Line 23: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | The parameters above will set the location and also a default image to use if no image is uploaded. | + | The parameters above will set the location and also a default image to use if no image is uploaded. |
+ | Now in the form for this model you will need to add 'product_pic' by including it in the list of fields: | ||
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | class Meta: | ||
+ | model=Product | ||
+ | fields = ['name','manufacturer','description','product_pic'] | ||
+ | </syntaxhighlight> |
Revision as of 08:42, 9 December 2019
You will need to ensure a module called 'pillow' is installed.
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. Now in the form for this model you will need to add 'product_pic' by including it in the list of fields:
class Meta:
model=Product
fields = ['name','manufacturer','description','product_pic']