I have a function definition in the views.py file of my Django app as follows:
def dashboard_page(request, date_range=None):
''' This is the function '''
template = 'accounts/dashboard.html'
dashboard_data_url = reverse('dashboard_data')
return render(request, template, {'dashboard_data_url': dashboard_data_url})
The Templates configuration on the settings.py file is as follows:
DEBUG = False
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates/'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages'
],
},
},
]
I tried to debug the issue using the pdb and got the following error as shown in the screenshot. I tried to add print statements before the render function, that works fine. But the render function takes forever and returns no response.
Related
This is my settings.py:
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
main urls.py:
handler404 = 'home.views.custom_page_not_found_view'
my views for 404 page:
def custom_page_not_found_view(request, exception):
return render(request, "404.html", {}, status=404)
i tryed this solution:(Correct way of implementing custom 404 page in Django 3 in production)
but it doesn't work and kept getting server 500 error responses
how do i fix this?
This error was because I had used the extends tag inside the 404.html file
def custom_proc(request):
"A context processor that provides 'app', 'user' and 'ip_address'."
return {
'app': 'My app',
'user': request.user,
'ip_address': request.META['REMOTE_ADDR']
}
above code is my code. a request Context. i write this code in a file context_processors.py.
and follow code is in settings.py file:
'context_processors': [
'django.template.context_processors.custom_proc',
how to use the context processor in the view.py file?
return render(request,'template1.html',
{'message': 'I am view 1.'})```
enter code here
The path 'django.template.context_processors.custom_proc' to your context processor is not correct.
If you have created the file context_processors.py in an app directory called my_app, then your settings.py should have something like:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'my_app.context_processors.custom_proc',
],
},
},
]
Erorr
I wanna know where django search for index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
views.py
def index(request):
return render(request, 'index.html', {})
Django will look in the templates directory, that's if you created a folder called templates and if you have a file named index.html in the templates folder. For some reason I feel like the reason why you are asking this question is because you can not get your template to render when you run the server, am I correct? Please get back to me and I can do my best to fill you in so you can get your project working.
You can easily solve this problem: if the name of your project directory, where manage.py is in, is "RalphPortfolio", you need to make the following correction in settings.py:
'DIRS': ['RalphPortfolio/templates'],
add the full path to the template dirs
for example if your path is /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
it's because the path is not clear well and it will not find it.
I have encountered interesting problem on Django 2.
In my settings.py I have written this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
After that I have created folder templates in my projects root folder.
Then I have created app with name front and added it into my settings.py INSTALLED_APPS:
Inside my views.py I have added this view:
def index(request):
return render(request, 'front/index')
I have assigned its url like this:
url(r'^$', views.index, name='index'),
When I'm trying to access to this view I get this error:
TemplateDoesNotExist at /
front/index
Why this happening? Did I miss something?
Change
def index(request):
return render(request, 'front/index')
to
def index(request):
return render(request, 'front/index.html')
I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand).
Changing 'APP_DIRS' doesn`t help
templates look like that:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': False,
'DIRS': ['main/templates', 'shop/templates'],
'OPTIONS': {
'environment': 'django_test.create_jinjia_env.environment',
'autoescape': True,
'auto_reload': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
If don`t include templates into DIRS it throws the same error
Didn`t find the questions like that. Thanks in advance!
The Django admin app does not come with Jinja templates. If you wish to use Jinja and the admin app, you need to include both engines in your TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True, # This allows Django to find the templates in the admin app
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
# The rest of your Jinja2 settings.
},
Secondly, when APP_DIRS is True, the Jinja2 backend looks for templates in a jinja2 subdirectory. That means you should put your templates in main/jinja2 and shop/jinja2 instead of main/templates and shop/templates.