django重定向
return HttpResponseRedirect('/index/')# 重定向 返回url格式:http://127.0.0.1:8000/index/會去掉前期的所有路由重新寫入/index/'路由 urls代碼
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代碼
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') 表單代碼
<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表單傳遞路由
<form method="post" action="/login_action/"> URL表現為http://127.0.0.1:8000/login_action/