Custom 404 django template
<p>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.</p>

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

<p>My views.py:</p>

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

    return response
</code></pre>

<p>And the syntax error:</p>

<pre><code>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 "&lt;frozen importlib._bootstrap&gt;", line 321, in _call_with_frames_removed
File "/home/dcaled/work/portal-interface/portal/urls.py", line 12
handler404 = 'views.handler404'
              ^
SyntaxError: invalid syntax
</code></pre>

<p>Does anyone have an idea of what's going on?
Thanks.</p>

<p><strong>UPDATE:</strong> 
After @Alasdair suggestion, I made some changes and fixes. The error has stopped.</p>

<p>Now, my urls.py is like:</p>

<pre><code>handler404 = 'views.page_not_found'

urlpatterns = [
    url(r'^$', views.home),]    
</code></pre>

<p>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:</p>

<p>"<strong>Not Found</strong> </p>

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

<p>Also, my settings.py:</p>

<pre><code>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',
            ],
        },
    },
]
</code></pre>

<p>My project tree:</p>

<pre><code>âââ 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
</code></pre>

<p>The <code>handler404</code> should be outside <code>urlpatterns</code>. If the view is called <code>page_not_found</code>, then it should refer to <code>page_not_found</code>, not <code>handler404</code>.</p>

<pre><code>handler404 = 'views.page_not_found'

urlpatterns = [
    url(r'^$', views.home),
]
</code></pre>

<p>However, in your case, you do not need a custom 404 handler at all. Remove the <code>handler404</code> line completely, and the default <a href="https://docs.djangoproject.com/en/1.10/ref/views/#django.views.defaults.page_not_found" rel="nofollow"><code>page_not_found</code></a> view will render your <code>404.html</code> template.</p>

python django