forms.py中的多个表单类

时间:2017-04-07 09:57:35

标签: python django web

我有一个登录/注册模板(我正在使用Django)页面,其中有一个用于登录的选项卡,另一个用于注册新用户。这意味着我在一个模板中有两个表单。我正在使用不同的视图处理它们。

我创建了一个forms.py,其中我放了两个类,如下所示:

    from django import forms


class LoginForm(forms.Form):
    username = forms.CharField(label="Username", max_length=30, widget=forms.TextInput(
        attrs={'name': "username",
               'id': "username",
               'tabindex': "2",
               'class': "form-control",
               'placeholder': 'Username'}))
    password = forms.CharField(label="Password", widget=forms.PasswordInput(
        attrs={'name': "password",
               'id': "password",
               'tabindex': "2",
               'class': 'form-control',
               'placeholder': 'Password'}))


class RegisterForm(forms.Form):
    firstNameReg = forms.CharField(label="First Name", max_length=30, widget=forms.TextInput(
        attrs={'name': "firstname",
               'id': "firstname",
               'tabindex': "1",
               'class': "form-control",
               'placeholder': 'First Name'}))
    lastNameReg = forms.CharField(label="Last Name", widget=forms.TextInput(
        attrs={'name': "lastname",
               'id': "lastname",
               'tabindex': "1",
               'class': 'form-control',
               'placeholder': 'Last Name'}))
    usernameReg = forms.CharField(label="Username", max_length=30, widget=forms.TextInput(
        attrs={'name': "username",
               'id': "username",
               'tabindex': "1",
               'class': "form-control",
               'placeholder': 'Username'}))
    emailReg = forms.CharField(label="Email", max_length=30, widget=forms.EmailInput(
        attrs={'name': "username",
               'id': "username",
               'tabindex': "1",
               'class': "form-control",
               'placeholder': 'Email'}))
    emailRegConfirm = forms.CharField(label="Email", max_length=30, widget=forms.EmailInput(
        attrs={'name': "username",
               'id': "username",
               'tabindex': "1",
               'class': "form-control",
               'placeholder': 'Confirm Email'}))
    passwordReg = forms.CharField(label="Password", widget=forms.PasswordInput(
        attrs={'name': "password",
               'id': "password",
               'tabindex': "2",
               'class': 'form-control',
               'placeholder': 'Password'}))
    passwordRegConfirm = forms.CharField(label="Password", widget=forms.PasswordInput(
        attrs={'name': "confirm-password",
               'id': "confirm-password",
               'tabindex': "2",
               'class': 'form-control',
               'placeholder': 'Confirm Email'}))

这是我的模板:

{% extends 'game_webstore/base.html' %}
{% load staticfiles %}
{% block body %}


    <link rel="stylesheet" href="{% static "game_webstore/login_css.css" %}">
    <script src="{% static "game_webstore/login.js" %}"></script>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-login">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-xs-6">
                                <a href="#" class="active" id="login-form-link">Login</a>
                            </div>
                            <div class="col-xs-6">
                                <a href="#" id="register-form-link">Register</a>
                            </div>
                        </div>
                        <hr>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-12">
                                <form id="login-form" action="" method="post" role="form" style="display: block;">
                                    {% csrf_token %}
                                    {% if error %}
                                        <p style="color: red"><strong>* Wrong Username or Password.</strong></p>
                                    {% endif %}
                                    <div class="form-group">
                                        {{ form.username.errors }}
                                        {{ form.username }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.password.errors }}
                                        {{ form.password }}
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-6 col-sm-offset-3">
                                                <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-lg-12">
                                                <div class="text-center">
                                                    <a href="" tabindex="5" class="forgot-password">Forgot Password?</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                                <form id="register-form" action="." method="post" role="form" style="display: none;">
                                    {{ form.as_p }}
                                    <div class="form-group">
                                        {{ form.firstNameReg.errors }}
                                        {{ form.firstNameReg }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.lastNameReg.errors }}
                                        {{ form.lastNameReg }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.usernameReg.errors }}
                                        {{ form.usernameReg }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.emailReg.errors }}
                                        {{ form.emailReg }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.emailRegConfirm.errors }}
                                        {{ form.emailRegConfirm }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.passwordReg.errors }}
                                        {{ form.passwordReg }}
                                    </div>
                                    <div class="form-group">
                                        {{ form.passwordRegConfirm.errors }}
                                        {{ form.passwordRegConfirm }}
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-6 col-sm-offset-3">
                                                <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock body %}

以下是我的观点,尚未完成:

def register_view(request):
    error = False

    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(False)
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user.username, password=request.POST['password1'])
            login(request, user)

            return redirect('/')

我的问题是,在模板中,它只能看到第一个类LoginForm(用户名和密码)中的字段,并忽略所有其他字段。关于这个问题的任何建议?

1 个答案:

答案 0 :(得分:1)

确保在渲染模板时在两个不同的上下文名称下传递2个表单。

例如(在视图中):

context = {'login_form': LoginForm(), 'register_form': RegisterForm()}
return render(request, 'your/template.html', context)

然后在模板中,相应地使用它们:

<div class="form-group">
    {{ login_form.username.errors }}
    {{ login_form.username }}
</div>

<div class="form-group">
    {{ register_form.first_name.errors }}
    {{ register_form.first_name }}
</div>

BTW Python中的约定是使用下划线而不是驼峰大小写变量名。所以first_name而不是firstName

BTW 2 拥有{{ form.as_p }}并分别渲染每个字段是多余的。 (例如{{ form.username }})。要么足够了。