Django – Series – Post – 16 – Django Validators

Spread the love

 

Django – Series – Post – 16 – ​​ Django Validator :

 In this blog post , we will see how to raise the validation error when an incorrect category is provided to the text input. ​​ Create​​ a file called validator.py which will raise the validation error if the values is not present in the categories.​​ 

 

Validator.py

from django.core.exceptions import ValidationError

categories=['mexican','asian','south indian','continental']

def category_validator(catvalue):
 ​​ ​​ ​​​​ if not catvalue in categories:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ raise ValidationError("Not a valid Category")

 

We will import the validator in models.py ​​ and in the category field , we will include the category_validator function.​​ 

models.py

from django.db import models
from django.utils import timezone
from django.db.models.signals import pre_save,post_save
from .utils import unique_slug_generator
from .validators import category_validator

# Create your models here.

class restaurantLocations(models.Model):
 ​​ ​​ ​​​​ name  ​​ ​​ ​​ ​​ ​​ ​​​​ = models.CharField(max_length=120)
 ​​ ​​ ​​​​ location  ​​ ​​​​ = models.CharField(max_length=120,null=True,blank=True)
 ​​ ​​ ​​​​ category  ​​ ​​​​ = models.CharField(max_length=120,null=True,blank=True,validators=[category_validator])
 ​​ ​​ ​​​​ created  ​​ ​​ ​​​​ = models.DateTimeField(auto_now_add=True,blank=True)
 ​​ ​​ ​​​​ modified  ​​ ​​​​ = models.DateTimeField(auto_now=True)
 ​​ ​​ ​​​​ slug  ​​ ​​ ​​ ​​ ​​ ​​​​ = models.SlugField(null=True,blank=True)

 ​​ ​​ ​​​​ def __str__(self):
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return self.name

 ​​ ​​ ​​​​ @property
 ​​ ​​ ​​​​ def title(self):
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return self.name # obj.title, instance.title

def rl_pre_save(sender, instance, *args, **kwargs):
 ​​ ​​ ​​​​ instance.category = instance.category.capitalize()
 ​​ ​​ ​​​​ print('Saving..')
 ​​ ​​ ​​​​ print(instance.created)
 ​​ ​​ ​​​​ if not instance.slug:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ instance.slug = unique_slug_generator(instance)

#def rl_post_save(sender, instance, *args, **kwargs):
#  ​​ ​​​​ print('Saved')
#  ​​ ​​​​ print(instance.created)
#  ​​ ​​​​ instance.save()

pre_save.connect(rl_pre_save,sender=restaurantLocations)

#post_save.connect(rl_post_save,sender=restaurantLocations)

 

 

Since the changes were made to the models.py, we need to makemigrations.​​ 

 

Under “forms.html” ​​ include the ​​ forms.error ​​ condition​​ 

 

Forms.html

 

{% extends 'base.html' %}
{% block title %}{{title}}{% endblock %}

{% block page %}
<h3>{{title}}</h3><br>
<div​​ class=​​ "col-sm-6">

 ​​ ​​ ​​​​ {% if form.errors %}
 ​​ ​​ ​​​​ <div​​ class="alert alert-danger alert-dismissible fade in"​​ role="alert">
 ​​ ​​ ​​ ​​ ​​​​ <button​​ type="button"​​ class="close"​​ data-dismiss="alert"​​ aria-label="Close">
 ​​ ​​ ​​ ​​ ​​​​ <span​​ aria-hidden="true">&times;</span>
 ​​ ​​ ​​ ​​ ​​​​ </button>
 ​​ ​​ ​​ ​​ ​​​​ <strong>Oh snap!</strong>​​ See the below error message.
 ​​ ​​ ​​ ​​ ​​​​ {{ form.errors }}
 ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​​​ {% endif %}

 ​​ ​​ ​​​​ <form​​ METHOD="POST">​​ {% csrf_token %}
 ​​ ​​ ​​ ​​ ​​​​ <div​​ class="form-group form-inline ">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {% for field in​​ form %}
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <fieldset​​ class="control-group">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <label​​ class="control-label">{{ field.label }}</label>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <div​​ class="controls">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {{ field }}
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ </fieldset>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {% endfor %}
 ​​ ​​ ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​ ​​ ​​​​ <button​​ type="submit"​​ class="btn btn-primary col-sm-2">Save</button>
 ​​ ​​ ​​​​ </form>

</div>
{% endblock %}

 

 

When you give an invalid category , it will pop up an error saying not a valid category.​​ 

 

Thanks for viewing the post, please post your comments below.

Please Like & Follow us on Facebook & Twitter,

https://www.facebook.com/gadoth/

https://www.twitter.com/GadothDA/

The code is available in the below github link,

https://github.com/rajathithan/Django

 

Related posts

Leave a Comment