Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Styling Django Forms with django-crispy-forms
Next article icon

How to customize Django forms using Django Widget Tweaks ?

Last Updated : 16 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Django forms are a great feature to create usable forms with just few lines of code. But Django doesn’t easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id to individual fields in forms.


Let’s say, we are having a simple Django form that will have four fields:

  • First Name
  • Last Name
  • Username (available default)
  • Password (available default)
  • Confirm Password (available default)

We are not going to discuss how we create this form but rather than we are going to see how to customize the frontend of Django forms. To create a form in Django you can check out – How to create a form using Django Forms ?

The initial Django forms (available default looks like)

The default Django form

which will have a simple code like

HTML

{{form}}
 <input type="submit" value="submit">
                      
                       

Of course, there are many default methods that Django provide us by  default like using form.as_p which looks like

Django form using as_p 

that will have a simple code like

HTML

{{form.as_p}}
<input type="submit" value="submit">
                      
                       

But we need to add custom classes and CSS to forms. 

How to customize Django forms using Django Widget Tweaks ?

So, Let’s see how to make it look good like this

Django form input form customized

So we are going to implement it using Django Widget Tweaks. Let’s install django-widget-tweaks using pip

pip install django-widget-tweaks

Now go to the INSTALLED_APPS in settings.py and add widget_tweaks in INSTALLED_APPS

INSTALLED_APPS = [    'django.contrib.auth',  #...........(some more apps already here maybe)......     'widget_tweaks',  #...........(some more apps already here maybe)...... ]

So Now we have Django widget tweaks installed let’s import it in the HTML file in which we are working write this {% load widget_tweaks %} in the top of your HTML page and we have to simply change Every field:

HTML

{% load widget_tweaks %}
 
<div class="form-group">
  // first_name is the name by which first name is created in django forms
  {% render_field form.first_name class="form-control" placeholder="First Name" type="text" %}
</div>
 
<div class="form-group">
   // last_name is the name by which last name is created in django forms
  {% render_field form.last_name type="text" class="form-control" placeholder="Last Name" %}
</div>
 
<div class="form-group">
   // username is the default name of username in django forms
  {% render_field form.username type="text" class="form-control" id="exampleInputUsername" placeholder="Username" %}
</div>
 
<div class="form-group">
   // password1 is the default name of password in django forms
  {% render_field form.password1 type="password" class="form-control" id="exampleInputPassword" placeholder="Password" %}
</div>
 
<div class="form-group">
   // password2 is the default name of confirm password in django forms
  {% render_field form.password2 type="password" class="form-control" id="exampleInputPassword1" placeholder="Confirm Password" %}
</div>
 
<div>
  <button type="submit" value="Submit" > Register </button>
</div>
                      
                       

Each field’s name has been explained in “// ” in code. In the CSS file, we will have all our code for customizing each field by class or id.

So let’s see how Django tweaks help us See in each of the fields, we have our class because of which we can add any CSS by our choice. So, we have to just design the HTML with css, and use it in Django by just replacing the input tag with render_field and form.field_name with {% %} at the end and design it with our choice.



Next Article
Styling Django Forms with django-crispy-forms
author
210manishprajapati
Improve
Article Tags :
  • Python
  • Python Django
Practice Tags :
  • python

Similar Reads

  • Styling Django Forms with django-crispy-forms
    Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Mod
    1 min read
  • How to create a form using Django Forms ?
    Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model
    3 min read
  • Django form field custom widgets
    A widget is Django’s representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
    3 min read
  • Django - How to add multiple submit button in single form?
    Prerequisites: PythonPipDjango When we submit the data using the HTML form, it sends the data to the server at a particular URL which is defined in the action attribute. To perform different actions with a single HTML form, we just need to add multiple submit buttons in the form. For example, If you
    5 min read
  • How to Integrate Custom Rich Text-Editor in Your Django Website?
    In this article, we will implement tinymce text editor in our Django application. What is django-tinymce? django-tinymce is a Django application that contains a widget to render a form field as a TinyMCE editor. Features :- Use as a form widget or with a view.Enhanced support for content languages.I
    3 min read
  • How to change border color in Tkinter widget?
    Prerequisites: Tkinter GUI, Tkinter Widgets Tkinter is Python’s standard GUI package which provides us with a variety of common GUI elements such as buttons, menus, and various kinds of entry fields and display areas which we can use to build out an interface. These elements are called Tkinter Widge
    3 min read
  • How to Add HTML Attributes to Input Fields in Django Forms?
    Django provides an easy-to-use system for creating and managing forms through its forms.Form and forms.ModelForm classes. These classes allow us to create form fields that map directly to HTML input fields, such as <input>, <textarea>, <select>, etc. By default, these fields come w
    4 min read
  • Handle Multiple Forms on a Single Page in Django
    One common scenario in web development is handling multiple forms on a single page. This might be required when a user needs to fill out various types of information without navigating away from the page. In this article, we will explore the best practices for managing multiple forms on a single pag
    6 min read
  • {{ form.as_ul }} - Render Django Forms as list
    Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
    2 min read
  • Initial form data - Django Forms
    After creating a Django Form, if one requires some or all fields of the form be filled with some initial data, one can use functionality of Django forms to do so. It is not the same as a placeholder, but this data will be passed into the view when submitted. There are multiple methods to do this, mo
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences