本文整理汇总了Python中forms.TestForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python TestForm.is_valid方法的具体用法?Python TestForm.is_valid怎么用?Python TestForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.TestForm
的用法示例。
在下文中一共展示了TestForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_error_and_help_inline
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_error_and_help_inline(self):
form = TestForm({'email': 'invalidemail'})
form.helper = FormHelper()
form.helper.error_text_inline = False
form.helper.help_text_inline = True
form.helper.layout = Layout('email')
form.is_valid()
html = render_crispy_form(form)
# Check that help goes before error, otherwise CSS won't work
if settings.CRISPY_TEMPLATE_PACK == 'bootstrap':
help_position = html.find('<span id="hint_id_email" class="help-inline">')
error_position = html.find('<p id="error_1_id_email" class="help-block">')
self.assertTrue(help_position < error_position)
# Viceversa
form = TestForm({'email': 'invalidemail'})
form.helper = FormHelper()
form.helper.error_text_inline = True
form.helper.help_text_inline = False
form.helper.layout = Layout('email')
form.is_valid()
html = render_crispy_form(form)
# Check that error goes before help, otherwise CSS won't work
if settings.CRISPY_TEMPLATE_PACK == 'bootstrap':
error_position = html.find('<span id="error_1_id_email" class="help-inline">')
help_position = html.find('<p id="hint_id_email" class="help-block">')
self.assertTrue(error_position < help_position)
示例2: test_form_show_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form_show_errors(self):
form = TestForm(
{
"email": "invalidemail",
"first_name": "first_name_too_long",
"last_name": "last_name_too_long",
"password1": "yes",
"password2": "yes",
}
)
form.helper = FormHelper()
form.helper.layout = Layout(
AppendedText("email", "whatever"),
PrependedText("first_name", "blabla"),
AppendedPrependedText("last_name", "foo", "bar"),
MultiField("legend", "password1", "password2"),
)
form.is_valid()
form.helper.form_show_errors = True
html = render_crispy_form(form)
self.assertEqual(html.count("error"), 6)
form.helper.form_show_errors = False
html = render_crispy_form(form)
self.assertEqual(html.count("error"), 0)
示例3: test_form_show_errors_non_field_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form_show_errors_non_field_errors(self):
form = TestForm({'password1': 'wargame', 'password2': 'god'})
form.helper = FormHelper()
form.helper.form_show_errors = True
form.is_valid()
template = get_template_from_string(u"""
{% load crispy_forms_tags %}
{% crispy testForm %}
""")
# First we render with errors
c = Context({'testForm': form})
html = template.render(c)
# Ensure those errors were rendered
self.assertTrue('<li>Passwords dont match</li>' in html)
self.assertTrue(unicode(_('This field is required.')) in html)
self.assertTrue('error' in html)
# Now we render without errors
form.helper.form_show_errors = False
c = Context({'testForm': form})
html = template.render(c)
# Ensure errors were not rendered
self.assertFalse('<li>Passwords dont match</li>' in html)
self.assertFalse(unicode(_('This field is required.')) in html)
self.assertFalse('error' in html)
示例4: test_form_show_errors_non_field_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form_show_errors_non_field_errors(self):
form = TestForm({"password1": "wargame", "password2": "god"})
form.helper = FormHelper()
form.helper.form_show_errors = True
form.is_valid()
template = get_template_from_string(
u"""
{% load crispy_forms_tags %}
{% crispy testForm %}
"""
)
# First we render with errors
c = Context({"testForm": form})
html = template.render(c)
# Ensure those errors were rendered
self.assertTrue("<li>Passwords dont match</li>" in html)
self.assertTrue(unicode(_("This field is required.")) in html)
self.assertTrue("error" in html)
# Now we render without errors
form.helper.form_show_errors = False
c = Context({"testForm": form})
html = template.render(c)
# Ensure errors were not rendered
self.assertFalse("<li>Passwords dont match</li>" in html)
self.assertFalse(unicode(_("This field is required.")) in html)
self.assertFalse("error" in html)
示例5: test_as_crispy_errors_form_without_non_field_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_as_crispy_errors_form_without_non_field_errors(self):
template = get_template_from_string(u"""
{% load crispy_forms_tags %}
{{ form|as_crispy_errors }}
""")
form = TestForm({'password1': "god", 'password2': "god"})
form.is_valid()
c = Context({'form': form})
html = template.render(c)
self.assertFalse("errorMsg" in html or "alert" in html)
示例6: demo_form_with_template
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def demo_form_with_template(request):
layout = request.GET.get('layout')
if not layout:
layout = 'vertical'
if request.method == 'POST':
form = TestForm(request.POST)
form.is_valid()
else:
form = TestForm()
modelform = TestModelForm()
return render_to_response('form_using_template.html', RequestContext(request, {
'form': form,
'layout': layout,
}))
示例7: test_form
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form(request):
layout = request.GET.get('layout')
if not layout:
layout = 'vertical'
if request.method == 'POST':
form = TestForm(request.POST)
form.is_valid()
else:
form = TestForm()
form.fields['title'].widget = BootstrapUneditableInput()
return render_to_response('form.html', RequestContext(request, {
'form': form,
'layout': layout,
}))
示例8: test_as_crispy_errors_form_with_non_field_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_as_crispy_errors_form_with_non_field_errors(self):
template = get_template_from_string(
u"""
{% load crispy_forms_tags %}
{{ form|as_crispy_errors }}
"""
)
form = TestForm({"password1": "god", "password2": "wargame"})
form.is_valid()
c = Context({"form": form})
html = template.render(c)
self.assertTrue("errorMsg" in html or "alert" in html)
self.assertTrue("<li>Passwords dont match</li>" in html)
self.assertFalse("<h3>" in html)
示例9: test_multifield_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_multifield_errors(self):
form = TestForm({"email": "invalidemail", "password1": "yes", "password2": "yes"})
form.helper = FormHelper()
form.helper.layout = Layout(MultiField("legend", "email"))
form.is_valid()
form.helper.form_show_errors = True
html = render_crispy_form(form)
self.assertEqual(html.count("error"), 3)
# Reset layout for avoiding side effects
form.helper.layout = Layout(MultiField("legend", "email"))
form.helper.form_show_errors = False
html = render_crispy_form(form)
self.assertEqual(html.count("error"), 0)
示例10: testEdit
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def testEdit(request, mode, id = 0):
render = render_options(request)
if request.method == 'GET':
if mode == 'add':
form = TestForm()
elif mode == 'edit':
form = TestForm(instance = get_object_or_404(Test, id = id))
else:
get_object_or_404(Test, id = id).delete()
return HttpResponseRedirect('/tests/test/')
form.fields['subject'].queryset = request.user.subjects
form.fields['grades'].queryset = request.user.grades
del form.fields['share']
render['form'] = form
return render_to_response('tests/test.html', render)
else:
if mode == 'add':
form = TestForm(request.POST)
if form.is_valid():
test = form.save(commit = False)
test.teacher = request.user
test.save()
form.save_m2m()
return HttpResponseRedirect('/tests/test/')
else:
form.fields['subject'].queryset = request.user.subjects
form.fields['grades'].queryset = request.user.grades
del form.fields['share']
render['form'] = form
return render_to_response('tests/test.html', render)
elif mode == 'edit':
form = TestForm(request.POST, instance = get_object_or_404(Test, id = id))
if form.is_valid():
form.save()
return HttpResponseRedirect('/tests/test/')
else:
form.fields['subject'].queryset = request.user.subjects
form.fields['grades'].queryset = request.user.grades
del form.fields['share']
render['form'] = form
return render_to_response('tests/test.html', render)
else:
return HttpResponseRedirect('/tests/test/')
示例11: test
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test(request):
from forms import TestForm
if request.method == 'POST':
form = TestForm(request.POST)
if form.is_valid():
# code handle form and file data
return render_to_response('test.html', {'form': form, 'succ': True})
else:
form = TestForm()
return render_to_response('test.html', {'form': form})
示例12: test_form_with_helper_without_layout
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form_with_helper_without_layout(self):
form_helper = FormHelper()
form_helper.form_id = "this-form-rocks"
form_helper.form_class = "forms-that-rock"
form_helper.form_method = "GET"
form_helper.form_action = "simpleAction"
form_helper.form_error_title = "ERRORS"
template = get_template_from_string(
u"""
{% load crispy_forms_tags %}
{% crispy testForm form_helper %}
"""
)
# now we render it, with errors
form = TestForm({"password1": "wargame", "password2": "god"})
form.is_valid()
c = Context({"testForm": form, "form_helper": form_helper})
html = template.render(c)
# Lets make sure everything loads right
self.assertTrue(html.count("<form"), 1)
self.assertTrue("forms-that-rock" in html)
self.assertTrue('method="get"' in html)
self.assertTrue('id="this-form-rocks"' in html)
self.assertTrue('action="%s"' % reverse("simpleAction") in html)
if settings.CRISPY_TEMPLATE_PACK == "uni_form":
self.assertTrue('class="uniForm' in html)
self.assertTrue("ERRORS" in html)
self.assertTrue("<li>Passwords dont match</li>" in html)
# now lets remove the form tag and render it again. All the True items above
# should now be false because the form tag is removed.
form_helper.form_tag = False
html = template.render(c)
self.assertFalse("<form" in html)
self.assertFalse("forms-that-rock" in html)
self.assertFalse('method="get"' in html)
self.assertFalse('id="this-form-rocks"' in html)
示例13: test_error_text_inline
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_error_text_inline(self):
form = TestForm({'email': 'invalidemail'})
form.helper = FormHelper()
layout = Layout(
AppendedText('first_name', 'wat'),
PrependedText('email', '@'),
AppendedPrependedText('last_name', '@', 'wat'),
)
form.helper.layout = layout
form.is_valid()
html = render_crispy_form(form)
matches = re.findall('<span id="error_\d_\w*" class="help-inline"', html, re.MULTILINE)
self.assertEqual(len(matches), 3)
form = TestForm({'email': 'invalidemail'})
form.helper = FormHelper()
form.helper.layout = layout
form.helper.error_text_inline = False
html = render_crispy_form(form)
matches = re.findall('<p id="error_\d_\w*" class="help-block"', html, re.MULTILINE)
self.assertEqual(len(matches), 3)
示例14: test_form_show_errors
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def test_form_show_errors(self):
form = TestForm({
'email': 'invalidemail',
'first_name': 'first_name_too_long',
'last_name': 'last_name_too_long',
'password1': 'yes',
'password2': 'yes',
})
form.helper = FormHelper()
form.helper.layout = Layout(
AppendedText('email', 'whatever'),
PrependedText('first_name', 'blabla'),
AppendedPrependedText('last_name', 'foo', 'bar'),
)
form.is_valid()
form.helper.form_show_errors = True
html = render_crispy_form(form)
self.assertEqual(html.count('error'), 6)
form.helper.form_show_errors = False
html = render_crispy_form(form)
self.assertEqual(html.count('error'), 0)
示例15: create_test
# 需要导入模块: from forms import TestForm [as 别名]
# 或者: from forms.TestForm import is_valid [as 别名]
def create_test(request, project_id):
""" Create a new Test.
"""
project = get_object_or_404(Project, pk=project_id)
form = TestForm(request.POST or None)
if form.is_valid():
new_test = form.save(commit=False) # returns unsaved instance
new_test.project = project
new_test.save() # real save to DB.
messages.add_message(request, messages.SUCCESS, 'The test has been successfully created.')
return redirect(details_project, project_id=project_id)
return render_to_response('form.html',
{'form' : form,
'project' : project},
context_instance=RequestContext(request))