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

24 views
Skip to first unread message

Sunil BK

unread,
Feb 27, 2020, 4:11:04 PM2/27/20
to Django users
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")




rossm6

unread,
Feb 27, 2020, 4:57:34 PM2/27/20
to django...@googlegroups.com
If the form is not valid you are creating a new form and returning this to the user.  So you need to remove the line where you create this second form object within the else clause of the view and it will work.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/28c122d0-72b9-4367-9f07-ce1939ad3b78%40googlegroups.com.

Naveen Arora

unread,
Feb 28, 2020, 7:26:20 AM2/28/20
to Django users
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.

Sunil BK

unread,
Mar 1, 2020, 10:43:31 AM3/1/20
to Django users
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
Reply all
Reply to author
Forward
0 new messages