Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework serializers render form individually

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 %}
like image 835
Dean Christian Armada Avatar asked Dec 15 '22 09:12

Dean Christian Armada


1 Answers

DRF provides another template tag, in addition to {% render_form %} called {% render_field %} that's actually used within the former to render all the fields.

So your views.py would look something like this (you need to send this style dict in your response):

class SnippetCreate(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'snippets/snippet_create.html'
    style = {'template_pack': 'rest_framework/vertical/'}

    def get(self, request):
        serializer = SnippetSerializer()
        return Response({'serializer': serializer, 'style': self.style})

    def post(self, request):
        serializer = SnippetSerializer(data=request.data)
        return Response({'serializer': serializer, 'style': self.style})

And in your template you could then just do:

{% render_field serializer.title style=style %}

{% render_field serializer.code style=style %}

Best way to understand what's going on is actually to look at the source itself: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/templatetags/rest_framework.py#L35

which then calls

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/renderers.py#L325

like image 176
getup8 Avatar answered Jan 21 '23 15:01

getup8