Search code examples
pythondjangoemailfield

Django: How to check if something is an email without a form


I have HTML form to post in Django View and because of some constraints, it's easier for me to do the validation without the usual Django form classes.

My only reason to use Django Forms is Email Field(s) that are entered.

Is there any function to check if something is an email or I have to use the EmailField to check and validate it?


Solution

  • You can use the following

    from django.core.validators import validate_email
    from django import forms
    
    ...
    if request.method == "POST":
        try:
            validate_email(request.POST.get("email", ""))
        except forms.ValidationError:
            ...
    

    assuming you have a <input type="text" name="email" /> in your form