django-文件下载-异步验证账号

<head>
    <meta charset="UTF-8">
    {% load static %}
    <title>文件</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/shangchuan/">
    {% csrf_token %}
    <input type="file" name="files"><br>
    <input type="file" name="files">
    <button type="submit">上传</button>
    {{ message }}
</form>
{% if imgurl %}
<img src="{%  static imgurl %}" width="200px">
{% endif %}


</body>

from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import HttpResponseRedirect
from django.http import StreamingHttpResponse
import os
import uuid

def
filexiazai(request,filename): filepath=os.getcwd()+'/app/static/file/'+filename print('服务器文件路径:{0}'.format(filepath)) with open(filepath,'rb') as filed: content=filed.read() #return HttpResponse(content,content_type='image/jpeg') return HttpResponse(content,content_type='application/octet-stream')
def filexiazai2(request,filename):
    filepath = os.getcwd() + '/app/static/file/' + filename
    def fileiter(path,chunk=512):
        with open(path,'rb')as filed:
            while True:
                content=filed.read(chunk)
                if content:
                    yield content
                else:
                    break
    response=StreamingHttpResponse(fileiter(filepath))
    response['Content-Type']='application/octet-stream'
    response['Content-Disposition']='attachment;filename={0}'.format(filename)
    return response

 

jquery实现获取value值 设置value值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajaxdd</title>
    <script src="jquery-3.2.1.min.js">
    </script>
    <script>
        $(function () {
            $("#btn").click(function () {
                v=$("#textd").val()
                alert(v);
            });
            $("#btn1").click(function () {
                $("#textd").val('456');
            });

        });
    </script>
</head>
<body>
<input type="text" id="textd">
<button id="btn">jquery获取value</button>
<button id="btn1">jquery设置</button>

</body>
</html>

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    {% load static %}
    <script src="{% static 'jquery-3.2.1.min.js' %}"></script>
    <script src="{% static 'jquery.cookie.js' %}"></script>
    <script>
        $(function () {
            $('#aa').click(function () {
                userds=document.getElementsByName('userd')[0]
                if(userds.value==''){
                 $('span').text('请输入用户名');
                 userds.focus();
                }else {
                    $('span').text('');
                    $.ajax({
                        async:true,
                        url:"/goajax/"+ userds.value +"/",
                        type:'post',
                        headers:{'X-CSRFTOKEN':$.cookie('csrftoken')},
                        success:function (data) {
                            if (data=='true'){
                                $('span').text('恭喜通过');
                                $('span').css({"color":"green"});

                            }else {
                                $('span').text('用户已占用');
                                $('span').css({"color":"red"});
                            }
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
<h3>异步验证注册</h3>
<form method="post" >

<input type="text" name="userd" placeholder="请输入用户名">
<a id="aa">唯一验证</a><span></span><br>
<input type="password" name="passd" placeholder="请输入密码">
<input type="submit" value="regs">
</form>
</body>
</html>
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def goreg(request):

    return render(request,"regd.html")


def goajax(request,username):
    usernames=['zhangsan','lisi']
    flag='true'
    print(username)
    if username in usernames:
        flag='false'
    return HttpResponse(flag)
urlpatterns = [
    path('admin/', admin.site.urls),
path('reg/', goreg),
path('goajax/<str:username>/', goajax),
]
原文地址:https://www.cnblogs.com/huazhou695/p/10020807.html