[Django]-How to get the template path in the view django

2👍

Try this. get template location from your project setting file.

from your_project_name.settings import BASE_DIR

path = os.path.join(BASE_DIR+'templates/', 'project.html')
open_html = open(path, 'r')
soup =  BeautifulSoup(open_html, "html5lib")
image = soup.find('img', {'name':'image_url'})
img_url = image.get('src')
open_html.close()
👤ammy

4👍

If you load the template….

mytemplate = loader.get_template('myapp/templatename.html')

What you get in return is a Template object.

One of the members of this object is the origin attribute, which itself has an attribute called ‘name’

templatepath = mytemplate.origin.name

This is the full path to the template file that the template loader discovered.

👤Mars

0👍

It’s best to store your templates within your app.
for example, if your app called polls your template should be at polls/templates/polls/

You can read more about it here:
https://docs.djangoproject.com/en/1.11/intro/tutorial03/

👤Rani

Leave a comment