Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django中基于类的视图中的Post方法不起作用_Django - Fatal编程技术网

Django中基于类的视图中的Post方法不起作用

Django中基于类的视图中的Post方法不起作用,django,Django,我正在Django中制作一个简单的登录和注册模块。我正在使用一个基于类的视图,其中有一个函数可以从HTML表单中获取数据并将其保存在数据库中。但是它没有通过请求。method=='Post'条件,因为我正在打印“In-Post”,它没有显示在终端中 View.py class MainView(TemplateView): template_name='main.html' def post(self,request): if request.method=='p

我正在Django中制作一个简单的登录和注册模块。我正在使用一个基于类的视图,其中有一个函数可以从HTML表单中获取数据并将其保存在数据库中。但是它没有通过请求。method=='Post'条件,因为我正在打印“In-Post”,它没有显示在终端中

View.py

class MainView(TemplateView):
    template_name='main.html'
    def post(self,request):
        if request.method=='post':
            print("In Post")
            email=request.POST.get('user_email')
            username=request.POST.get('user_name')
            password=request.POST.get('user_pass')

            hashed_password=make_password(password)
            print(hashed_password,email,username)
            UserModel.objects.create(email=email,username=username,password=hashed_password)
        else:
            return render(request,'main.html')
main.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="{% static 'login.css' %}">
    <script src="{% static 'login.js' %}"></script>
    <title>Main</title>
</head>
<body>
    <div class="form-wrap">
        <div class="tabs">
            <h3 class="signup-tab"><a class="active" href="#signup-tab-content">Sign Up</a></h3>
            <h3 class="login-tab"><a href="#login-tab-content">Login</a></h3>
        </div><!--.tabs-->

        <div class="tabs-content">
            <div id="signup-tab-content" class="active">
                <form class="signup-form" action="" method="post">
                    {% csrf_token %}
                    <input type="email" class="input" id="user_email" autocomplete="off" placeholder="Email">
                    <input type="text" class="input" id="user_name" autocomplete="off" placeholder="Username">
                    <input type="password" class="input" id="   " autocomplete="off" placeholder="Password">
                    <input type="submit" class="button" value="Sign Up">
                </form><!--.login-form-->
                <div class="help-text">
                    <p>By signing up, you agree to our</p>
                    <p><a href="#">Terms of service</a></p>
                </div><!--.help-text-->
            </div><!--.signup-tab-content-->

            <div id="login-tab-content">
                <form class="login-form" action="" method="post">
                    {% csrf_token %}
                    <input type="text" class="input" id="user_login" autocomplete="off" placeholder="Email or Username">
                    <input type="password" class="input" id="user_pass" autocomplete="off" placeholder="Password">
                    <input type="checkbox"from django.contrib.auth.hashers import make_password class="checkbox" id="remember_me">
                    <label for="remember_me">Remember me</label>

                    <input type="submit" class="button" value="Login">
                </form><!--.login-form-->
                <div class="help-text">
                    <p><a href="#">Forget your password?</a></p>
                </div><!--.help-text-->
            </div><!--.login-tab-content-->
        </div><!--.tabs-content-->
    </div><!--.form-wrap-->
</body>
</html>
{%load static%}
主要
{%csrf_令牌%}
通过注册,您同意我们的

{%csrf_令牌%} 记得我吗


尝试用大写字母进行比较:
request.method=='POST'