• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

[Django] AttributeError: ‘module’ object has no attribute ‘TextField’

Aniruddha Chaudhari/7851/2
Django

We can define the TextField in the Django model. But, there is no TextField type in the Django form.

This Django implementation is really strange for many of the developers. I’m not sure about it. But, there is a way to sort it out.

Let’s start with creating Django model and form for text data.

How to Create TextField in Django Model and Form?

You can create the TextField in the Django model by adding the following line of code in models.py.

For example:

note = models.TextField(default='', blank=True)

Here “note” is the model variable name which has type TextField.

Now, you want to use this field for creating a form so that the user can insert text data through the Django form.

Let’s try creating TextField in Django form by updating forms.py.

note = forms.TextField(required=False)

When you run your Django application, it will throw an error as

AttributeError: 'module' object has no attribute 'TextField'

This is because, there is no attribute as the TextField associated with the module forms.

Use the CharField in the Django form instead of TextField.

note = forms.CharField(required=False, widget=forms.Textarea)

We are also setting a widget attribute for the Django form. It can replicate the behavior of TextField in the Django model.

Django is very beautiful for creating websites and it is widely adopted in the software industry for the reason.

In this tutorial, we have learned to create the Django form with the TextField data by solving AttributeError exception. If you have any doubt, let me know in the comment. Keep learning.

Python Interview Questions eBook

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Jagannath Bhanja
    July 7, 2021 at 1:09 pm

    can you please give a demo on this? I mean total code.

    • Reply
      Aniruddha Chaudhari
      July 8, 2021 at 5:11 pm

      Sure. I will plan it. We already have a YouTube channel where I share Python programming tutorials.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard