简体   繁体   中英

django form.as_p doesnt render the form

So here's my form :

class SecretNoteForm(forms.Form):
    note = forms.Textarea()

Here's my view :

def index(request):
    if request.method == 'POST':
        pass
    else:
        form = SecretNoteForm()

    return render(request, "notes/index.html", {"form" : form})

and my html :

{% block content %}
<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create">
</form>
{% endblock %}

When I go to the url it only shows me Create button but no form fields. What am I doing wrong here ? I can't seem to figure that out..

The correct way to make a TextArea is with a widget:

note = forms.CharField(widget=forms.Textarea)

Here is more documentation: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

and include attributes like that:

note = forms.CharField(widget=forms.Textarea(attrs={'rows': 16,'cols': 25}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM