File Upload Flashcards

1
Q

Configure For file upload

A

Create media directory
On settings: under static add MEDIA_URL = ‘/media/‘
add MEDIA_ROOT = os.path.join( BASE_DIR, ‘media’)
Media_root → tells Django where to look For media files are stored in file system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Url.py

A

Add
if settings. DEBUG:
url patterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Implementing upload feature

A

On models.py

Image = models.ImageField(upload_to=‘appname/image’)
* since we are working with images we need to install pillow*
Pip install pillow
*migrate
# use FileField for other files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

On the template

A

<form method=‘post’ encrypt=multiparty/form-data>
{% csrf_token%}
{{form.as_p }}
</form>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Uploading files offer than images

A

On models.py
File = models.FileField(upload_to=‘app_name/files’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Creating A form

A

Create forms.py
Class uploadForm(forms.ModelForm):
class meta:
Model = [model name]
fields = [name of fields]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Views.py

A

def upload_Image(request):
if request.method == ‘POST’:
form = uploadForm( request.POST, request.FILES)
If form. is_valid:
form. save()
saved_object = form. instance # get uploaded file
return render(request,’template_name’, {‘form’: form, ‘file’:saved_object}
else:
form = UploadForm()
return render …..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly