Change language

{{form.as_p}} — render Django forms as a paragraph

Django Forms — it is an advanced set of HTML forms that you can create with Python and that support all the features of HTML forms in Python. Rendering Django forms in a template can sometimes seem messy, but with the right knowledge of Django forms and field attributes, you can easily create a great form with all the powerful features. In this article, the form is rendered as paragraphs in the template.

{{form.as_p}} — render Django forms as paragraph

Illustration {{form.as_p}} 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.

Let’s create a sample Django form to display and show as an example. In geeks" forms.py enter the following code

from django import forms

 
# create a form

class InputForm (forms.Form):

 

first_name = forms.CharField (max_length = 200 )

last_name = forms.CharField (max_length =  200 )

roll_number = forms.IntegerField (

help_text = "Enter 6 digit roll number"

)

password = forms.CharField (widget = forms.PasswordInput ())

Now we need a View to render this form to a template. Let’s create a view,

from django.shortcuts import render

from . forms import InputForm

 
# Create your views here.

def home_view ( request):

context = {}

context [ ’form’ ] = InputForm ()

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

Finally, we will create a template where we need to place the form. In templates" home.html ,

" form action = " " method = "post"

{% csrf_token%}

{{form.as_p}}

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

"/ form "

Here, {{form.as_p}} will display them in "p" tags. Let’s check if this works accordingly or not. Open http:// localhost: 8000 /


Let’s check the is step code, whether the form is displayed as a paragraph or not. Rendering as a paragraph means that all input fields will be wrapped in "p" tags. 
Here is a demo,

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically