I know how to render the form of serializers as a whole but I do not know how to render it individually similar to django forms on a template like
{{ form.name }}
{{ form.name.errors }}
My current files and codes are the following:
models.py
# Create your models here.
from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
class Meta:
ordering = ('created',)
serializers.py
from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
form.html
{% extends 'main/base.html' %}
{% load rest_framework %}
{% block content %}
<div class="container">
<form method="POST" id="id-login-form" class="panel-body">
{% csrf_token %}
{% render_form serializer %}
<!-- {% render_form serializer template_pack='rest_framework/horizontal' %}
{% render_form serializer template_pack='rest_framework/vertical' %} -->
<!-- {{ serializer }} -->
<input type="submit" value="Save">
</form>
</div>
{% endblock %}