赞
踩
输入 conda activate 激活base环境再输入python manage.py runserver即可运行成功
输入python manage.py migrate再输入python manage.py runserver即可运行成功
1.先使用ctrl+C终止运行,再输入python manage.py startapp djangoApp即可创建app
注意:app在项目文件夹内,与同名子应用处于同一级。
2.在djangoProject的settings.py中注册app
1.djangoProject的urls.py
- from django.contrib import admin
- from django.urls import path
- from djangoApp.views import signup
-
- urlpatterns = [
- path('admin/', admin.site.urls),
- path('signup/', signup.as_view()),
- ]
2.djangoApp的views.py
- from django.shortcuts import render
- from django.views.generic import View
-
-
- # Create your views here.
- class signup(View):
- def get(self, request):
- return render(request, 'signup.html')
3.在template中新建signup.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- Please sign up
- </body>
- </html>
4.运行程序后进入http://127.0.0.1:8000/signup/
5.重新编辑html文件
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>FLEA MARKET</title>
- </head>
- <style>
- *{
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- </style>
- <body style="background: rgba(248,245,134,0.51);">
- <!--title-->
- <div class="title">WELCOME TO FLEA MARKET</div>
- <style>
- .title {
- -webkit-box-sizing: content-box;
- -moz-box-sizing: content-box;
- box-sizing: content-box;
- cursor: pointer;
- border: none;
- font: normal 5em/normal "Passero One", Helvetica, sans-serif;
- color: rgb(248, 245, 134);
- text-shadow: 0 1px 0 rgba(255,255,255,1) , 0 2px 0 rgba(255,255,255,1) , 0 3px 0 rgba(255,255,255,1) , 0 4px 0 rgba(255,255,255,1) , 0 5px 0 rgba(255,255,255,1) , 0 6px 1px rgba(0,0,0,0.0980392) , 0 0 5px rgba(0,0,0,0.0980392) , 0 1px 3px rgba(0,0,0,0.298039) , 0 3px 5px rgba(0,0,0,0.2) , 0 -5px 10px rgba(0,0,0,0.247059) , 0 -7px 10px rgba(0,0,0,0.2) , 0 -15px 20px rgba(0,0,0,0.14902) ;
- -webkit-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1) 10ms;
- -moz-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1) 10ms;
- -o-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1) 10ms;
- transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1) 10ms;
- text-align: center;
- -o-text-overflow: clip;
- text-overflow: clip;
- }
- </style>
- <link async href="https://fonts.googleapis.com/css?family=Passero%20One" data-generated="https://enjoycss.com" rel="stylesheet" type="text/css"/>
- <!--ghost-->
- <div class="container1">
- <svg class="ghost" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
- width="200px" height="200px"
- xml:space="preserve">
- <path fill="#fcfcda" d="M116.223,125.064c1.032-1.183,1.323-2.73,1.391-3.747V54.76c0,0-4.625-34.875-36.125-44.375
- s-66,6.625-72.125,44l-0.781,63.219c0.062,4.197,1.105,6.177,1.808,7.006c1.94,1.811,5.408,3.465,10.099-0.6
- c7.5-6.5,8.375-10,12.75-6.875s5.875,9.75,13.625,9.25s12.75-9,13.75-9.625s4.375-1.875,7,1.25s5.375,8.25,12.875,7.875
- s12.625-8.375,12.625-8.375s2.25-3.875,7.25,0.375s7.625,9.75,14.375,8.125C114.739,126.01,115.412,125.902,116.223,125.064z"></path>
- <circle fill="#013E51" cx="86.238" cy="57.885" r="6.667"></circle>
- <circle fill="#013E51" cx="40.072" cy="57.885" r="6.667"></circle>
- <path fill="#013E51" d="M71.916,62.782c0.05-1.108-0.809-2.046-1.917-2.095c-0.673-0.03-1.28,0.279-1.667,0.771
- c-0.758,0.766-2.483,2.235-4.696,2.358c-1.696,0.094-3.438-0.625-5.191-2.137c-0.003-0.003-0.007-0.006-0.011-0.009l0.002,0.005
- c-0.332-0.294-0.757-0.488-1.235-0.509c-1.108-0.049-2.046,0.809-2.095,1.917c-0.032,0.724,0.327,1.37,0.887,1.749
- c-0.001,0-0.002-0.001-0.003-0.001c2.221,1.871,4.536,2.88,6.912,2.986c0.333,0.014,0.67,0.012,1.007-0.01
- c3.163-0.191,5.572-1.942,6.888-3.166l0.452-0.453c0.021-0.019,0.04-0.041,0.06-0.061l0.034-0.034
- c-0.007,0.007-0.015,0.014-0.021,0.02C71.666,63.771,71.892,63.307,71.916,62.782z"></path>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="18.614" cy="99.426" r="3.292"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="95.364" cy="28.676" r="3.291"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="24.739" cy="93.551" r="2.667"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="101.489" cy="33.051" r="2.666"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="18.738" cy="87.717" r="2.833"></circle>
- <path fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" d="M116.279,55.814c-0.021-0.286-2.323-28.744-30.221-41.012
- c-7.806-3.433-15.777-5.173-23.691-5.173c-16.889,0-30.283,7.783-37.187,15.067c-9.229,9.736-13.84,26.712-14.191,30.259
- l-0.748,62.332c0.149,2.133,1.389,6.167,5.019,6.167c1.891,0,4.074-1.083,6.672-3.311c4.96-4.251,7.424-6.295,9.226-6.295
- c1.339,0,2.712,1.213,5.102,3.762c4.121,4.396,7.461,6.355,10.833,6.355c2.713,0,5.311-1.296,7.942-3.962
- c3.104-3.145,5.701-5.239,8.285-5.239c2.116,0,4.441,1.421,7.317,4.473c2.638,2.8,5.674,4.219,9.022,4.219
- c4.835,0,8.991-2.959,11.27-5.728l0.086-0.104c1.809-2.2,3.237-3.938,5.312-3.938c2.208,0,5.271,1.942,9.359,5.936
- c0.54,0.743,3.552,4.674,6.86,4.674c1.37,0,2.559-0.65,3.531-1.932l0.203-0.268L116.279,55.814z M114.281,121.405
- c-0.526,0.599-1.096,0.891-1.734,0.891c-2.053,0-4.51-2.82-5.283-3.907l-0.116-0.136c-4.638-4.541-7.975-6.566-10.82-6.566
- c-3.021,0-4.884,2.267-6.857,4.667l-0.086,0.104c-1.896,2.307-5.582,4.999-9.725,4.999c-2.775,0-5.322-1.208-7.567-3.59
- c-3.325-3.528-6.03-5.102-8.772-5.102c-3.278,0-6.251,2.332-9.708,5.835c-2.236,2.265-4.368,3.366-6.518,3.366
- c-2.772,0-5.664-1.765-9.374-5.723c-2.488-2.654-4.29-4.395-6.561-4.395c-2.515,0-5.045,2.077-10.527,6.777
- c-2.727,2.337-4.426,2.828-5.37,2.828c-2.662,0-3.017-4.225-3.021-4.225l0.745-62.163c0.332-3.321,4.767-19.625,13.647-28.995
- c3.893-4.106,10.387-8.632,18.602-11.504c-0.458,0.503-0.744,1.165-0.744,1.898c0,1.565,1.269,2.833,2.833,2.833
- c1.564,0,2.833-1.269,2.833-2.833c0-1.355-0.954-2.485-2.226-2.764c4.419-1.285,9.269-2.074,14.437-2.074
- c7.636,0,15.336,1.684,22.887,5.004c26.766,11.771,29.011,39.047,29.027,39.251V121.405z"></path>
- </svg>
- <p class="shadowFrame"><svg version="1.1" class="shadow" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="61px" y="20px"
- width="122.436px" height="39.744px" viewBox="0 0 122.436 39.744"
- xml:space="preserve">
- <ellipse fill="#d0d0b7" cx="61.128" cy="19.872" rx="49.25" ry="8.916"></ellipse>
- </svg></p>
- </div>
- <div class="container2">
- <svg class="ghost" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
- width="200px" height="200px"
- xml:space="preserve">
- <path fill="#fcfcda" d="M116.223,125.064c1.032-1.183,1.323-2.73,1.391-3.747V54.76c0,0-4.625-34.875-36.125-44.375
- s-66,6.625-72.125,44l-0.781,63.219c0.062,4.197,1.105,6.177,1.808,7.006c1.94,1.811,5.408,3.465,10.099-0.6
- c7.5-6.5,8.375-10,12.75-6.875s5.875,9.75,13.625,9.25s12.75-9,13.75-9.625s4.375-1.875,7,1.25s5.375,8.25,12.875,7.875
- s12.625-8.375,12.625-8.375s2.25-3.875,7.25,0.375s7.625,9.75,14.375,8.125C114.739,126.01,115.412,125.902,116.223,125.064z"></path>
- <circle fill="#013E51" cx="86.238" cy="57.885" r="6.667"></circle>
- <circle fill="#013E51" cx="40.072" cy="57.885" r="6.667"></circle>
- <path fill="#013E51" d="M71.916,62.782c0.05-1.108-0.809-2.046-1.917-2.095c-0.673-0.03-1.28,0.279-1.667,0.771
- c-0.758,0.766-2.483,2.235-4.696,2.358c-1.696,0.094-3.438-0.625-5.191-2.137c-0.003-0.003-0.007-0.006-0.011-0.009l0.002,0.005
- c-0.332-0.294-0.757-0.488-1.235-0.509c-1.108-0.049-2.046,0.809-2.095,1.917c-0.032,0.724,0.327,1.37,0.887,1.749
- c-0.001,0-0.002-0.001-0.003-0.001c2.221,1.871,4.536,2.88,6.912,2.986c0.333,0.014,0.67,0.012,1.007-0.01
- c3.163-0.191,5.572-1.942,6.888-3.166l0.452-0.453c0.021-0.019,0.04-0.041,0.06-0.061l0.034-0.034
- c-0.007,0.007-0.015,0.014-0.021,0.02C71.666,63.771,71.892,63.307,71.916,62.782z"></path>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="18.614" cy="99.426" r="3.292"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="95.364" cy="28.676" r="3.291"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="24.739" cy="93.551" r="2.667"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="101.489" cy="33.051" r="2.666"></circle>
- <circle fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" cx="18.738" cy="87.717" r="2.833"></circle>
- <path fill="#f8f587" stroke="#FEEBE6" stroke-miterlimit="10" d="M116.279,55.814c-0.021-0.286-2.323-28.744-30.221-41.012
- c-7.806-3.433-15.777-5.173-23.691-5.173c-16.889,0-30.283,7.783-37.187,15.067c-9.229,9.736-13.84,26.712-14.191,30.259
- l-0.748,62.332c0.149,2.133,1.389,6.167,5.019,6.167c1.891,0,4.074-1.083,6.672-3.311c4.96-4.251,7.424-6.295,9.226-6.295
- c1.339,0,2.712,1.213,5.102,3.762c4.121,4.396,7.461,6.355,10.833,6.355c2.713,0,5.311-1.296,7.942-3.962
- c3.104-3.145,5.701-5.239,8.285-5.239c2.116,0,4.441,1.421,7.317,4.473c2.638,2.8,5.674,4.219,9.022,4.219
- c4.835,0,8.991-2.959,11.27-5.728l0.086-0.104c1.809-2.2,3.237-3.938,5.312-3.938c2.208,0,5.271,1.942,9.359,5.936
- c0.54,0.743,3.552,4.674,6.86,4.674c1.37,0,2.559-0.65,3.531-1.932l0.203-0.268L116.279,55.814z M114.281,121.405
- c-0.526,0.599-1.096,0.891-1.734,0.891c-2.053,0-4.51-2.82-5.283-3.907l-0.116-0.136c-4.638-4.541-7.975-6.566-10.82-6.566
- c-3.021,0-4.884,2.267-6.857,4.667l-0.086,0.104c-1.896,2.307-5.582,4.999-9.725,4.999c-2.775,0-5.322-1.208-7.567-3.59
- c-3.325-3.528-6.03-5.102-8.772-5.102c-3.278,0-6.251,2.332-9.708,5.835c-2.236,2.265-4.368,3.366-6.518,3.366
- c-2.772,0-5.664-1.765-9.374-5.723c-2.488-2.654-4.29-4.395-6.561-4.395c-2.515,0-5.045,2.077-10.527,6.777
- c-2.727,2.337-4.426,2.828-5.37,2.828c-2.662,0-3.017-4.225-3.021-4.225l0.745-62.163c0.332-3.321,4.767-19.625,13.647-28.995
- c3.893-4.106,10.387-8.632,18.602-11.504c-0.458,0.503-0.744,1.165-0.744,1.898c0,1.565,1.269,2.833,2.833,2.833
- c1.564,0,2.833-1.269,2.833-2.833c0-1.355-0.954-2.485-2.226-2.764c4.419-1.285,9.269-2.074,14.437-2.074
- c7.636,0,15.336,1.684,22.887,5.004c26.766,11.771,29.011,39.047,29.027,39.251V121.405z"></path>
- </svg>
- <p class="shadowFrame"><svg version="1.1" class="shadow" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="61px" y="20px"
- width="122.436px" height="39.744px" viewBox="0 0 122.436 39.744"
- xml:space="preserve">
- <ellipse fill="#d0d0b7" cx="61.128" cy="19.872" rx="49.25" ry="8.916"></ellipse>
- </svg></p>
- </div>
- <style>
- .container1 {
- position: absolute;
- top: 60%;
- left: 20%;
- }
- .container2 {
- position: absolute;
- top: 60%;
- right: 20%;
- }
-
- .ghost {
- animation: float 3s ease-out infinite;
- }
-
- @keyframes float {
- 50% {
- transform: translate(0, 20px);
- }
- }
- .shadowFrame {
- width: 130px;
- margin-top: 15px;
- }
- .shadow {
- animation: shrink 3s ease-out infinite;
- transform-origin: center center;
-
- }
- ellipse {
- transform-origin: center center;
- }
-
- @keyframes shrink {
- 0% {
- width: 90%;
- margin: 0 5%;
- }
- 50% {
- width: 60%;
- margin: 0 18%;
- }
- 100% {
- width: 90%;
- margin: 0 5%;
- }
- }
- </style>
- <!--card-->
- <div class="card">
- <div class="box signup">
- <div class="form-content">
- <h1>SIGN UP</h1>
- <form class="form" method="post">
- {% csrf_token %}
- <div>
- <div class="fa"><img src="/static/img/account.png" style="height: 25px; width: 25px;" alt=""></div>
- <label>
- <input type="text" placeholder="Username" name="username">
- </label>
- </div>
- <div>
- <div class="fa"><img src="/static/img/password.png" style="height: 25px; width: 25px;" alt=""></div>
- <label>
- <input type="password" placeholder="Password" name="password">
- </label>
- </div>
- <div class="btn">
- <input type="submit" value="sign up">
- </div>
- </form>
- <p class="btn-something">
- Already have an account ? <span class="loginbtn">login</span>
- </p>
- </div>
- </div>
- <div class="box login">
- <div class="form-content">
- <h1>LOG IN</h1>
- <form class="form">
- <div>
- <div class="fa"><img src="/img/account.png" style="height: 25px; width: 25px;" alt=""></div>
- <label>
- <input type="text" placeholder="Username">
- </label>
- </div>
- <div>
- <div class="fa"><img src="/static/img/password.png" style="height: 25px; width: 25px;" alt=""></div>
- <label>
- <input type="password" placeholder="Password">
- </label>
- </div>
- <div class="btn">
- <button>log in</button>
- </div>
- </form>
- <p class="btn-something">
- Don't have an account ? <span class="signupbtn">signup</span>
- </p>
- </div>
- </div>
- </div>
- <style>
- .card {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- width: 20rem;
- height: 18rem;
- perspective: 1500px;
- -webkit-perspective: 1500px;
- -moz-perspective: 1500px;
- }
-
- .box {
- position: absolute;
- top: 0;
- left: 0;
- width: 20rem;
- height: 18rem;
- border-radius: 10px;
- cursor: pointer;
- backface-visibility: hidden;
- transition: all 0.5s;
- user-select: none;
- }
-
- .login {
- transform: rotateY(-108deg);
- }
-
- .card .form-content {
- position: relative;
- width: 100%;
- height: 100%;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-wrap: wrap;
- border-radius: 30px;
- background: #fafaea;
- }
-
- .card .form-content h1 {
- box-sizing: content-box;
- cursor: pointer;
- border: none;
- font: normal 3em/normal "Passero One", Helvetica, sans-serif;
- color: rgb(255, 240, 149);
- transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1) 10ms;
- text-align: center;
- text-overflow: clip;
- margin-bottom: -30px;
- text-shadow: 0 1px 0 rgba(255,255,255,1) , 0 2px 0 rgba(255,255,255,1) , 0 3px 0 rgba(255,255,255,1) , 0 4px 0 rgba(255,255,255,1) , 0 5px 0 rgba(255,255,255,1)}
-
- .card .form {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- }
-
- .card .form input {
- width: 16rem;
- padding: 0.8rem 0.8rem 0.8rem 3rem;
- outline: none;
- border: none;
- border-radius: 15px;
- box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
- margin: 5px;
- }
-
- .card .form div {
- position: relative;
- }
-
- .card .form div .fa {
- position: absolute;
- top: 12px;
- left: 20px;
- }
-
- .card .form .btn {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 100%;
- margin-top: 10px;
- }
-
- .card .form .btn button {
- text-transform: uppercase;
- padding: 8px;
- border: none;
- outline: none;
- background-color: rgb(255, 240, 149);
- color: #fff;
- width: 12rem;
- border-radius: 30px;
- cursor: pointer;
- }
-
- .card .btn-something {
- color: #888;
- font-size: 14px;
- }
-
- .card .btn-something span {
- color: rgb(255, 240, 149);
- cursor: pointer;
- font-weight: 900;
- }
-
- svg {
- position: absolute;
- bottom: 0;
- z-index: -1;
- }
- </style>
- <script>
- let login = document.querySelector(".login");
- let signup = document.querySelector(".signup");
-
- let loginbtn = document.querySelector(".loginbtn");
- let siginupbtn = document.querySelector(".signupbtn");
-
- let user = document.querySelector(".head");
-
- siginupbtn.addEventListener("click", () => {
- login.style.transform = "rotateY(180deg)"
- signup.style.transform = "rotateY(0deg)";
-
- user.innerHTML = "create account"
- })
-
- loginbtn.addEventListener("click", () => {
- login.style.transform = "rotateY(0deg)"
- signup.style.transform = "rotateY(-180deg)";
-
- user.innerHTML = "account login"
- })
- </script>
- <!--number-->
- <div id="root1" class="number"></div>
- <div id="number-name1">Number of items</div>
- <div id="root2" class="number"></div>
- <div id="number-name2">Number of users</div>
- <style>
- #root1{
- position: absolute;
- font-size: 50px;
- font-weight: 600;
- color: #fad623;
- -webkit-border-radius: 50px;
- width: 200px;
- height: 100px;
- display: -webkit-flex;
- align-items: center;
- justify-content:center;
- margin-top: 5%;
- }
- #root2{
- position: absolute;
- font-size: 50px;
- font-weight: 600;
- color: #fad623;
- -webkit-border-radius: 50px;
- width: 200px;
- height: 100px;
- display: -webkit-flex;
- align-items: center;
- justify-content:center;
- margin-top: 5%;
- right: 0;
- }
- #number-name1{
- color: darkgray;
- font-size: 20px;
- font-weight: 600;
- position: absolute;
- top: 42%;
- width: 200px;
- height: 80px;
- display: -webkit-flex;
- align-items: center;
- justify-content:center;
- }
- #number-name2{
- color: darkgray;
- font-size: 20px;
- font-weight: 600;
- position: absolute;
- top: 42%;
- right: 0;
- width: 200px;
- height: 80px;
- display: -webkit-flex;
- align-items: center;
- justify-content:center;
- }
- </style>
- <script type="text/javascript">
- function roll(total, idname, step) {
- let n = 0;
- return function () {
- n = (n + step) >= total ? total : (n + step);
- if (n <= total) {
- document.getElementById(idname).innerHTML = n;
- }
- }
- }
- function start(index, idname, step, runtime = 1000){
- let rolling = roll(index, idname, step)
- runtime = (runtime >= 300) ? runtime : 1000;
- for (let i = 0; i < (index/step); i++) {
- let timer = setTimeout(rolling, (runtime/index)*i*step)
- }
- clearTimeout(timer);
- }
- start(1200,'root1', 4,)
- </script>
- <script type="text/javascript">
- function roll(total, idname, step) {
- let n = 0;
- return function () {
- n = (n + step) >= total ? total : (n + step);
- if (n <= total) {
- document.getElementById(idname).innerHTML = n;
- }
- }
- }
- function start(index, idname, step, runtime = 1000){
- let rolling = roll(index, idname, step)
- runtime = (runtime >= 300) ? runtime : 1000;
- for (let i = 0; i < (index/step); i++) {
- let timer = setTimeout(rolling, (runtime/index)*i*step)
- }
- clearTimeout(timer);
- }
- start(5500,'root2', 4,)
- </script>
- </body>
- </html>
6.加入静态文件
在template文件夹同级添加static文件夹,static内添加img文件夹,用于放所有的相关图片
7.需要修改djangoProject中的settings.py文件 需要import os
- STATIC_URL = "/static/"
- STATICFILES_DIRS=[
- os.path.join(BASE_DIR, 'static')
- ]
8.刷新页面,效果如图
9.表单相关代码
参考链接:(1条消息) Django实现注册登录功能_django登录注册功能_秋秋xxx的博客-CSDN博客
# views.py from django.shortcuts import render, HttpResponse, redirect from django.views.generic import View from .models import User # Create your views here. class signup(View): def get(self, request): return render(request, 'signup.html') def post(self, request): username = request.POST.get("username") password = request.POST.get("password") users = User.objects.all() for i in users: if username == i.username: return HttpResponse("用户已存在") try: User.objects.create(username=username, password=password) except Exception as e: print(e) return HttpResponse("注册失败") return redirect("/signup")
# models.py from django.db import models # Create your models here. class User(models.Model): account = models.CharField(max_length=128, unique=True) # 必填,最长不超过128个字符,并且唯一,不能有相同账号; username = models.CharField(max_length=128, unique=True) # 必填,最长不超过128个字符,并且唯一,不能有相同姓名; password = models.CharField(max_length=256) # 必填,最长不超过256个字符; create_time = models.DateTimeField(auto_now_add=True) # 使用__str__帮助人性化显示对象信息; def __str__(self): return self.username class Meta: ordering = ['create_time'] # 定义用户按创建时间的反序排列,也就是最近的最先显示; verbose_name = '用户' verbose_name_plural = '用户'
10.注册或登录成功与否的通知
参考链接:(1条消息) 【Django】弹窗页面实现(view端逻辑判断)_django 弹窗_微雨停了的博客-CSDN博客
- # views.py
- from django.http import HttpResponseRedirect
- from django.shortcuts import render, HttpResponse, redirect
- from django.views.generic import View
- from .models import User
- from django.contrib import messages
-
-
- # Create your views here.
- class signup(View):
- def get(self, request):
- return render(request, 'signup.html')
-
- def post(self, request):
- saccount = request.POST.get("saccount")
- susername = request.POST.get("susername")
- spassword = request.POST.get("spassword")
-
- laccount = request.POST.get("laccount")
- lpassword = request.POST.get("lpassword")
-
- users = User.objects.all()
- for i in users:
- if saccount == i.account:
- messages.error(request, '该账号已注册!')
- return HttpResponseRedirect('/signup')
- if saccount == '' or susername == '' or spassword == '' or saccount == ' ' or susername == ' ' or spassword == ' ':
- messages.error(request, '账号或用户名或密码不能为空!')
- return HttpResponseRedirect('/signup')
- if laccount == i.account and lpassword == i.password:
- messages.success(request, '登录成功!')
- return redirect("/index")
- else:
- messages.error(request, '登陆失败!')
- return redirect("/signup")
- try:
- User.objects.create(account=saccount, username=susername, password=spassword)
- except Exception as e:
- print(e)
- return HttpResponse("注册失败")
- return redirect("/index")
-
-
- class index(View):
- def get(self, request):
- return render(request, 'index.html')
- {% if messages %}
- <script>
- {% for msg in messages %}
- alert('{{ msg.message }}');
- {% endfor %}
- </script>
- {% endif %}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。