Django POST - 无法检索表单数据,POST检查失败

时间:2017-09-12 17:32:15

标签: python django post

我对Django很新。我试图设计我的登录模块并尝试打印我提交的值。

  1. login.html(请原谅缩进错误)

    {% load staticfiles %}
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Login Form</title>
        <link rel="stylesheet" href="{% static 'css/lstyle.css' %}"> 
    </head>
    
    <body>
    {% block content %}
    {% if form.errors %}
        <script> alert("Your username and password didn't match. Please try 
    again."); </script>
    {% endif %}
    
    {% if next %}
    {% if user.is_authenticated %}
        <script> alert("Your account doesn't have access to this page. To 
        proceed,please login with an account that has access."); </script>
    {% else %}
        <script> alert('Please login'); </script>
    {% endif %}
    {% endif %}
    <div class="login">
        <div class="login-screen">
            <div class="app-title">
                <h1>Login</h1>
            </div>
            <div class="login-form">
                    <form method="POST" action="{% url 'login' %}">
    {% csrf_token %}
                        <p class="bs-component">
                            <table>
                                <tr>
                                    <td>{{ form.username }}</td>
                                </tr>
                                <tr>
                                    <td>{{ form.password }}</td>
                               </tr>
                                <tr>
                                    <td>{{ form.schoolID }}</td>
                                </tr>
                            </table>
                        </p>
                        <p class="bs-component">
                            <center>
                                <input class="btn btn-success btn-sm" 
                                type="submit" value="login" />
                            </center>
                        </p>
                        <input type="hidden" name="next" value="{{ next }}" 
                        />
                    </form>
            </div>
        </div>
    </div>
    </body>
    {% endblock %}
    </html>
    
  2. Forms.py

    from django.contrib.auth.forms import AuthenticationForm
    from django import forms
    
    
    # If you don't do this you cannot use Bootstrap CSS
    class LoginForm(AuthenticationForm):
          username = forms.CharField(max_length=30,
                           widget=forms.TextInput(attrs={'class': 
                           'form-control', 'name': 'username', 'placeholder': 'Username'}))
          password = forms.CharField(max_length=30,
                           widget=forms.PasswordInput(attrs={'class': 
                           'form-control', "name": 'password', 'placeholder': 'Password'}))
          schoolID = forms.CharField(max_length=100,
                           widget=forms.TextInput(attrs={'class': 
                           'form-control', 'name': 'schoolid', 'placeholder': 'School ID'}))
    
  3. Views.py

    from django.shortcuts import render, redirect
    from django.contrib.auth.decorators import login_required
    from home.forms import LoginForm
    from home.models import UserProfile
    from django.views.generic import TemplateView
    from django.contrib.auth.models import User
    
    
    @login_required(login_url="login/")
    def home(request):
        if request.method == 'POST':
            myform = LoginForm(request.POST)
            print(myform.data['schoolID'])
            return render(request, "home.html")
    
  4. 工作正常:

    1. html文件渲染完美。

    2. 用户身份验证有效

    3. 如果“return render(request,”home.html“)”放在if块之外,代码运行完美,我将被重定向到home.html - 输入匹配的用户名和密码。< / p>

    4. 事情不起作用:

      1. 无法打印schoolID。即,“if request.method =='POST':”未被执行。

      2. 错误:视图home.views.home未返回HttpResponse对象。它改为返回None。

      3. 我在这里做错了什么。我只想在执行return语句之前打印表单数据并对其进行操作。请帮忙。

2 个答案:

答案 0 :(得分:0)

<强>尝试:

\0

在home.html中:将@login_required(login_url="login/") def home(request): context={} if request.method == 'POST': myform = LoginForm(request.POST) if myform.is_valid(): myform.save() context['schoolid'] = myform.cleaned_data.get('schoolID') return render(request, "home.html",context) else: context['myform'] = LoginForm() return render(request, "home.html",context) 打印为

schoolid

答案 1 :(得分:0)

使用myform = LoginForm(data=request.POST)

myform = LoginForm(None , request.POST)

因为请求不是第一个参数。