Django html file Upload upload image

Source: Internet
Author: User
Tags html file upload




html file upload  upload Image      First step:  This is a button to upload a picture <form class= " Form-horizontal " method=" post " action=" {% url  ' create_article '  %} " enctype=" Multipart/form-data "> {% csrf_token %}  <div class=" Form-group ">     <label for= "InputEmail3"  class= "Col-sm-2 control-label" > Pictures </label >    <div class= "Col-sm-10" >         <input type= "File"  name= "Head_img" >    </div>  </div ></form>     the above button will send you a local image to the  create_article   this URL entry processing.     Step two: create_article This URL entry will be given to the following views.py content processing    def create_article (Request ):    if request.method ==  "GET"  :         return  rendER (request, ' create_aritcle.html ')     elif request.method ==  "POST" &NBSP;:         print request. post        print  '----------'          print request. files       # #查看上传图片的路径         bbs_ Generater = utils. Articlegen (Request)         res = bbs_generater.create ()         html_ele = "" "         your article <<a href= "/article/%s/" > %s</a>> has been  created successfully !!!,          "" " % (Res.id,  res.title)         return httpresponse (Html_ele)       step three: The above utils  template content is as follows: #!/usr/bin/python# -*- coding: utf-8 -*-__author__ =   ' GAOGD ' Import  osimport  modelsfrom s10day12bbs import settingsclass  articlegen (object):     def __init__ (self,request):         self.requset = request    def parse_data (self):         form_data = {          ' title '  : self.requset.post.get (' title '),          ' content '  : self.requset.post.get (' content '),         ' Summary '  : self.requset.post.get (' summary '),         ' author_ Id '   : self.requset.user.userprofile.id,         ' head_ IMG ':  ',         ' category_id '  : 1           # #这里应该是用户选择的才对         }         return form_data    def create (self):         self.data = self.parse_data ()          bbs_obj = models. Article (**self.data)         bbs_obj.save ()          filename = handle_upload_file (self.requset,self.requset.files[' head_img ')    #获取图片路径并保存到数据库         bbs_obj.head_img =  Filename        bbs_obj.save ()          return bbs_obj    def update (self):        &Nbsp;passdef handle_upload_file (request, file_obj):    upload_dir =  '% s/%s '  %  (settings. Base_dir, settings. Fileuploaddir)     if not os.path.isdir (upload_dir):         os.mkdir (upload_dir)     print   '----> ',  dir (file _obj)     with open ('%s/%s '  %  (upload_dir, file_obj.name),  ' WB ')  as destination:        for chunk in file_ Obj.chunks ():             destination.write (Chunk)     return file_obj.name     Step Two: View the path to the uploaded image         requset. files[' head_img ']       request. files       # #查看上传图片的路径      Step three:  get picturesPath and save it to the database.     # #这个是图片处理的重点     def handle_upload_file (request, file_obj ):        upload_dir =  '%s/%s '  %  (settings. Base_dir, settings. Fileuploaddir)         if not os.path.isdir (upload_dir):             os.mkdir (Upload_dir)          print   '----> ',  dir (file_obj)          with open ('%s/%s '  %  (upload_dir, file_obj.name),  ' WB ')  as  destination:            for chunk  In file_obj.chunks ():                 destination.write (Chunk) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;FILE_OBJ.NAme 


This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1858761

Django html file Upload upload image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.