python - multiple form classes in forms.py -
i have login/registration template (i using django) page tab login , registration of new users. means have 2 forms in 1 template. processing them using different views.
i created forms.py put 2 classes follows:
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'}))
here template:
{% 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 %}
here view, not complete yet:
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('/')
my issue in template, sees fields first class loginform (username , password) , ignores other fields. suggestions regarding issue?
make sure pass 2 forms under 2 different context names when rendering template.
for example (in view):
context = {'login_form': loginform(), 'register_form': registerform()} return render(request, 'your/template.html', context)
and in template, use them accordingly:
<div class="form-group"> {{ login_form.username.errors }} {{ login_form.username }} </div>
and:
<div class="form-group"> {{ register_form.first_name.errors }} {{ register_form.first_name }} </div>
btw convention in python use underscores rather camel case variable names. first_name
rather firstname
.
btw 2 it's redundant have {{ form.as_p }}
, render each field separately. (e.g. {{ form.username }}
). either should enough.
Comments
Post a Comment