Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to 404 custom page not found in django

#STEP 1: Create your custom 404.html template in your template folder 

#STEP 2: In your views create a new view function 
def error_404_handler(request, exception):
    return render(request, 'crypto/404.html')
  
#STEP 3: Go to your url.py file in your project file and add this
from django.conf.urls import handler404
handler404 = 'appname.views.error_404_handler'

#STEP 4: In your setting.py change these settings
DEBUG = FALSE
ALLOWED_HOSTS = ['*'] #Do not use in production

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',
            ],
        },
    },
]
Comment

how to custom page not found in django

#STEP 1: In your template folder create a new html file named 404.html and with custom style of whatever you want
#STEP 2: Go to your setting.py and change debug to false also adding your domain names

DEBUG = FALSE
ALLOWED_HOSTS = ['*'] #NEVER USE THIS IN PRODUCTION!!!!!

#STEP 3: Add the template path to your template directory
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',
            ],
        },
    },
]

#STEP 4: If you get and error that says: ValueError: Missing staticfiles manifest entry for 'admin/css/base.css' Then run 'python manage.py collectstatic' locally and make sure {% load static %} is at the of your html file'''
		
Comment

PREVIOUS NEXT
Code Example
Python :: iterate over list and select 2 values together python 
Python :: extract url from page python 
Python :: how to delete a specific line in a file 
Python :: django rest documentation 
Python :: plot histogram in seaborn 
Python :: how to use with open 
Python :: Get last “column” after .str.split() operation on column in pandas DataFrame 
Python :: tuple slicing in python 
Python :: lista to txt python 
Python :: how to check django rest framework version 
Python :: check pandas version 
Python :: what is join use for in python 
Python :: how to get the percentage accuracy of a model in python 
Python :: python undefine variable 
Python :: python change column order in dataframe 
Python :: convert string to utf8 python 
Python :: describe function in pandas 
Python :: how to convert a byte array to string in python 
Python :: pyautogui color 
Python :: python delete white spaces 
Python :: changing axis labels matplotlib 
Python :: datediff in seconds in pandas 
Python :: python copy an object 
Python :: pandas apply function to every row 
Python :: How to draw a rectangle in cv2 
Python :: current date to epoch python 
Python :: password generator in python 
Python :: merge dictionaries in python 
Python :: python palindrome 
Python :: pyspark when otherwise multiple conditions 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =