2👍
You should absolutely not be putting your own templates inside any of Django’s directories. Your code should be completely separate.
You should create your own templates
directory inside your project and put your template in there; then your first attempt, os.path.join(BASE_DIR,'templates')
, would work.
0👍
You said that you placed this new template, current_date.html
in the C:\Users\reza\env_mysite\lib\site-packages\django\contrib\admin\templates
directory. That is part of the Django package, and should contain two directories, admin/
, and registration/
. Is it possible that you placed your new template inside of one of those inner folders, like the admin/
folder?
If so, then you should update the call to get_template()
in your view so that it looks like this:
def current_datetime(request):
now = datetime.datetime.now()
t = get_template('admin/current_datetime.html') # note updated path
html = t.render(Context({'current_date':now}))
return HttpResponse(html)
Note that you’re not conforming to best practices here, but that’s a separate issue really. A couple suggestions would be to shift this new template into your own application’s templates/
directory (it’s generally not a good idea to make modifications to the installed packages), and you should also use django.shortcuts.render
, which reduces the amount of code you have to write/manage:
from django.shortcuts import render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, 'admin/current_datetime.html', {'current_date':now})
- [Answered ]-If / else in Django View
- [Answered ]-How to get attribute of a django model's foreign key object using getattr in python?