1

I need to construct the HTML body from a Django view and I cannot find a solution to refer correctly a JPG file ( must say that the HTML is much larger then this, but with other stuff seems that is working for me ):

I've tried this:

from django.template import Template
...
html = Template('<IMG SRC="{% static "base/images/course/website-46-2.jpg" %}">')
return HttpResponse( html )

And I get this error:

Invalid block tag on line 1: 'static'. Did you forget to register or load this tag?

In Django template I resolve this by loading the static files:

{% load static %}

How can I do this in Python ( Django View ) ? Or any other suggestion is much appreciated.

I've tried different solution that I have found on this site and others but none seems to work for me.

Django Version: 2.2.1

2
  • {% extends "name" %} ? Commented Aug 20, 2019 at 16:23
  • Don't understand this. Commented Aug 20, 2019 at 16:44

3 Answers 3

1

You can create an engine with the static library as a built-in. This makes it available to the template without calling {% load static %} first.

from django.template import Template, Context, Engine

engine = Engine(builtins=['django.templatetags.static'])
template = engine.from_string('<IMG SRC="{% static "base/images/course/website-46-2.jpg" %}">')
return HttpResponse(template.render(Context()))
Sign up to request clarification or add additional context in comments.

8 Comments

With this, I get this error: AttributeError at /course/1/ 'SafeText' object has no attribute 'get'
That's probably something like this (invalid view): stackoverflow.com/questions/43987462/…
Not likely because I have a view like this: course( request, question_id ) and it's working without the suggested code.
Which version of Django are you using and which line is causing the exception? This works for me on Django 1.11.
Ah, I think I know the issue. I didn't realize your function was a view (returning a response), I assumed it was a utility function. I've updated my answer to return a response object instead of plain text. See if that works.
|
0

Have you set your STATIC_URL in settings.py? You can do this by the following:

STATIC_URL = '/static/'

Your image would then be found under 'your_app/static/base/images/course/website-46-2.jpg'.

Does the folder structure follow this convention? If not you can set the STATIC_URL to '/base/'

4 Comments

Yes I have that setup, STATIC_URL = '/static/'
Actually my app is called "base" and my effective path to the image is: "project_name\base\static\base\images\course\website-46-2.jpg"
Have you tried html = Template('{% load static %}<IMG SRC="{% static "base/images/course/website-46-2.jpg" %}">')
Doesn't work either, gives and odd result on the page: '>
0
def startchat(request):
   template=loader.get_template('app1/chatbot.html')
   return HttpResponse(template.render())

This function loads the html page into Django. Before that , we need to import the module

from django.template import loader

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.