如果表单在django中无效,则打印表单字段的输入值

时间:2016-09-22 07:37:51

标签: python django

这是我的,我想显示在提交此表单之前输入的所有值,如果此表单无效。当我提交此表单时,验证成功运行,但它会从表单字段中删除所有值

class PromoterRegistrationForm(forms.Form):
    email = forms.EmailField(required = True, max_length=50, widget=forms.EmailInput(attrs={
        'class' : 'custome-input promote-input', 
        'autocomplete' : 'off',
        'required' : 'required',
        'data-empty-message' : 'Please enter a email.',
        'data-error-message' : 'Please enter a valid email.'
    }))
    password = forms.CharField(required = True, widget=forms.PasswordInput(attrs={
        'class' : 'custome-input promote-input',
        'autocomplete' : 'off',
        'required' : 'required',
        'data-empty-message' : 'Please enter password.'
    }))
    firstname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
        'class' : 'custome-input promote-input', 
        'autocomplete' : 'off',
        'required' : 'required',
        'data-empty-message':'Please enter first name.' 
    }))
    lastname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
        'class' : 'custome-input promote-input', 
        'autocomplete' : 'off',
        'required' : 'required',
        'data-empty-message':'Please enter last name.' 
    }))

查看是: -

def signup(request):
promoterRegistrationForm = PromoterRegistrationForm()
if request.method == 'POST':
    promoterRegistrationForm = PromoterRegistrationForm(request.POST)
    if promoterRegistrationForm.is_valid():
        #code for saving data
    return render(request, 'home/signup.py', {'promoterRegistrationForm':promoterRegistrationForm})
else:
    promoterRegistrationForm = PromoterRegistrationForm()
    return render(request, 'home/signup.py', {'promoterRegistrationForm':promoterRegistrationForm})

模板是:

<form id="promoter-registration-form" action="signup" method="POST">
    {{ promoterRegistrationForm.non_field_errors }}
    {% csrf_token %}
    <div class="page-login-outer register-modal-outer">
        <div class="login-div">
            <div class="login-form custom-text-boxs">
            {{ promoterRegistrationForm.email }}
                <label class="custom-text-box">Email*</label>
                {% if promoterRegistrationForm.email.errors %}
                    <ul class="error-ul">
                    {% for error in promoterRegistrationForm.email.errors %}
                        <li style="color:red;">{{ error|escape }}</li>
                    {% endfor %}
                    </ul>
                {% endif %}
            </div>
        </div>
    </div>
</form>

0 个答案:

没有答案