• django: form fileupload


    本节介绍 Form 中一些字段类型的使用,以文件上传字段 FileField 为例:(注,其它字段和相关用法见官方文档中的 Forms -> Built-in Fields

    一,配置 urls.py:

    from django.conf.urls import patterns, include, url
    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'csvt03.views.home', name='home'),
        # url(r'^csvt03/', include('csvt03.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        url(r'^admin/', include(admin.site.urls)),
        url(r'^index/$','blog.views.index'),
        url(r'^blog/show_author/$', 'blog.views.show_author'),
        url(r'blog/register/$', 'blog.views.register'),  # Django Form
        url(r'blog/regist/$', 'blog.views.regist'),      # 在前面例子的基础上增加 127.0.0.1:8000/blog/regist/ 地址作测试
    )

    二,编写 blog.views.regist 处理函数(blog/views.py):

    from django.shortcuts import render_to_response as r2r
    from blog.models import Employee, Author, Book
    
    def index(req):
        emps = Employee.objects.all()
        return r2r('index.html', {'emps':emps})
    
    def show_author(req):
        authors = Author.objects.all()
        return r2r('show_author.html', {'authors':authors})
    
    from django import forms
    from django.http import HttpResponse
    
    class UserForm(forms.Form):
        name = forms.CharField()
    #   username = forms.CharField()
        headImg = forms.FileField()
    
    def register(req):
        if req.method == 'POST':
            form = UserForm(req.POST)   # Binding
            if form.is_valid():
                print form.cleaned_data
                return HttpResponse('ok')
        else:
            form = UserForm()
        return r2r('register.html', {'form':form})
    
    def regist(req):
        if req.method == 'POST':
            uf = UserForm(req.POST, req.FILES)    # 绑定
            if uf.is_valid():
                print uf.cleaned_data['name']
                upfile = uf.cleaned_data['headImg']
                print upfile.name, upfile.size
                fp = file('upload/'+upfile.name, 'wb')
                fp.write(upfile.read())
                fp.close()
                return HttpResponse('OK')
        else:
            uf = UserForm()
        return r2r('regist.html', {'uf':uf})

    三,编写模板文件 blog/templates/regist.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Django DB</title>
        </head>
        <body>
            <div>User Regist</div>
            <div>
            <form method='post' enctype="multipart/form-data">
                {{uf.as_p}}
                <input type='submit' value='ok' />
            </form>
            </div>
        </body>
    </html>
  • 相关阅读:
    linux下mysql基于mycat做主从复制和读写分离之基础篇
    HttpClient之基本使用
    查阅资料学习的书签(补充中.......)
    java架构《Socket网络编程基础篇》
    Redis常见配置文件详解
    redis学习教程五《管道、分区》
    redis学习教程四《管理、备份、客户端连接》
    js将json数组转成tree对象
    小程序转发事件生命周期
    mpvue开发小程序添加页面
  • 原文地址:https://www.cnblogs.com/exclm/p/3376898.html
Copyright © 2020-2023  润新知