Difference between revisions of "Images in models and uploading images"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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')...")
 
(HTML Template)
 
(6 intermediate revisions by the same user not shown)
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 7: Line 9:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=Urls.py==
+
=Urls.py=
 +
You will need to add the following lines to the import statements at the top of the page:
 +
 
 +
<syntaxhighlight lang=python>
 +
from django.conf.urls.static import static
 +
from .settings import MEDIA_ROOT, MEDIA_URL
 +
</syntaxhighlight>
 +
 
 
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:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 18: Line 27:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
product_pic = models.ImageField(upload_to = 'media/', default = 'media/None/no-img.jpg')
+
product_pic = models.ImageField(upload_to = 'products/', default = 'products/no-img.jpg')
 +
</syntaxhighlight>
 +
 
 +
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>
 
</syntaxhighlight>
  
The parameters above will set the location and also a default image to use if no image is uploaded.
+
=Views.py=
 +
Now we need to pass the files uploaded into the view, so find your view to add products to the product model. It will currently have 'request.POST' as a parameter for the form, you need to also add 'request.FILES':
 +
 
 +
<syntaxhighlight lang=python>
 +
form = DocumentForm(request.POST, request.FILES)
 +
</syntaxhighlight>
 +
 
 +
=HTML Template=
 +
Now you need to make sure you set the enctype of the form:
 +
 
 +
[[File:Test..png]]

Latest revision as of 10:41, 20 May 2021

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

You will need to add the following lines to the import statements at the top of the page:

from django.conf.urls.static import static
from .settings import MEDIA_ROOT, MEDIA_URL

Now in your 'urls.py' we need to add to your URL Patterns, so after the 'urlpatterns' add the following line:

urlpatterns += static(MEDIA_URL, document_root=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 = 'products/', default = 'products/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']

Views.py

Now we need to pass the files uploaded into the view, so find your view to add products to the product model. It will currently have 'request.POST' as a parameter for the form, you need to also add 'request.FILES':

form = DocumentForm(request.POST, request.FILES)

HTML Template

Now you need to make sure you set the enctype of the form:

Test..png