About me

Sunday, May 20, 2018

python django chap 10

Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link


GENERIC VIEWS

The generic views are used when in our earlier we created our views with functions such as index we can use and make more functions in the views for getting data from the database according to a parameter passed in the URL, loading a template and returning the rendered template.so django provides a simple method which is called as generic views. Generic views are classes and not  functions Generic views abstract common patterns to the point where you don’t even need to write Python code to write an app.and also to use that we need to change the URLconf were it is like the change in myproj/urls.py
path('', views.IndexView.as_view(), name='index')
and then we need to create our class in the myproj/views.py as we have removed the old index function and placed it with the IndexView as a class



from django.views.generic import TemplateView
 
class IndexView(TemplateView):
                 template_name = 'myproj/index.html'
            
This will display the template view there are different types of generic views as given here is the TemplateView which renders the template, with the context containing parameters captured in the URL.And other type of generic view is as the DetailView and ListView which will do as display a detail page for a particular type of object and other one will display a list of objects for more details on generic views click this  link

Uploading files 

                
when django handles file uploads it ends up in the placed in request.FILES. At first here we will consider a simple form containing a file upload field so for that we need to upload file there in forms.py file write as
from django import forms
class UploadFileForm(forms.Form):
        title = forms.CharField(max_length=50)
        file = forms.FileField()
The view handling this will receive the data in request.FILES which is a dictionary containing a key for each FileField(ImageField or any other FileField subclasses). in the form the data from the above form can be accessed as request.FILES ['file'].in order to upload files, it is to be submitted using the post method and also you need to make sure that your <form> element correctly defines the enctype as "multipart/form-data"

<form enctype="multipart/form-data" method="post" action="/foo/">

django have model fields to upload the files like FileField  and ImageField the files uploaded here are not stored in database but in the filesystem it is stored .they are created as a string field in the database  usually as VARCHAR which is the reference to the actual file if we delete one file it will not delete the physical file but only the reference to the file.      
                                             
                                         Now we will see the methods to file upload in here as normal you will pass your file as simple from request in to the form by binding the file with the forms .As is the case of forms which deals with FileField and ImageField are little more complicated than the normal ones here we need to specify a second argument when we bind our form. as for example if included our form with an ImageField called as image we need to bind the file data containing the image data in here given an example for it :


from django.core.files.uploadfiles import SimpleUploadFile
data = { 'subject' = 'this is the subject'
              'message'= 'this is the messge'
              'sender' = 'this@this.com'
              'cc_myself' = True }
file_data = {'image':SimpleUploadFile (img.jpg),<file data>}
f = ContactFormWithImage(data,file_data)

we will usually specify request.FILES as the source of the file data As above we can make a bound form like
f = ContactFormWithImage(request.POST,request.FILES)
here is a form with unbound
f = ContactFormWithImage{}
We can create the views like
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm
 #and an imaginary function to handle uploaded file
from somewere import handle_uploaded
def upload_file(request):
       if request.method== POST:
           form = UploadFileForm(request.POST,request.FILES)
            if form.is_valid():
                  handle_uploaded(request.FILES['file'])
                  returnHttpResponseRedirect('success/url/')   
else :
           form = UploadFileForm()
           return = render (request,'upload.html',{'form':form})
More on this topic will be included in the next post

THANKS FOR VIEWING MY BLOG HOPE YOU LIKE THE POST IF YOU WANT TO GET LATEST POSTS ON THIS PLEASE FOLLOW BY EMAIL ID AND FEEL FREE TO ASK YOUR DOUBTS AND DON'T FORGET TO SHARE,COMMENT AND FOLLOW

No comments:

Post a Comment