django ,上传文件,接收表单,接收文件

博客提及了add.html、views.py和model.py文件,结合标签可知与Django开发相关,这些文件在Django项目中是常见的组成部分,add.html可能是视图模板,views.py处理业务逻辑,model.py定义数据模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

add.html

{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台会员添加</title>
{% endblock %}


{% block con %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-fl">
                        线条表单
                    </div>
                    <div class="widget-function am-fr">
                        <a href="javascript:;" class="am-icon-cog">
                        </a>
                    </div>
                </div>
                <div class="widget-body am-fr">
                    <form action="{% url 'myadmin_user_insert' %}" method="POST" enctype="multipart/form-data" class="am-form tpl-form-line-form">
                        {% csrf_token %}
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                                昵称
                            </label>
                            <div class="am-u-sm-9">
                                <input type="text" name="nikename" class="tpl-form-input" 
                                 placeholder="请输入昵称">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                                密码
                            </label>
                            <div class="am-u-sm-9">
                                <input type="password" name="password"  class="tpl-form-input" 
                                 placeholder="请输入密码">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                              手机号
                            </label>
                            <div class="am-u-sm-9">
                                <input type="text" name="phone"  class="tpl-form-input" 
                                 placeholder="请输入手机号">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                              邮箱
                            </label>
                            <div class="am-u-sm-9">
                                <input type="email" name="email"  class="tpl-form-input" 
                                 placeholder="请输入email">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                              年龄
                            </label>
                            <div class="am-u-sm-9">
                                <input type="number" name="age"  class="tpl-form-input" 
                                 placeholder="请输入年龄">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">
                              性别
                            </label>
                            <div class="am-u-sm-9">
                                <label class="am-radio-inline"><input type="radio" value="0" name="sex"></label>
                                <label class="am-radio-inline"><input type="radio" value="1" name="sex"></label>
                            </div>
                        </div>
                      
                        <div class="am-form-group">
                            <label for="user-weibo" class="am-u-sm-3 am-form-label">
                                封面图
                                <span class="tpl-form-line-small-title">
                                    Images
                                </span>
                            </label>
                            <div class="am-u-sm-9">
                                <div class="am-form-group am-form-file">
                                    <div class="tpl-form-file-img">
                                        <img src="assets/img/a5.png" alt="">
                                    </div>
                                    <button type="button" class="am-btn am-btn-danger am-btn-sm">
                                        <i class="am-icon-cloud-upload">
                                        </i>
                                        添加封面图片
                                    </button>
                                    <input id="doc-form-file" name="pic" type="file" multiple="">
                                </div>
                            </div>
                        </div>
                       
                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3">
                                <button class="am-btn am-btn-primary tpl-btn-bg-color-success ">
                                    提交
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse

from django.contrib.auth.hashers import make_password, check_password
from .. models import Users


# Create your views here.
# 用户模型的管理
# 会员添加表单
def user_add(request):
    return render(request,'myadmin/users/add.html')

# 会员执行添加
def user_insert(request):
    # 接收表单数据
    data = request.POST.dict()
    data.pop('csrfmiddlewaretoken')

    # 处理密码 加密 存入数据库
    data['password'] = make_password(data['password'], None, 'pbkdf2_sha256')

    # 头像上传
    # 接收上传的文件   request.FILES.获取文件数据
    myfile = request.FILES.get('pic',None)
    if not myfile:
        # 没有选择头像上传,
		#history.back(-1)这句话指的是返回上一个页面
        return HttpResponse('<script>alert("没有选择头像上传");history.back(-1);</script>')

    # 处理头像的上传  1.jpg ==> [1,jpg]
    # 把路径报保存到数据库 
    data['pic_url'] = uploads_pic(myfile)
    
    try:
        # 创建模型,添加数据
        ob = Users(**data)
        ob.save()
        return HttpResponse('<script>alert("添加成功");location.href="/myadmin/user/index/";</script>')
    except:
        pass
    return HttpResponse('<script>alert("添加失败");history.back(-1);</script>')


# 会员列表
def user_index(request):
    return HttpResponse('user_index')



# 头像上传封装成函数
def uploads_pic(myfile):
    try:
        import time
        #获取当前时间+'.'+jpg,  此处split将 1.jpg ==>[1,'jpg']   .pop() 删除第一个出现的元素 [1,'jpg'] ==> 'jpg'
        filename = str(time.time())+"."+myfile.name.split('.').pop()
        # 保存路径/+文件名称,wb+ 二进制可读写打开
        destination = open("./static/uploads/"+filename,"wb+")
        for chunk in myfile.chunks():# 分块写入文件  
            destination.write(chunk)  
        destination.close()
        #把文件路径返回
        return '/static/uploads/'+filename
    except:
        return False

model.py

from django.db import models

# Create your models here.


# 定义会员模型
class Users(models.Model):
    nikename = models.CharField(max_length=20,null=True)
    password = models.CharField(max_length=77)
    # phone = models.CharField(max_length=11,unique=True)
    phone = models.CharField(max_length=11)
    email = models.CharField(max_length=100)
    age = models.IntegerField(null=True)
    pic_url = models.CharField(max_length=100)
    SEX_CHOICES = (
        (0, '女'),
        (1, '男'),
    )
    sex = models.CharField(max_length=1,null=True,choices=SEX_CHOICES)
    # 0 正常  1禁用  2 删除 ....
    status = models.IntegerField(default=0)
    addtime = models.DateTimeField(auto_now_add=True)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值