Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- pagenotfound
- 데이타타입
- block_user_process
- follow 기획
- 팔로우기획
- 유튜브주소
- 로그인테스트
- url설계
- 파이썬
- fakesession
- replyschema
- 차단프로세스
- 이용자블럭
- 팔로우팔로워
- django
- 앱생성방법
- 가짜 세션
- 장고
- 제일상단으로
- logintest
- 댓글설계
- blockuser
- Block
- 반응형이미지
- gototop
- 차단시프로세스
- 영문과숫자가혼합된중복되지않는주소
- Bootstrap
- Python
- celery # redis #django #장고 #python #파이썬
Archives
- Today
- Total
코딩이야기
Django 404 페이지 세팅 본문
1. setting.py에 값을 수정
기본값으로 세팅된 DEBUG 값 TRUE를 False로 변경하고
Allowed Hosts값을 변경
# settings.py
DEBUG = False
ALLOWED_HOSTS = ['*'] # 모든 호스트를 허용. 실제 배포 환경에서는 특정 도메인 또는 IP 주소를 추가하세요.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
#Base_DIR / 'folder/newfolder/templates', <-처럼 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',
],
},
},
]
2. 404에러를 보여줄 페이지 작성
<!-- templates/404.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Not Found</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body, html {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
flex-direction: column;
}
.container h1 {
font-size: 5rem;
margin-bottom: 0;
}
.container p {
font-size: 1.5rem;
margin-top: 0;
}
.container a {
margin-top: 20px;
font-size: 1.2rem;
}
</style>
</head>
<body>
<div class="container">
<h1>404</h1>
<div>404 페이지임을 알리는 내용을 필요에 맞게 추가 </div>
</div>
</body>
</html>
3. projectname/urls.py에 404 에러핸들러 생성
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
# 404를 나타내기 위해 아래 2줄이 추가 됨.
from django.conf.urls import handler404
from django.shortcuts import render
urlpatterns = [
path('admin/', admin.site.urls),
]
def your_404(request, exception):
return render(request, '404.html', status=404)
handler404 = your_404
'BackEnd > 장고(Django)' 카테고리의 다른 글
앱 생성. (0) | 2024.06.25 |
---|---|
Django 템플릿에서는 request.COOKIES를 직접 접근할 수 없습니다. (0) | 2024.06.24 |
Django에서 실제 세션을 사용하지 않고 가짜 세션을 구현하는 방법 (0) | 2024.06.23 |
Include & {% block %} 태그 사용법 (0) | 2024.06.22 |
첫 번째 장고 앱 작성하기, part 1 (0) | 2024.05.11 |