前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django重定向

django重定向

作者头像
py3study
发布2020-01-19 15:29:08
1.4K0
发布2020-01-19 15:29:08
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
return HttpResponseRedirect('/index/')# 重定向
返回url格式:http://127.0.0.1:8000/index/会去掉前期的所有路由重新写入/index/'路由

urls代码
代码语言:javascript
复制
from django.urls import path
from django.contrib import admin
from ProductOutWarehouse import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'',views.login),
    path(r'login/',views.login),
    path(r'login_action/',views.login_action),
    path(r'index/',views.index),
]


views代码
代码语言:javascript
复制
import os
from django.shortcuts import render,render_to_response,redirect,HttpResponseRedirect
from .models import User

# Create your views here.


#首页
def login(request):
    return render(request,'login.html')

def login_action(request):
    if request.POST:
        acount = (request.POST.get("Acount").strip())
        password = (request.POST.get("Password").strip())
        user = User.objects.filter(workNumber=acount,password=password)
        if user:
            print("账户密码正确")
            return HttpResponseRedirect('/index/')# 重定向
        else:
            print("密码错误")
            return render(request,"login.html",{"error":"账户不存在"})
    return render(request,"login.html")
def index(request):
    return render(request,'index.html')
表单代码
代码语言:javascript
复制
<form method="post" action="/login_action/">
    {% csrf_token %}
  <div class="form-group">
      <h3 class="text-left" >帐号</h3>
      <input type="text" class="form-control" name="Acount" placeholder="帐号">
  </div>
  <div class="form-group">
      <h3 class="text-left" >密码</h3>
      <input type="password" class="form-control" name="Password" placeholder="密码">
  </div>
  <div class="form-group text-left">
    <div class="checkbox checkbox-primary">
      <label><input type="checkbox">
        <i></i></label>
        <span class="white f-s-16 m-l-5">记住密码</span>
    </div>

    <div class="text">
          {{ error }}
    </div>
  </div>
  <button type="submit" class="btn btn-block btn-lg btn-primary">登录</button>
</form>

form表单传递路由
代码语言:javascript
复制
<form method="post" action="/login_action/">
URL表现为http://127.0.0.1:8000/login_action/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档