Search code examples
djangoformswidgettextarea

Django forms, attrs cols and rows for textarea doesn't work, why?


I'm working on a project with Django and a textarea. The textarea by default renders cols="40" and rows="10", which is not great for my page. I'm trying to use Django's widgets to change those attributes to 20 and 5 respectively. This is my code:

class NewContent(forms.Form):
    content = forms.CharField(widget=forms.Textarea(attrs={"cols":20, "rows":5}))

Unfortunately, the code does not change the looks of the form at all when the page gets rendered. Meaning, it displays cols 40 and rows 10. But wait, things get really bizarre... when checking on the developer tools, on Google, I can see that the HTML code has changed to what I want! crazy!!

I also tried the following code that I found in a different chat:

attrs={"style": "height:60px; width:500px"}

Which "changes" the size of the box but... for different reasons I'm not in love with this solution.

Does anybody have an idea of what is happening here?

I have a windows 10 and use VEC. Cheers!


Solution

  • you might have css style which defines width and height of your textarea e.g.:

    <html>
    <head>
        <style>
            textarea {width: 100px; height:30px;}
        </style>
    </head>
    <body>
        <form>
            <textarea cols="30" rows="5"></textarea><br/>
            <textarea cols="40" rows="7"></textarea>
        </form>
    </body>
    </html>