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>









share|improve this question
























  • 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

















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>









share|improve this question
























  • 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















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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check