lolz
lolz

Reputation: 245

Template file path

I'm trying to open and read this html file with in my django project, however i'm getting this [Errno 2] No such file or directory: 'test.html'.

html = 'test.html'
open_html = open(html, 'r')
soup =  BeautifulSoup(open_html, "html5lib")
open_html.close()

But the template path seems to work fine when rendered

template = 'test.html'
context = {
}
return render(request, template, context)

TEMPLATES = [
        'DIRS': [os.path.join(BASE_DIR, "templates")],
]

I know my templates are suppose to go into my apps folders, however i like to keep them in one folder when developing and debugging.

Upvotes: 1

Views: 1239

Answers (1)

NIKHIL RANE
NIKHIL RANE

Reputation: 4092

Because you are trying to access file from templates so you need to add full path

Try following solution

from your_project_name.settings import BASE_DIR
path = os.path.join(BASE_DIR, 'templates', 'test.html')
with open(path, 'r') as open_html:
    soup =  BeautifulSoup(open_html, "html5lib")

Hope this is help you

Upvotes: 3

Related Questions