How to raise an error in template by form.error.as_text?
up vote
0
down vote
favorite
I cant understand how to show error in template without redirecting on error page.
My task is to check existence of email in database and if its doesnt exist raise an error in form.
Im using Django 2.1 and Python 3.7
What i have: django/auth/views.py
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_for.html'
title = _('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
form.save(**opts)
return super().form_valid(form)
else:
raise forms.ValidationError('Email is not in database')
return email
its standart django class and im just trying to customize it a little bit.
Its works but redirects user on error page.
And here is a form of password reset
class PasswordResetForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
def check_email(self, email):
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
return email
else:
return forms.ValidationError('Email does not exist')
Ive tryed to add check method in form but its not working(
And here is code of my template:
{% if form.errors %}
{{ form.errors.as_text }}
{% endif %}
<form method="post">
{% csrf_token %}
<label for="id_email">Email:</label>
<input type="email" name="email" maxlength="254" required="" id="id_email">
<input type="submit">
</form>
python django
add a comment |
up vote
0
down vote
favorite
I cant understand how to show error in template without redirecting on error page.
My task is to check existence of email in database and if its doesnt exist raise an error in form.
Im using Django 2.1 and Python 3.7
What i have: django/auth/views.py
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_for.html'
title = _('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
form.save(**opts)
return super().form_valid(form)
else:
raise forms.ValidationError('Email is not in database')
return email
its standart django class and im just trying to customize it a little bit.
Its works but redirects user on error page.
And here is a form of password reset
class PasswordResetForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
def check_email(self, email):
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
return email
else:
return forms.ValidationError('Email does not exist')
Ive tryed to add check method in form but its not working(
And here is code of my template:
{% if form.errors %}
{{ form.errors.as_text }}
{% endif %}
<form method="post">
{% csrf_token %}
<label for="id_email">Email:</label>
<input type="email" name="email" maxlength="254" required="" id="id_email">
<input type="submit">
</form>
python django
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I cant understand how to show error in template without redirecting on error page.
My task is to check existence of email in database and if its doesnt exist raise an error in form.
Im using Django 2.1 and Python 3.7
What i have: django/auth/views.py
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_for.html'
title = _('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
form.save(**opts)
return super().form_valid(form)
else:
raise forms.ValidationError('Email is not in database')
return email
its standart django class and im just trying to customize it a little bit.
Its works but redirects user on error page.
And here is a form of password reset
class PasswordResetForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
def check_email(self, email):
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
return email
else:
return forms.ValidationError('Email does not exist')
Ive tryed to add check method in form but its not working(
And here is code of my template:
{% if form.errors %}
{{ form.errors.as_text }}
{% endif %}
<form method="post">
{% csrf_token %}
<label for="id_email">Email:</label>
<input type="email" name="email" maxlength="254" required="" id="id_email">
<input type="submit">
</form>
python django
I cant understand how to show error in template without redirecting on error page.
My task is to check existence of email in database and if its doesnt exist raise an error in form.
Im using Django 2.1 and Python 3.7
What i have: django/auth/views.py
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_for.html'
title = _('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
form.save(**opts)
return super().form_valid(form)
else:
raise forms.ValidationError('Email is not in database')
return email
its standart django class and im just trying to customize it a little bit.
Its works but redirects user on error page.
And here is a form of password reset
class PasswordResetForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
def check_email(self, email):
email = form.cleaned_data['email']
if User.objects.filter(email=email).exists():
return email
else:
return forms.ValidationError('Email does not exist')
Ive tryed to add check method in form but its not working(
And here is code of my template:
{% if form.errors %}
{{ form.errors.as_text }}
{% endif %}
<form method="post">
{% csrf_token %}
<label for="id_email">Email:</label>
<input type="email" name="email" maxlength="254" required="" id="id_email">
<input type="submit">
</form>
python django
python django
edited Nov 10 at 4:53
Bast
374111
374111
asked Nov 9 at 21:28
Andrew Blacker
1
1
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27
add a comment |
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233482%2fhow-to-raise-an-error-in-template-by-form-error-as-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have you read the docs? (docs.djangoproject.com/en/2.1/topics/forms)
– Jack Herer
Nov 10 at 1:26
Maybe look into wtforms aswell (wtforms.readthedocs.io)
– Jack Herer
Nov 10 at 1:27