2017-02-12 101 views
2

我在我的django應用程序中有兩種形式,我使用bootstrap3來定製它們,但是窗體的錯誤消息並不相同。Bootstrap窗體顯示不同風格的錯誤消息

這一切是葡萄牙語

enter image description here

我的模板使用的是相同的結構,我不知道爲什麼會有差別。

的login.html

{% extends 'base.html' %} 
{% load crispy_forms_tags %} 
{% load bootstrap3 %} 

<!DOCTYPE html> 
<html lang="pt-br"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
{% block content %} 
    <div class="row"> 
    <div class="col-sm-4"> 
     <form method="POST" action=""> 
       {% csrf_token %} 
       {% bootstrap_form form %} 
       <input class='btn btn-primary' type="submit" value="Logar"> 
      </form></br></br> 


    </div> 

    </div> 

{% endblock %} 
</body> 
</html> 

signup.html

{% extends 'base.html' %} 
{% load crispy_forms_tags %} 
{% load bootstrap3 %} 


<!DOCTYPE html> 
<html lang="pt-br"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
{% block content %} 
    <div class="row"> 
    <div class="col-sm-4"> 
<form method="POST" action=""> 
     {% csrf_token %} 
     {% bootstrap_form form %} 
     <input class='btn btn-primary' type="submit" value="Cadastrar"> 
    </form> 
    </div> 

    </div> 
{% endblock %} 

</body> 
</html> 

forms.py

from django.contrib.auth import get_user_model, authenticate 
from django.contrib.auth.forms import UserCreationForm 
from django.contrib.auth.forms import AuthenticationForm 
from .models import User 
from django import forms 


class UserCreateForm(UserCreationForm): 
    class Meta: 
     fields = ("username","email", "password1","password2") 
     model = get_user_model() 

    def __init__(self, *args, **kwargs): 
     super().__init__(*args, **kwargs) 
     self.fields["username"].label = "Usuário" 
     self.fields["email"].label = "Email" 
     self.fields["password2"].help_text = None 
     #self.fields["email"].widget.attrs['readonly']=True 

    def clean_email(self): 

     email = self.cleaned_data['email'].lower() 
     try: 
      u = User.objects.get(email=email) 
     except User.DoesNotExist: 
      return email 
     raise forms.ValidationError('Email já em uso') 
    # return self.cleaned_data['email'].lower() 


class UserLogin(AuthenticationForm): 

    def clean(self): 
     username = self.cleaned_data.get('username') 
     password = self.cleaned_data.get('password') 

     if username and password: 
      self.user_cache = authenticate(username=username, password=password) 
      if self.user_cache is None: 
       raise forms.ValidationError(
        ("Email ou Senha errados")) 
      elif not self.user_cache.is_active: 
       raise forms.ValidationError(("This account is inactive.")) 
     return self.cleaned_data 

base.html文件

{% load staticfiles %} 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <link rel="icon" href="../../favicon.ico"> 

    <title>Static Top Navbar Example for Bootstrap</title> 

    {% include 'header_css.html' %} 



    </head> 

    <body> 
    {% include 'navbar.html' %} 

    {% block jumbotron %} 
    <div class="container"> 

     <!-- Main component for a primary marketing message or call to action --> 
     <div class="jumbotron"> 
     {% block jumbotron_content %} 
     {% endblock %} 
     </div> 

    {% block content %} 
    {% endblock %} 


    </div> <!-- /container --> 
    {% endblock jumbotron %} 

    <!-- Bootstrap core JavaScript 
    ================================================== --> 
    <!-- Placed at the end of the document so the pages load faster --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script> 
    <script src="{% static 'js/bootstrap.min.js' %}"></script> 
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> 
    <script src="{% static 'js/ie10-viewport-bug-workaround.js' %}"></script> 
    </body> 
</html> 

回答

0

第一個屏幕截圖將錯誤消息應用於整個表單(第NON_FIELD_ERRORS),第二個屏幕看起來像是將錯誤應用於各個字段本身。在左邊的屏幕截圖中,錯誤文本正在打印在頂部,因爲錯誤已應用於整個表單,而不是特定的字段。

UserCreateForm中,您正在通過在clean_email方法中進行驗證將錯誤應用於email字段。在UserLogin這是一般clean

請參閱https://docs.djangoproject.com/en/1.10/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

你可以使用add_error方法來指定你想要的錯誤,複製右手截圖領域,或add_error(None,...複製左側的格式(或者只是使用clean代替clean_email)。

參見:https://docs.djangoproject.com/en/1.10/ref/forms/api/#django.forms.Form.add_error

0

它看起來像signup.html和login.html的頁面使用不同的引導方式(可能是一個文件仍在高速緩存)。

您是否在瀏覽器中嘗試過「硬刷新」(CTRL +點擊Chrome中的刷新按鈕,Firefox中的CTRL + F5)?

+0

他們使用相同的風格,我編輯我的職務與我的形式原imagens,可惜刷新頁面沒有工作,但即便如此,謝謝。 – Goun2

+0

你能否提供你的forms.py和base.html? – doggra

+0

編輯............... – Goun2