I have problem with cutomizing the 404 page in django - python

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

Related

Django: Project name is duplicated in the template path

My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
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',
],
},
},
]
And dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error

Django render function takes forever to load

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.

TemplateDoesNotExist Error in Pythonanywhere

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.

Django 2: TemplateDoesNotExist even if files placed properly

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')

After installing jinja2 TemplateDoesNotExist at /admin/

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.

Categories