Django表单未显示

时间:2018-07-20 09:46:45

标签: python django

我正在尝试使用Django和Bootstrap创建一个简单的登录表单。我使用了有关结合Bootstrap和Django的教程,但是它不起作用。我检查了我的代码几次,并检查了另一个主题,但没有找到解决方案。

所以,这是我的 views.py

from django.shortcuts import render
from .forms import login_form
# Create your views here.
from django.http import *

def index(request):
    if request.method == "POST":
        name = request.POST.get("email")
        # age = request.POST.get("age")     # getting age
        return HttpResponse("<h2>Hello, {0}</h2>".format(name))
    else:
        userform = login_form()
        return render(request, "index.html", {"form": userform})

index.html

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="{% static 'css/styles.css' %}">
    <meta charset="UTF-8">
    <title>Password Generator Main Page</title>
    <script>
        $(document).ready(function(){
            $("#login-form-link").click(function(){
                $(".register-form").hide();
                $(".login-form").show();

            });
            $("#register-form-link").click(function(){
                $(".login-form").hide();
                $(".register-form").show();
            });
        });
    </script>
</head>
<body>
<div class="main-block">
    <div class="login-signup">
        <div class="login-menu">
            <br/>
            <p class="log-reg-link"><a href="#"  id="login-form-link">Login</a></p>
        </div>
        <div class="signup-menu">
                <br/>
               <p class="log-reg-link"> <a href="#"  id="register-form-link" class="active">Sign Up</a></p>
        </div>

    </div>
    <div class="form-holder">
        <form class="login-form" action="#" method="post" style="display: block;">
            {% csrf_token %}
            <table>
                {{ login_form }}
            </table>

            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">
                    <button type="submit" class="btn btn-lg btn-primary">Login</button>
                </div>
            </div>
        </form>

forms.py

from django import forms

class login_form(forms.Form):
    email = forms.EmailField(widget=forms.EmailInput(
        attrs={
            'class':'form-control',
            #'placeholder':'Enter email',
        }
    ))
    password = forms.CharField(widget=forms.PasswordInput(
        attrs={
            'class':'form-control',
            #'placeholder':'Enter password',
        }
    ) )

如果您能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用 {{form}} 代替发送给前端的 {{login_form}}