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:
On Friday, 28 February 2020 02:41:04 UTC+5:30, Sunil BK wrote:
Hello communityI 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:
but is displayed in the log.Can somebody give me a hint what I am missing here?Thanks a lot!!
SunilThis 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_idthe 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 classclass 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/e3874615-ee6d-43af-b2cb-3405038817c0%40googlegroups.com.
No comments:
Post a Comment