ratrace123
ratrace123

Reputation: 1076

putting a class view in a template django

django newb. I am just trying to include my class view in a template. The class view that I have is a CreateView, so i want to generate a form which i can submit. I see all sorts of info on the web for generating lists of objects etc, but nothing for a form. I am thinking this should not be that hard. I have the following:

models.py

class Comments(models.Model):
    comment_text = models.CharField(max_length=600, default="None")

views.py

class CommentCreate(CreateView):
    model = Comments
    fields = ['comment_text']
    template_name = 'commentcreate.html'
    context_object_name = 'comment_create'

my template

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">{% csrf_token %}
    {{ comment_create }}
    <input type="submit" value="Create" />
</form>
</body>
</html>

The template is mock, i really want to add this form to a completely different template which is part of another app. If someone could point me in the right direction it would be much appreciated.

Upvotes: 0

Views: 346

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Just use {{ form }} to access all the fields defined inside your view class.

To access a specific field from the form, use dot operator, ie, {{ form.db_field_name }}

For ex,

{{ form.comment_text }}

This would create a label, input tags for the field comment_text.

You may also render the form as table or paragraph etc.

Upvotes: 1

Related Questions