django+js实现下载

json代码

$("#download").on("click", function () {
    location.href="/cexcel/download/?download="+"yes"+"&excel_file="+encodeURIComponent(data["excel_file"])+"&excel_name="+encodeURIComponent(data["excel_name"]);
})

 

django代码

def exceldownload(request):
    if request.method == "GET" and request.GET.get('download') == "yes":
        excel_file = request.GET["excel_file"]
        excel_name = request.GET["excel_name"]
        file = open(excel_file, 'rb')
        response = FileResponse(file)
        # response['Content-Type'] = 'application/vnd.ms-excel' #xls
        response['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' #xlsx
        response['Content-Disposition'] = 'attachment;filename="%s"' % excel_name
        return response

 

转载于:https://my.oschina.net/u/3164044/blog/1841037

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现登录功能可以分为前端和后端两部分。前端使用Layui框架实现页面布局,后端使用Django框架实现业务逻辑。 1. 前端页面 首先在Layui官网下载需要的组件,然后创建一个登录页面的HTML文件,例如: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录</title> <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css"> </head> <body> <div class="layui-container"> <div class="layui-row"> <div class="layui-col-md6 layui-col-md-offset3"> <form class="layui-form" action="" method="post"> {% csrf_token %} <div class="layui-form-item"> <label class="layui-form-label">用户名</label> <div class="layui-input-block"> <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密码</label> <div class="layui-input-block"> <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit="" lay-filter="login">立即登录</button> </div> </div> </form> </div> </div> </div> <script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script> </body> </html> ``` 在这个页面中,使用了Layui的表单组件,包括输入框、按钮等。此外,需要在表单中添加`{% csrf_token %}`标签,用于防止跨站请求伪造攻击。 2. 后端逻辑 使用Django框架实现后端逻辑,主要包括视图函数和路由。首先在`urls.py`文件中设置路由: ```python from django.urls import path from . import views urlpatterns = [ path('login/', views.login_view, name='login'), ] ``` 然后在`views.py`文件中实现视图函数: ```python from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login def login_view(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect('home') else: return render(request, 'login.html', {'error': '用户名或密码错误'}) else: return render(request, 'login.html') ``` 在这个视图函数中,首先从POST请求中获取用户名和密码,然后调用Django中的`authenticate`函数进行身份验证。如果验证通过,调用`login`函数将用户信息存储到session中,并重定向到主页;如果验证不通过,返回登录页面并显示错误信息。 最后,在`settings.py`文件中配置Django的认证系统: ```python AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', ] LOGIN_URL = '/login/' LOGIN_REDIRECT_URL = '/home/' ``` 其中,`AUTHENTICATION_BACKENDS`用于指定认证后端,这里使用默认的`ModelBackend`。`LOGIN_URL`设置登录页面的URL,`LOGIN_REDIRECT_URL`设置登录成功后重定向到的URL。 这样,就完成了使用Django和Layui实现登录功能的开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值