Change language

RegexField — Django Forms

| |

RegexField has the following optional arguments:

  • regex: — A regex specified as a string or compiled regex object expressions.
  • max_length and min_length : — if provided, these arguments ensure that the string is not greater than or equal to the given length.
  • strip: — if True (default), the value will be stripped from leading and trailing spaces.


 field_name = forms.RegexField ( ** options ) 

Django form RegexField Explanation

RegexField illustration using example. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Enter the following code into the file of the geeks application.

from django import forms

# create a form

class GeeksForm (forms.Form):

geeks_field = forms.RegexField (regex = "G. * s" )

Add the geek app to INSTALLED_APPS

# Application definition



’django.contrib.admin’ ,

’ django.contrib.auth’ ,

’django.contrib.contenttypes’ ,

  ’django.contrib.sessions’ ,

  ’django.contrib.messages’ ,

  ’django.contrib.staticfiles’ ,

  ’geeks’ ,


Now, to convert this form to a view, we need a view and The URL associated with this URL. Let’s first create a view in the of the geeks app,

from django .shortcuts import render

from . forms import GeeksForm

# Create your views here.

def home_view (request):

context = {}

  context [ ’form’ ] = GeeksForm ()

return render (request, "home.html" , context)

Here we import this particular form from and instantiate it in the view so that it could be displayed in the template. 
Now, to initiate your Django form, you need to create a home.html where you can create things as you see fit. Let’s create a form in home.html .

" form method = "POST"

{% csrf_token%}


" input type = "submit" value = "Submit"

"/ form

Finally, the URL to map to this view in

from django.urls import path

# import views from

from . views import home_view


urlpatterns = [

  path (’’, home_view),


Let’s start the server and check what’s on actually happened, Run

 Python runserver 

So the geeks_field RegexField is created by replacing "_" with ".

How to use RegexField?

RegexField is used to inject small selected text into the database. You can enter a specific entity name, email address, etc. So far, we’ve discussed how to implement a RegexField, but how to use it in a view to execute the boolean part. To do some logic, we need to get the value entered in the field into a Python string instance. 

from django.shortcuts import render

from .forms import GeeksForm

# Create your views here.

def home_view (request):

  context = {}

  form = GeeksForm ()

context [ ’form’ ] = form

if request.POST:

temp = request.POST [ ’geeks_field’ ]

print (temp)

return render (request, "home.html " , context)

Now let’s try to enter something else in the field.

Since it needs to be checked in soo Matching the supplied regular expression, the input must start with "G" and end with "s". Let’s enter "GeeksForGeeks" in the field

This data can now be retrieved using the appropriate query dictionary. If the method is GET, the data will be available in request.GET, and if post, then request.POST , respectively. In the above example, we have a value in temp that we can use for any purpose.

Basic Field Arguments

Basic Field Arguments — they are arguments given to each field to apply some kind of constraint or to convey a particular characteristic to a particular field. For example, adding the required = False argument to the RegexField will allow the user to leave it blank. Each Field constructor takes at least these arguments. Some Field classes accept additional field-specific arguments, but you should always accept the following:

Field Options Description
required By default, each Field class assumes the value is required, so to make it not required you need to set required=False
label The label argument lets you specify the “ human-friendly ”label for this field. This is used when the Field is displayed in a Form.
label_suffix The label_suffix argument lets you override the form’s label_suffix on a per-field basis.
widget The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.
help_text The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods.
error_messages The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
validators The validators argument lets you provide a list of validation functions for this field.
localize The localize argument enables the localization of form data input, as well as the rendered output.
disabled The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method