无法使用Django将数据插入数据库
Not able to insert data into database using Django
我是 Django 的初学者,我正在尝试将值插入 auth_user table。但不知何故我无法这样做。我试图解决,但一切似乎都是徒劳的。
这是我的 views.py:
from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth
# Create your views here.
def register(request):
if request.method == 'POST' :
firstname = request.POST['firstname']
lastname = request.POST[ 'lastname']
emailid= request.POST['emailid']
username= firstname+lastname
passwordl = request.POST['password']
user = User.objects.create_user(username=username,first_name=firstname,last_name=lastname)
user.save()
return redirect('admin/')
else:
return render(request, 'employee/register.html')
这是模板(我已经下载了)
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Delights-Dashbord</title>
<!-- Custom fonts for this template-->
<link href="{% static 'customer/vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet" type="text/css">
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet">
<!-- CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- jQuery and JS bundle w/ Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
<!-- Custom styles for this template-->
<link rel="stylesheet" href="{% static 'css/sb-admin-2.css' %}" />
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">
<!-- Sidebar - Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="Admin-reg.html">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
</div>
<div class="sidebar-brand-text mx-3">Delights</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="index.html">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Dashboard</span></a>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Nav Item - Pages Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseTwo"
aria-expanded="true" aria-controls="collapseTwo">
<i class="fas fa-fw fa-user-tie"></i>
<span>Employee</span>
</a>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<!-- <h6 class="collapse-header">Custom Components:</h6>-->
<a class="collapse-item" href="Admin-reg.html">Register</a>
<a class="collapse-item" href="tables.html">Update</a>
<a class="collapse-item" href="tables.html">Delete</a>
<a class="collapse-item" href="tables.html">View</a>
<!-- <a class="collapse-item" href="cards.html">Cards</a>-->
</div>
</div>
</li>
<!-- Nav Item - Utilities Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseUtilities"
aria-expanded="true" aria-controls="collapseUtilities">
<i class="fas fa-fw fa-user"></i>
<span>Customer</span>
</a>
<div id="collapseUtilities" class="collapse" aria-labelledby="headingUtilities"
data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<!-- <h6 class="collapse-header">Custom Utilities:</h6>-->
<a class="collapse-item" href="utilities-color.html">View</a>
<a class="collapse-item" href="utilities-border.html">View bills</a>
</div>
</div>
</li>
<!-- Divider -->
<hr class="sidebar-divider d-none d-md-block">
<!-- Sidebar Toggler (Sidebar) -->
<div class="text-center d-none d-md-inline">
<button class="rounded-circle border-0" id="sidebarToggle"></button>
</div>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Nav Item - Search Dropdown (Visible Only XS) -->
<li class="nav-item dropdown no-arrow d-sm-none">
<a class="nav-link dropdown-toggle" href="#" id="searchDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-search fa-fw"></i>
</a>
<!-- Dropdown - Messages -->
<div class="dropdown-menu dropdown-menu-right p-3 shadow animated--grow-in"
aria-labelledby="searchDropdown">
<div class="form-inline mr-auto w-100 navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-light border-0 small"
placeholder="Search for..." aria-label="Search"
aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</div>
</div>
</li>
<div class="topbar-divider d-none d-sm-block"></div>
<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600 small"><?php echo $nam ?></span>
<!-- <img class="img-profile rounded-circle" src="https://source.unsplash.com/QAB-WJcbgJk/60x60">-->
</a>
<!-- Dropdown - User Information -->
<div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
aria-labelledby="userDropdown">
<a class="dropdown-item" href="#">
<i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i>
Profile settings
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
<i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
Logout
</a>
</div>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 mb-8">
<div class="card shadow mb-8">
<div class="card-header py-8">
<h4 class="m-0 font-weight-bold text-primary">Register Employee</h4>
</div>
<div class="card-body">
<form action="register/" method="post">
{% csrf_token %}
<div class="text-primary">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">First name</label>
<input type="text" class="form-control" id="inputEmail4"
name="firstname" required>
</div>
<div class="form-group col-md-6">
<label for="lastname">Last name</label>
<input type="text" class="form-control" id="lastname"
name="lastname" required>
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Email</label>
<input type="text" class="form-control" id="inputPassword4"
name="emailid" required>
</div>
<div class="form-group col-md-6">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress"
name="address" required>
</div>
<div class="form-group col-md-6">
<label for="inputState">Sex</label>
<select id="inputState" class="form-control" name="sex">
<option selected>Male</option>
<option>Female</option>
<option>Others</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="id-proof">Aadhaar No</label>
<input type="tel" pattern="[0-9]{4}-[0-9]{4}-[0-9]{4}"
class="form-control" id="id-proof" name="aadharno" required>
</div>
<div class="form-group col-md-6">
<label for="Age">Age</label>
<input type="number" class="form-control" id="Age" max="100"
name="age" required>
</div>
<div class="form-group col-md-6">
<label for="phone-no">Phone no</label>
<input type="tel" pattern="[0-9]{10}" class="form-control"
id="phone-no" name="phoneno" required>
</div>
<div class="form-group col-md-6">
<label for="inputposition">Designation</label>
<select id="inputposition" class="form-control" name="designation">
<option selected>lol</option>
<option>lmao</option>
<option>rolf</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="phone-no">Salary</label>
<input type="tel" class="form-control" id="no-adults" name="Salary"
required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12 ">
<div class="text-center">
<input type="submit" value="Register" name="submit-emp"
class="btn btn-primary col-sm-3 btn-user ">
</div>
</div>
</div>
</form>
</div>
<!-- Approach -->
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
</div>
<!-- End of Main Content -->
<!-- Footer -->
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>Copyright © Delights 1995-2020</span>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="login.html">Logout</a>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript-->
<script src="{% static 'customer/vendor/jquery/jquery.min.js' %}"></script>
<script src="{% static 'customer/vendor/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
<!-- Core plugin JavaScript-->
<script src="{% static 'customer/vendor/jquery-easing/jquery.easing.min.js' %}"></script>
<!-- Custom scripts for all pages-->
<script src="{% static 'customer/js/sb-admin-2.min.js' %}"></script>
<!-- Page level plugins -->
<script src="{% static 'customer/vendor/chart.js/Chart.min.js' %}"></script>
<!-- Page level custom scripts -->
<script src="{% static 'customer/js/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'customer/js/demo/chart-pie-demo.js' %}"></script>
</body>
</html>
这个urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register/', views.register, name='register'),
]
我还可以注意到,当我点击注册按钮时,我收到一个错误,提示 employee\register\register 被调用,但它不存在,这很奇怪,因为要获取 employee\register。
此处您缺少在用户中插入密码 table...
另一种方式(推荐):
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ["username","first_name","last_name","email"]
labels = {'email':'Email'}
views.py:
def register(request):
if request.method=="POST":
fm = SignUpForm(request.POST)
if fm.is_valid():
fm.save()
return HttpResponseRedirect("admin/")
else:
fm = SignUpForm()
return render(request,'signup.html',{"fm":fm})
我查看了您的模板,发现您在第 196
行将表单的操作设置为 register/
。因此只需将其更改为 \employee\register
并从您在 urls.py
中创建的 url 中删除正斜杠,如下所示:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register', views.register, name='register'),
]
模板
...
<form action="\employee\register" method="post">
{% csrf_token %}
...
希望这能解决您的问题。
我是 Django 的初学者,我正在尝试将值插入 auth_user table。但不知何故我无法这样做。我试图解决,但一切似乎都是徒劳的。
这是我的 views.py:
from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth
# Create your views here.
def register(request):
if request.method == 'POST' :
firstname = request.POST['firstname']
lastname = request.POST[ 'lastname']
emailid= request.POST['emailid']
username= firstname+lastname
passwordl = request.POST['password']
user = User.objects.create_user(username=username,first_name=firstname,last_name=lastname)
user.save()
return redirect('admin/')
else:
return render(request, 'employee/register.html')
这是模板(我已经下载了)
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Delights-Dashbord</title>
<!-- Custom fonts for this template-->
<link href="{% static 'customer/vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet" type="text/css">
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet">
<!-- CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- jQuery and JS bundle w/ Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
<!-- Custom styles for this template-->
<link rel="stylesheet" href="{% static 'css/sb-admin-2.css' %}" />
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">
<!-- Sidebar - Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="Admin-reg.html">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
</div>
<div class="sidebar-brand-text mx-3">Delights</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="index.html">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Dashboard</span></a>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Nav Item - Pages Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseTwo"
aria-expanded="true" aria-controls="collapseTwo">
<i class="fas fa-fw fa-user-tie"></i>
<span>Employee</span>
</a>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<!-- <h6 class="collapse-header">Custom Components:</h6>-->
<a class="collapse-item" href="Admin-reg.html">Register</a>
<a class="collapse-item" href="tables.html">Update</a>
<a class="collapse-item" href="tables.html">Delete</a>
<a class="collapse-item" href="tables.html">View</a>
<!-- <a class="collapse-item" href="cards.html">Cards</a>-->
</div>
</div>
</li>
<!-- Nav Item - Utilities Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseUtilities"
aria-expanded="true" aria-controls="collapseUtilities">
<i class="fas fa-fw fa-user"></i>
<span>Customer</span>
</a>
<div id="collapseUtilities" class="collapse" aria-labelledby="headingUtilities"
data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<!-- <h6 class="collapse-header">Custom Utilities:</h6>-->
<a class="collapse-item" href="utilities-color.html">View</a>
<a class="collapse-item" href="utilities-border.html">View bills</a>
</div>
</div>
</li>
<!-- Divider -->
<hr class="sidebar-divider d-none d-md-block">
<!-- Sidebar Toggler (Sidebar) -->
<div class="text-center d-none d-md-inline">
<button class="rounded-circle border-0" id="sidebarToggle"></button>
</div>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Nav Item - Search Dropdown (Visible Only XS) -->
<li class="nav-item dropdown no-arrow d-sm-none">
<a class="nav-link dropdown-toggle" href="#" id="searchDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-search fa-fw"></i>
</a>
<!-- Dropdown - Messages -->
<div class="dropdown-menu dropdown-menu-right p-3 shadow animated--grow-in"
aria-labelledby="searchDropdown">
<div class="form-inline mr-auto w-100 navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-light border-0 small"
placeholder="Search for..." aria-label="Search"
aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</div>
</div>
</li>
<div class="topbar-divider d-none d-sm-block"></div>
<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600 small"><?php echo $nam ?></span>
<!-- <img class="img-profile rounded-circle" src="https://source.unsplash.com/QAB-WJcbgJk/60x60">-->
</a>
<!-- Dropdown - User Information -->
<div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
aria-labelledby="userDropdown">
<a class="dropdown-item" href="#">
<i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i>
Profile settings
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
<i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
Logout
</a>
</div>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 mb-8">
<div class="card shadow mb-8">
<div class="card-header py-8">
<h4 class="m-0 font-weight-bold text-primary">Register Employee</h4>
</div>
<div class="card-body">
<form action="register/" method="post">
{% csrf_token %}
<div class="text-primary">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">First name</label>
<input type="text" class="form-control" id="inputEmail4"
name="firstname" required>
</div>
<div class="form-group col-md-6">
<label for="lastname">Last name</label>
<input type="text" class="form-control" id="lastname"
name="lastname" required>
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Email</label>
<input type="text" class="form-control" id="inputPassword4"
name="emailid" required>
</div>
<div class="form-group col-md-6">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress"
name="address" required>
</div>
<div class="form-group col-md-6">
<label for="inputState">Sex</label>
<select id="inputState" class="form-control" name="sex">
<option selected>Male</option>
<option>Female</option>
<option>Others</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="id-proof">Aadhaar No</label>
<input type="tel" pattern="[0-9]{4}-[0-9]{4}-[0-9]{4}"
class="form-control" id="id-proof" name="aadharno" required>
</div>
<div class="form-group col-md-6">
<label for="Age">Age</label>
<input type="number" class="form-control" id="Age" max="100"
name="age" required>
</div>
<div class="form-group col-md-6">
<label for="phone-no">Phone no</label>
<input type="tel" pattern="[0-9]{10}" class="form-control"
id="phone-no" name="phoneno" required>
</div>
<div class="form-group col-md-6">
<label for="inputposition">Designation</label>
<select id="inputposition" class="form-control" name="designation">
<option selected>lol</option>
<option>lmao</option>
<option>rolf</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="phone-no">Salary</label>
<input type="tel" class="form-control" id="no-adults" name="Salary"
required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12 ">
<div class="text-center">
<input type="submit" value="Register" name="submit-emp"
class="btn btn-primary col-sm-3 btn-user ">
</div>
</div>
</div>
</form>
</div>
<!-- Approach -->
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
</div>
<!-- End of Main Content -->
<!-- Footer -->
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>Copyright © Delights 1995-2020</span>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="login.html">Logout</a>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript-->
<script src="{% static 'customer/vendor/jquery/jquery.min.js' %}"></script>
<script src="{% static 'customer/vendor/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
<!-- Core plugin JavaScript-->
<script src="{% static 'customer/vendor/jquery-easing/jquery.easing.min.js' %}"></script>
<!-- Custom scripts for all pages-->
<script src="{% static 'customer/js/sb-admin-2.min.js' %}"></script>
<!-- Page level plugins -->
<script src="{% static 'customer/vendor/chart.js/Chart.min.js' %}"></script>
<!-- Page level custom scripts -->
<script src="{% static 'customer/js/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'customer/js/demo/chart-pie-demo.js' %}"></script>
</body>
</html>
这个urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register/', views.register, name='register'),
]
我还可以注意到,当我点击注册按钮时,我收到一个错误,提示 employee\register\register 被调用,但它不存在,这很奇怪,因为要获取 employee\register。
此处您缺少在用户中插入密码 table...
另一种方式(推荐):
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ["username","first_name","last_name","email"]
labels = {'email':'Email'}
views.py:
def register(request):
if request.method=="POST":
fm = SignUpForm(request.POST)
if fm.is_valid():
fm.save()
return HttpResponseRedirect("admin/")
else:
fm = SignUpForm()
return render(request,'signup.html',{"fm":fm})
我查看了您的模板,发现您在第 196
行将表单的操作设置为 register/
。因此只需将其更改为 \employee\register
并从您在 urls.py
中创建的 url 中删除正斜杠,如下所示:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register', views.register, name='register'),
]
模板
...
<form action="\employee\register" method="post">
{% csrf_token %}
...
希望这能解决您的问题。