These are chat archives for django/django

Nov 2018
Nov 13 2018 00:53
Hi, I'm trying to inject some HTML into the response content through a middleware, is this the correct way of doing it? I'm basically trying to achieve what django-debug-toolbar does, but when I return the rendered string from render_to_string function, I get a page with alot of \n\n\n\n and \t\t\t, it seems that whitespaces are not escaped/removed properly?
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
from lxml import html
from . import models

_HTML_TYPES = ("text/html", "application/xhtml+xml")

class Middleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):

        # request, before response/view

        should_render = True

        # don't render if ajax request
        should_render = not request.is_ajax()

        response = self.get_response(request)

        # request, after response/view

        # Check for responses where we can't insert html.
        content_encoding = response.get("Content-Encoding", "")
        content_type = response.get("Content-Type", "").split(";")[0]
        if any(
                getattr(response, "streaming", False),
                "gzip" in content_encoding,
                content_type not in _HTML_TYPES,
            should_render = False

        if should_render:
            response.content = self.render_content(response)

            if response.get("Content-Length", None):
                    response["Content-Length"] = len(response.content)

        return response

    def render_content(self, response):
        Injects header html into response content
        content = force_text(response.content, encoding=response.charset)
        if content:
            ctx = {}
            doc_tree = html.fromstring(content)
            # extract
            for ctx_var, find_element, start_tag, close_tag in (("head_content", './/head', '<head>', '</head>'),
                                                                ("body_content", './/body', '<body>', '</body>')):
                tree_el = doc_tree.find(find_element)
                if tree_el:
                    ctx[ctx_var] = html.tostring(tree_el)

            content = render_to_string("app/base.html", ctx)

        return content
Ekam Walia
Nov 13 2018 09:09
Hey guys, I am working in building a webapp with django and react
I am confused if i really need to use django res framework. Is DRF really needed, cause i tried it out an i can easily use the json module to get and return jsons. What is the use of DRF then? Is it important and is it recommended?
Nov 13 2018 11:11
@kelsier27 I guess it depends on your architcture. DRF was a kind of standart for "frontend frameworks" login AFAIK. But you could also use JWT to query a token.
Ekam Walia
Nov 13 2018 15:10
@LukeVideo I don't think it really answers my question. If i am making a rest api with Django, does DRF provide any advantages and is using DRF standard practice?
Nov 13 2018 15:18
@kelsier27 I'm no expert but DRF is the de facto rest implementation for Django. Serialing is dead easy and connection token are build in. So yeah it does help a lot. So much that it is also quite common to use just django-rest-framework to handle authentication when using a third party frontend library or using Graphql.