Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom 404 django template

Tags:

python

django

I'm trying to customize the 404 error pages in my application. After searching many possible solutions, I created an 404.html template, added a method which should handle HTTP Error 404 and edited my urls.py.

But I guess I'm doing something really wrong. My log presents an invalid syntax error and I cannot solve it.

My views.py:

# HTTP Error 400
def page_not_found(request):
    response = render_to_response('404.html',context_instance=RequestContext(request))
    response.status_code = 404

    return response

And the syntax error:

Traceback (most recent call last):
File "/.../myenv/lib/python3.4/site-packages/django/core/urlresolvers.py", line 393, in urlconf_module
return self._urlconf_module
AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'
...
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/dcaled/work/portal-interface/portal/urls.py", line 12
handler404 = 'views.handler404'
              ^
SyntaxError: invalid syntax

Does anyone have an idea of what's going on? Thanks.

UPDATE: After @Alasdair suggestion, I made some changes and fixes. The error has stopped.

Now, my urls.py is like:

handler404 = 'views.page_not_found'

urlpatterns = [
    url(r'^$', views.home),]    

But I still don't get my custom 404.html when accessing a non existing page. Instead, a default page is loaded, with this message:

"Not Found

The requested URL /url/404 not found on this server."

Also, my settings.py:

DEBUG = False
ALLOWED_HOSTS = ['*']
TEMPLATE_DIRS = (os.path.join(BASE_DIR , 'portal/templates'),)

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 project tree:

├── fb_dev
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
│
└── portal
    ├── admin.py
    ├── forms.py
    ├── json
    ├── migrations
    ├── models.py
    ├── static
    ├── templates
    │   ├── 404.html
    │   └── portal
    │       ├── base.html
    │       ├── home.html
    ├── templatetags
    ├── urls.py
    └── views.py
like image 732
revy Avatar asked Sep 19 '25 16:09

revy


2 Answers

After some months, I revisited the problem and identified a solution. Here is the answer:

a) I removed page_not_found method from the views.py. There's no need to add it. Django automatically gets and renders the 404.html template.

b) Once again, there's no need to edit urls.py. So I removed this line:

handler404 = 'views.page_not_found'

c) Also, it is necessary to edit settings.py, setting DEBUG to FALSE.

DEBUG = False

And I removed the following line:

TEMPLATE_DIRS = (os.path.join(BASE_DIR , 'portal/templates'),)

d) Finally, I created a 404.html template in my portal/templates directory. It is worth noticing that the custom template was rendered only when placed inside this directory.

like image 79
revy Avatar answered Sep 21 '25 06:09

revy


After some troubles, the revy solution worked perfectly. So, just to clarify:

The Two things to do are:

  • Add a 404.html file in your_app/template/404.html
  • Set your DEBUG value to False

After that, Django will render automatically your template.

like image 21
Slot Avatar answered Sep 21 '25 06:09

Slot