我在我的django應用程序中有兩種形式,我使用bootstrap3來定製它們,但是窗體的錯誤消息並不相同。Bootstrap窗體顯示不同風格的錯誤消息
這一切是葡萄牙語
我的模板使用的是相同的結構,我不知道爲什麼會有差別。
的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>
他們使用相同的風格,我編輯我的職務與我的形式原imagens,可惜刷新頁面沒有工作,但即便如此,謝謝。 – Goun2
你能否提供你的forms.py和base.html? – doggra
編輯............... – Goun2