Sunday, March 1, 2020

Re: How to do validation on a form which is opened by clicking a button

Hey Guys

Thanks for your reply!!
I finally managed to get the code running. 

The reason for no showing up the validation messages was, that I my action within the form action method was set to POST. When I have switched that to GET, everything worked as expected.

Thanks
Sunil


Am Freitag, 28. Februar 2020 13:26:20 UTC+1 schrieb Naveen Arora:
Use this as below  in your view - 

context["form"] = YourForm(request.POST or None)

and just a method if form.is_valid(), there is no need for the else part.

On Friday, 28 February 2020 02:41:04 UTC+5:30, Sunil BK wrote:
Hello community

I am trying to build a simple form which can be opened using a button. It should offer some fields which should be further process after validation. And there is exactly my issue. When I write some data into the fields and click the submit button. the validation error is NOT displayed below the screen like in this screeshot:

2020-02-27_21-52-30.png


but is displayed in the log.

Can somebody give me a hint what I am missing here?
Thanks a lot!!
Sunil


This is the code I am using:

my HTML Code for presenting the form.
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{%block content%}
<br>
<h1 style="color:black">Add System to Organization</h1>
<br>
<div class="col" style="color:black">
<form method="post" action="{% url 'chr_add_system' %}" class="uniForm">
{% csrf_token %}
{{form|crispy}}
<input type="submit" value="Submit"/>
</form>
</div>
<br>
{%endblock content%}

the view:
def chr_add_system_view(request):
if request.method == 'POST':
form = AddChrSystemForm(request.POST)
if form.is_valid():
print('add_system-------->valid')
# TODO: do something
return redirect(chr_systems_view)
else:
print('form.errors ' + str(form.errors))
form = AddChrSystemForm()
context = {'form': form}
return render(request=request, template_name='chr/add_system.html', context=context)

the form:
class AddSystemForm(ModelForm):

def __init__(self, *args, **kwargs):
super(AddSystemForm, self).__init__(*args, **kwargs)
self.fields['profile_name'].widget.attrs['readonly'] = True

class Meta:
model = System
fields = '__all__'

def clean_system_id(self):
print('validating oid of system..')

system_id = self.cleaned_data['system_id']
print('system_id: ' + system_id)

if "2.16" not in system_id:
print('oid of system id is invalid')

raise forms.ValidationError(
"Your submissionset source oid seems to be invalid. ==> it has to be a valid oid and must start with: 2.16")
print('oid of system id is valid')
return system_id

the button where the form is called:
<div class="col system_title">
<font style="color:Black;"><b>SourceId</b></font>
<form method="post" action="{% url 'add_system' %}" class="uniForm">
{% csrf_token %}
<button type='submit' name="add_system"
class="btn btn-warning btn-sm float-right" value="{{item0}}">[WIP] Add System
</button>
</form>
</div>

my class
class System(models.Model):
system_id = models.CharField(max_length=100)
abbreviation = models.CharField(max_length=100, default="SRC-")
software_name = models.CharField(max_length=100, default="Portal")
vendor = models.CharField(max_length=100, default="vendor")
profile_name = models.CharField(max_length=100, default="Source")




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e5b55023-9b80-4875-99ea-0795d1a43fba%40googlegroups.com.

No comments:

Post a Comment