{"id":667,"date":"2020-12-28T15:19:42","date_gmt":"2020-12-28T12:19:42","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=667"},"modified":"2020-12-28T15:26:13","modified_gmt":"2020-12-28T12:26:13","slug":"creating-custom-404-page-in-python-django","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/","title":{"rendered":"Creating Custom 404 Page in Python Django"},"content":{"rendered":"\n<p>When we develop a web application with Django, the user may  enter a <strong>url<\/strong> that does not exist. In this case, Django will present the user with a standard <strong>404 page<\/strong> that is not stylish. Instead, we can create custom 404 page in Django.<\/p>\n\n\n\n<p>You can learn how to create custom 404 page by following the steps below.<\/p>\n\n\n\n<h4>Step 1<\/h4>\n\n\n\n<p>In order to see the 404 page we created, it is necessary to change DEBUG status to False in settings.py.<\/p>\n\n\n\n<p><code>DEBUG = False<\/code><\/p>\n\n\n\n<h4>Step 2<\/h4>\n\n\n\n<p>Next we need to create an alternative 404.html page. We have to create this file in a location we want in the folder where our templates are.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">TEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [os.path.join(BASE_DIR, 'templates'),],  # Templates pathway\n        'APP_DIRS': True,\n        'OPTIONS': {\n            'context_processors': [\n                'django.template.context_processors.debug',\n                'django.template.context_processors.request',\n                'django.contrib.auth.context_processors.auth',\n                'django.contrib.messages.context_processors.messages',\n            ],\n        },\n    },\n]<\/pre>\n\n\n\n<h4>Step 3<\/h4>\n\n\n\n<p>There are predefined handler methods inside the <code>django.urls<\/code> functions. <br>To change this, we add the following line of code to our <strong>urls.py<\/strong> file;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from django.urls import path\nfrom main.views import *\n\nhandler404 = 'main.views.handler404'  #add to urls.py file\n\napp_name = \"main\"\n\nurlpatterns = [\n    path('', home, name=\"home\"),\n<\/pre>\n\n\n\n<p>We are crushing the existing <strong>handler404<\/strong> function with the handler404 function that will work in this way.<\/p>\n\n\n\n<h4>Step 4<\/h4>\n\n\n\n<p>We define our handler404 function in the views.py file in our <strong>main<\/strong> app.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># 404 PAGE \ndef handler404(request, exception=None):\n    return render(request, 'auth\/404.html')\n<\/pre>\n\n\n\n<p>In the picture below, you can see the location of the <strong>views.py<\/strong> file in our <strong>main<\/strong> app and the file structure of our templates where our <strong>404.html<\/strong> page is located.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"253\" height=\"536\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-21.png\" alt=\"\" class=\"wp-image-664\"\/><\/figure>\n\n\n\n<p>After completing these steps, you can view the 404 page by entering a url that there is not in your application from the browser.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we develop a web application with Django, the user may enter a url that does not exist. In this case, Django will present the user with a standard 404 page that is not stylish. Instead, we can create custom 404 page in Django. You can learn how to create custom 404 page by following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Custom 404 Page in Python Django - blog website<\/title>\n<meta name=\"description\" content=\"When we develop a web application with Django, the user may enter a url that does not exist. In this case, Django will present the user with a standard 404 page that is not stylish. Instead, we can create custom 404 page in Django.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Custom 404 Page in Python Django - blog website\" \/>\n<meta property=\"og:description\" content=\"When we develop a web application with Django, the user may enter a url that does not exist. In this case, Django will present the user with a standard 404 page that is not stylish. Instead, we can create custom 404 page in Django.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-28T12:19:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-28T12:26:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-21.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-21.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-21.png\",\"width\":253,\"height\":536},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/\",\"name\":\"Creating Custom 404 Page in Python Django - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/#primaryimage\"},\"datePublished\":\"2020-12-28T12:19:42+00:00\",\"dateModified\":\"2020-12-28T12:26:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"When we develop a web application with Django, the user may enter a url that does not exist. In this case, Django will present the user with a standard 404 page that is not stylish. Instead, we can create custom 404 page in Django.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/28\/creating-custom-404-page-in-python-django\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Custom 404 Page in Python Django\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/667"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":8,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":675,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/667\/revisions\/675"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}