【Django必回三板斧】

Django三板斧

  1. HttpResponse

    返回字符串类型的数据

  2. render

    返回HTML文件,支持模板语法

  3. redirect

    重定向,括号内可以写其他网站的全称,也可以自己网站的后缀。

这三个方法都是用于视图层(views.py)中视图函数的返回值。

例子:

HttpResponse:

先在urls.py文件中添加对应关系

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZqpXS0QX-1652286805751)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512000311101.png)]

再去应用文件夹views.py下添加功能

在这里插入图片描述

启动Django

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yJ09uVea-1652286805752)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512000229944.png)]

render:

先去templates编写index.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iiPfx7ra-1652286805752)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512001332976.png)]

views.py

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qCltQyzw-1652286805753)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512001423449.png)]

结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o83Tf59Y-1652286805753)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512001508141.png)]

redirect()

重定向跳转到指定网站

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tRky7H6j-1652286805753)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512001932479.png)]

启动Django

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y9gB4h72-1652286805754)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512001915452.png)]

也可以定向到自己的网页

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UVcM2i14-1652286805754)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512003113833.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vD3QKyD3-1652286805754)(C:\Users\86199\AppData\Roaming\Typora\typora-user-images\image-20220512003059469.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django中有三种常见的登录方式: 1. 基于函数的登录方式: ```python from django.contrib.auth import authenticate, login from django.shortcuts import render, redirect 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: message = 'Invalid username or password' return render(request, 'login.html', {'message': message}) else: return render(request, 'login.html') ``` 2. 基于类的登录方式: ```python from django.contrib.auth import authenticate, login from django.views import View from django.shortcuts import render, redirect class LoginView(View): def get(self, request): return render(request, 'login.html') def post(self, request): 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: message = 'Invalid username or password' return render(request, 'login.html', {'message': message}) ``` 3. 使用Django内置的登录视图: ```python from django.contrib.auth.views import LoginView class CustomLoginView(LoginView): template_name = 'login.html' success_url = 'home' ``` 其中,第三种方式是使用Django内置的登录视图,它已经提供了大部分常用的登录功能,可以通过继承并覆盖相应的属性实现自定义的登录功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值