Monday, 19 July 2021

*Episode 13* DJANGO (Uploading File of Django)


 Uploading Files


     Our web apps should be in a position to support uploading of files. The files can be pictures, pdfs, word documents, songs and other types.


  • Uploading an Image :-

    Before can start to work with images in Django, you have to ensure that you have installed the Python Image Library (PIL). To demonstrate how images can be uploaded, let us begin by creating a profile form. The following code is necessary for this:


#-*- coding: utf-8 -*-

from django import forms

class ProfForm(forms.Form):

name = forms.CharField(max_length = 90)

pict = forms.ImageFields()


      The “ImageField” works to ensure that the file that is uploaded is an image. If this is not the case, then validation of the form will fail.


      We now need to create a model named “Profile”, which will be used for storing the images that we upload. This has to be done in the file “myapplication/models.py” as shown in the code given below:


from django.db import models

class Profiles(models.Model):

name = models.CharField(max_length = 60)

pic = models.ImageField(upload_to = ‘pictures’)

class Meta:

db_table = “profiles”


      As shown in the above code, the “ImageField” has taken a compulsory field named “upload_to”. This property represents the location in our hard drive in which we will store our uploaded images.


      Now that we have the model and the form, the view can be created in the directory “myapplication/views.py”. This is shown below:


#-*- coding: utf-8 -*-

from myapplication.forms import ProfileForm

from myapplication.models import Profile

def SvProfile(request):

saved = False

if request.method == “POST”:

#Getting our posted form

MyProfForm = ProfileForm(request.POST, request.FILES)

if MyProfForm.is_valid():

prof = Profile()

prof.name = MyProfForm.cleaned_data[“name”]

prof.picture = MyProfForm.cleaned_data[“picture”]

prof.save()

saved = True

else:

MyProfForm = Profform()

return render(request, ‘saved.html’, locals())


       Here is the code for the file “myapplication/templates/saved.html”:


<html>

<body>

{% if saved %}

<strong>The profile picture was successfully saved.</strong>

{% endif %}

{% if not saved %}

<strong>The profile was not saved.</strong>

{% endif %}

</body>

</html>


      Here is the code for the file “myapplication/templates/profile.html”


<html>

<body>

<form name = “form” enctype = “multipart/form-data”

action = “{% url “myapplication.views.SvProfile” %}” method = “POST” >{%

csrf_token %}

<div style = “max-width:460px;”>

<center>

<input type = “text” style = “margin-left:19%;”

placeholder = “Name” name = “name” />

</center>

</div>

<br>

<div style = “max-width:460px;”>

<center>

<input type = “file” style = “margin-left:19%;”

placeholder = “Picture” name = “picture” />

</center>

</div>

<br>

<div style = “max-width:460px;”>

<center>

<button style = “border:1px;background-color:#4285F4; margin-top:9%;

height:36px; width:79%; margin-left:20%;” type = “submit” value = “Login” >

<strong>Login</strong>

</button>

</center>

</div>

</form>

</body>

</html>


      What we need next is to set up our URLs and then we will be in a position to get started. The code given below can be used for specification of these:


from django.conf.urls import patterns, url

from django.views.generic import TemplateView

upatterns = patterns(

‘myapplication.views’, url(r’^profile/’,TemplateView.as_view(

template_name = ‘profile.html’)), url(r’^saved/’, ‘SvProfile’, name = ‘saved’)

)


      Now we should be in a position to upload images to our web app. However, when you need to upload another type of file, the “ImageField” has to be replaced with “FileField” in both the Form and the Model.


👈Episode 12(D).                                                                            Episode 14(D)👉

Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...