<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    Some interesting examples on how to handle file uploads using Django:

    • Code snippet posted on Djangosnippets. The file is saved by declaring a save() method in the form class. This method is invoked when calling form.save(), which is standard Django newforms practice. (Note that this snipped uses clean_data. As of Django version 0.96, clean_data has been renamed to cleaned_data, so you will have to change the code or it won’t work)
    • Django image upload and validation. The author uses a model for the file and its related data. The uploaded file is saved by calling the save_FOO_file method. (This method is automatically provided by Django for fields declared as models.ImageField or models.FileField in the model. See the db-api documentation.)
    • Django image upload, form_for_instance and monkey-patching. The example code creates a form class from request.user by calling form_for_instance. The resulting class in then monkey-patched to insert the avatar image validation code. (Although the code is interesting the monkey patch seems unnecessary. I wouldn’t mind inserting the avatar validation method in a UserProfileForm class derived from form.Forms. The code would be certainly clearer: I think KISS takes precedence over DRY in this case.)

    Interesting, there seems to be no easy way of limiting the uploaded file size. The file can be rejected at validation time, but the data would have already been transfered.

    A file upload recipe

    After reading those posts, I think that a good recipe for handling file uploads in Django would be:

    • Write a django model for the uploaded file and its related data. Using a Django model makes sense, because it is usually necessary for the application to keep track of the uploaded files.
    • Write a subclass of form.Forms and declare a clean_FOO method for each models.FileInput or models.ImageInput fields declared in the model class. These clean_FOO methods are used to validate the uploaded files.
    • use a django view to receive the POST data, or display the form if no data is posted or errors are found.
    • validate the uploaded file or files by triggering the standard django newforms validation mechanism: is_valid().
    • save the file or files getting the data directly from the request.FILES object, by writing a save() method for the subclassed form or by calling save_FOO_file for the model instance.

    A simpler way to upload a file

    The following short Django example uses no data models, does no data validation, and saves the file directly to disk using python standard file functions. It is just a simple test I wrote to get familiar with the request.FILES object. This is not production code: it could be used to execute an arbitrary script on the server.
    The directory where the file is to be saved must be writable by the user that is running the Django server script. (The example uses MEDIA_ROOT as defined in settings.py.)

    file: views.py

    1. from django import http  
    2. from django import newforms as forms  
    3. from django.shortcuts import render_to_response  
    4. from djangotest.settings import MEDIA_ROOT  
    5.    
    6.    
    7. class SimpleFileForm(forms.Form):  
    8.     file = forms.Field(widget=forms.FileInput, required=False)  
    9.    
    10.    
    11. def directupload(request):  
    12.     """ 
    13.     Saves the file directly from the request object.  
    14.     Disclaimer:  This is code is just an example, and should 
    15.     not be used on a real website.  It does not validate 
    16.     file uploaded:  it could be used to execute an  
    17.     arbitrary script on the server.  
    18.     """  
    19.    
    20.     template = 'fileupload.html'  
    21.    
    22.     if request['method'] == 'POST':  
    23.         if 'file' in request.FILES:  
    24.             file = request.FILES['file']  
    25.    
    26.             # Other data on the request.FILES dictionary:  
    27.             #   filesize = len(file['content'])<br />  
    28.             #   filetype = file['content-type']   
    29.    
    30.             filename = file['filename']  
    31.    
    32.             fd = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')  
    33.             fd.write(file['content'])  
    34.             fd.close()  
    35.    
    36.             return http.HttpResponseRedirect(' upload_success.html')  
    37.     else:  
    38.         # display the form  
    39.         form = SimpleFileForm()  
    40.         return render_to_response(template, { 'form': form })  
    41.    

    file: fileupload.html

    1. {% extends "base.html" %}  
    2.    
    3. {% block body  %}  
    4.      <h1>Upload a file</h1>  
    5.      <form action="." method="post" enctype="multipart/form-data">  
    6.              {{ form }}  
    7.              <input type="submit" value="Upload" />  
    8.      </form>  
    9. {% endblock %} 
    posted on 2008-09-22 23:08 seal 閱讀(1535) 評論(0)  編輯  收藏 所屬分類: Python
    主站蜘蛛池模板: 亚洲国产精品精华液| 亚洲韩国在线一卡二卡| 精品亚洲视频在线| 日韩免费高清一级毛片在线| 亚洲欧洲日产国码久在线| 毛片免费视频播放| 亚洲AV无码XXX麻豆艾秋| 国产免费牲交视频| 色多多www视频在线观看免费| 亚洲 小说区 图片区 都市| ssswww日本免费网站片| 在线亚洲人成电影网站色www| 中国一级全黄的免费观看| 久热综合在线亚洲精品| 无码人妻久久一区二区三区免费 | 亚洲综合色视频在线观看| 一级午夜免费视频| 亚洲av不卡一区二区三区| 在线人成精品免费视频| 亚洲色大成网站www尤物| 一本久到久久亚洲综合| 成全视频在线观看免费| 亚洲黄色网址大全| 全免费一级毛片在线播放| 污视频网站免费观看| 亚洲av无码成人黄网站在线观看| 1000部羞羞禁止免费观看视频 | 亚洲午夜日韩高清一区| 精品国产一区二区三区免费| 亚洲免费视频网址| 亚洲国产精品不卡毛片a在线| 七色永久性tv网站免费看| 国产亚洲玖玖玖在线观看| 亚洲黄片手机免费观看| 最近2022中文字幕免费视频| 亚洲AV第一成肉网| 亚洲国产成人私人影院| 国产精品成人免费综合| 人妻丰满熟妇无码区免费| 国产亚洲精品美女| 亚洲国产精品久久久久秋霞影院|