Django Learning Notes (vi) Django upload file

Source: Internet
Author: User
Tags button type configuration settings

This should be the last learning module for Django. And then you need to go into the project to practice.

There are two ways to upload a Django file, one is the traditional IO-flow based operation, and one is encapsulated as an object-based operation.

But either way, you need to first create a directory of uploaded files,


Operation based on Io flow

The configuration in the URLs, based on the submission of the form in learning Notes (v) blog/param/(? P<ID>\D{2})

The modified post method is as follows:


Def post (req):     If Req.method = ' post ':         form = UserForm (req. Post,req. FILES)         if Form.is_valid ():             Print Form.cleaned_data & nbsp           print form.cleaned_data[' name ']             Print form.cl eaned_data[' Userimag '].name             print form.cleaned_data[' Userimag '].size   & nbsp         #way 1             s = form.cleaned_data[' Userimag '].read () &nbs P           fb = File ('./djangotest/upload/' +form.cleaned_data[' userimag '].name, ' WB ')   & nbsp         Fb.write (s)             Fb.close ()         & nbsp                 return HttpResponse (' OK ')         ELSE:             return HttpResponse (' Failed ')     else:         form =  userform ()                  return render_to_response (' param.html ', {' form ': Form})

The corresponding param.html are as follows:

<! DOCTYPE html>

Second, object-based storage:

First, in the configuration settings file

Media_root = './djangotest/'
The goal is to let the path of subsequent uploads be relative to the path of the project itself, without having to write dead


Add a new models:

Class Files (models. Model):
    name = models. Charfield (max_length=300)
    f = models. Filefield (upload_to= ' upload/')

    def __unicode__ (self): return
        Self.name
Synchronizing databases

To modify the Post method:



Def post (req):     If Req.method = ' post ':         form = UserForm (req. Post,req. FILES)         if Form.is_valid ():             Print Form.cleaned_data & nbsp           print form.cleaned_data[' name ']             Print form.cl eaned_data[' Userimag '].name             print form.cleaned_data[' Userimag '].size   & nbsp         #way 1             s = form.cleaned_data[' Userimag '].read () &nbs P           fb = File ('./djangotest/upload/' +form.cleaned_data[' userimag '].name, ' WB ')   & nbsp         Fb.write (s)             Fb.close ()         & nbsp                #way2             f = Files (name=form . cleaned_data[' Userimag '].name,f=form.cleaned_data[' Userimag '])             F.save ()     & nbsp       return HttpResponse (' OK ')         else:       &NBSP     ; Return HttpResponse (' Failed ')     else:         form =  userform ()     &NBSP ;            return render_to_response (' param.html ', {' form ': Form})


The corresponding param.html are as follows:

<! DOCTYPE html>



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.