Тест рендеринга как шаблон: {% stati c%} не работает - PullRequest
0 голосов
/ 09 мая 2020

Django == 3.0.6

Я пытаюсь разрешить пользователю с правами администратора использовать теги шаблонов. Я имею в виду, вот так:

enter image description here

Фильтр получает идентификатор и отображает огромный тег изображения.

views.py

class PostDetailView(DetailView):

    model = Post

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        post_body = "{% autoescape off %}\n{% load image %}\n{% load static %}\n" + self.object.body + "{% endautoescape %}"
        return HttpResponse(Template(post_body).render(Context())) # Breakpoint

В точке останова (показанной в приведенном выше коде) post_body равно:

{% autoescape off %}
{% load image %}
{% load static %}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>

{{ 1|img }}

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>{% endautoescape %}

Результат в браузере:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>

<picture>
    <source srcset="{% static image/1_320_1x.png %} 1x, {% static image/1_320_2x.png %} 2x, {% static image/1_320_3x.png %} 3x" media="(max-width: 319.98px)" type="image/png">
    <source srcset="{% static image/1_320_1x.webp %} 1x, {% static image/1_320_2x.webp %} 2x, {% static image/1_320_3x.webp %} 3x" media="(max-width: 319.98px)" type="image/webp">
    <source srcset="{% static image/1_400_1x.png %} 1x, {% static image/1_400_2x.png %} 2x, {% static image/1_400_3x.png %} 3x" media="(min-width: 320px) and (max-width: 575.98px)" type="image/png">
    <source srcset="{% static image/1_400_1x.webp %} 1x, {% static image/1_400_2x.webp %} 2x, {% static image/1_400_3x.webp %} 3x" media="(min-width: 320px) and (max-width: 575.98px)" type="image/webp">
    <source srcset="{% static image/1_540_1x.png %} 1x, {% static image/1_540_2x.png %} 2x, {% static image/1_540_3x.png %} 3x" media="(min-width: 576px) and (max-width: 767.98px)" type="image/png">
    <source srcset="{% static image/1_540_1x.webp %} 1x, {% static image/1_540_2x.webp %} 2x, {% static image/1_540_3x.webp %} 3x" media="(min-width: 576px) and (max-width: 767.98px)" type="image/webp">
    <source srcset="{% static image/1_720_1x.png %} 1x, {% static image/1_720_2x.png %} 2x, {% static image/1_720_3x.png %} 3x" media="(min-width: 768px) and (max-width: 991.98px)" type="image/png">
    <source srcset="{% static image/1_720_1x.webp %} 1x, {% static image/1_720_2x.webp %} 2x, {% static image/1_720_3x.webp %} 3x" media="(min-width: 768px) and (max-width: 991.98px)" type="image/webp">
    <source srcset="{% static image/1_960_1x.png %} 1x, {% static image/1_960_2x.png %} 2x, {% static image/1_960_3x.png %} 3x" media="(min-width: 992px) and (max-width: 1199.98px)" type="image/png">
    <source srcset="{% static image/1_960_1x.webp %} 1x, {% static image/1_960_2x.webp %} 2x, {% static image/1_960_3x.webp %} 3x" media="(min-width: 992px) and (max-width: 1199.98px)" type="image/webp">
    <source srcset="{% static image/1_1140_1x.png %} 1x, {% static image/1_1140_2x.png %} 2x, {% static image/1_1140_3x.png %} 3x" media="(min-width: 1200px)" type="image/png">
    <source srcset="{% static image/1_1140_1x.webp %} 1x, {% static image/1_1140_2x.webp %} 2x, {% static image/1_1140_3x.webp %} 3x" media="(min-width: 1200px)" type="image/webp">
    <img src="{% static %}1_720_1x.png" alt="asdfasdf">
</picture>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>

В браузер сгенерировал html:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>

<picture>
    <source srcset="{% static image/1_320_1x.png %} 1x, {% static image/1_320_2x.png %} 2x, {% static image/1_320_3x.png %} 3x" media="(max-width: 319.98px)" type="image/png">
    <source srcset="{% static image/1_320_1x.webp %} 1x, {% static image/1_320_2x.webp %} 2x, {% static image/1_320_3x.webp %} 3x" media="(max-width: 319.98px)" type="image/webp">
    <source srcset="{% static image/1_400_1x.png %} 1x, {% static image/1_400_2x.png %} 2x, {% static image/1_400_3x.png %} 3x" media="(min-width: 320px) and (max-width: 575.98px)" type="image/png">
    <source srcset="{% static image/1_400_1x.webp %} 1x, {% static image/1_400_2x.webp %} 2x, {% static image/1_400_3x.webp %} 3x" media="(min-width: 320px) and (max-width: 575.98px)" type="image/webp">
    <source srcset="{% static image/1_540_1x.png %} 1x, {% static image/1_540_2x.png %} 2x, {% static image/1_540_3x.png %} 3x" media="(min-width: 576px) and (max-width: 767.98px)" type="image/png">
    <source srcset="{% static image/1_540_1x.webp %} 1x, {% static image/1_540_2x.webp %} 2x, {% static image/1_540_3x.webp %} 3x" media="(min-width: 576px) and (max-width: 767.98px)" type="image/webp">
    <source srcset="{% static image/1_720_1x.png %} 1x, {% static image/1_720_2x.png %} 2x, {% static image/1_720_3x.png %} 3x" media="(min-width: 768px) and (max-width: 991.98px)" type="image/png">
    <source srcset="{% static image/1_720_1x.webp %} 1x, {% static image/1_720_2x.webp %} 2x, {% static image/1_720_3x.webp %} 3x" media="(min-width: 768px) and (max-width: 991.98px)" type="image/webp">
    <source srcset="{% static image/1_960_1x.png %} 1x, {% static image/1_960_2x.png %} 2x, {% static image/1_960_3x.png %} 3x" media="(min-width: 992px) and (max-width: 1199.98px)" type="image/png">
    <source srcset="{% static image/1_960_1x.webp %} 1x, {% static image/1_960_2x.webp %} 2x, {% static image/1_960_3x.webp %} 3x" media="(min-width: 992px) and (max-width: 1199.98px)" type="image/webp">
    <source srcset="{% static image/1_1140_1x.png %} 1x, {% static image/1_1140_2x.png %} 2x, {% static image/1_1140_3x.png %} 3x" media="(min-width: 1200px)" type="image/png">
    <source srcset="{% static image/1_1140_1x.webp %} 1x, {% static image/1_1140_2x.webp %} 2x, {% static image/1_1140_3x.webp %} 3x" media="(min-width: 1200px)" type="image/webp">
    <img src="{% static %}1_720_1x.png" alt="asdfasdf">
</picture>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>

Проблема: {% stati c%} не работал. Как видим, автоэскейп сработал, изображение загрузки сработало. Но {% stati c%} не работает. Не могли бы вы мне помочь?

1 Ответ

0 голосов
/ 09 мая 2020

попробуйте использовать носитель вместо stati c для изображений

https://docs.djangoproject.com/en/3.0/topics/files/

...