2026年3月21日 星期六

[水井USR] 使用Cookie來記錄旅人是否到過水井村網站

 




1.模版程式:templates/index.html

 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
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
    <meta charset="UTF-8">
    <title>水井村導覽</title>
    <style>
        body { font-family: system-ui, sans-serif; text-align: center; padding: 60px 20px; }
        .welcome { font-size: 1.8rem; margin: 2rem 0; color: #2c7a77; }
        .count   { color: #555; }
        .btn     { padding: 12px 28px; background: #2c7a77; color: white; border: none; border-radius: 8px; font-size: 1.1rem; cursor: pointer; }
        .btn:hover { background: #1e5955; }
    </style>
</head>
<body>

    <h1>水井村</h1>
    <p class="welcome">{{ message }}</p>

    {% if has_visited %}
        <p class="count">這是你第 <strong>{{ visit_count }}</strong> 次來訪</p>
        <form action="{% url 'village:forget_me' %}" method="post">
            {% csrf_token %}
            <button type="submit" class="btn">忘記我讓我重新當新客</button>
        </form>
    {% else %}
        <p>這裡有風頭水尾打拼的故事有水井三寶的傳說等你來走一趟</p>
    {% endif %}

</body>
</html>

2.視域程式:village/views.py
 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
32
33
34
35
36
37
38
39
40
41
# village/views.py
from django.shortcuts import render, redirect


def home(request):
    # 檢查是否有 visit 水井村的 cookie
    has_visited = request.COOKIES.get('visited_suijing', False)

    if has_visited:
        message = "歡迎回到水井村!老朋友又見面啦~"
        visit_count = int(has_visited) + 1
    else:
        message = "歡迎來到水井村!第一次造訪的旅人~"
        visit_count = 1

    context = {
        'message': message,
        'visit_count': visit_count,
        'has_visited': bool(has_visited),
    }

    response = render(request, 'index.html', context)

    # 設定/更新 cookie(存活 30 天)
    response.set_cookie(
        key='visited_suijing',
        value=str(visit_count),           # 順便記錄來訪次數
        max_age=60 * 60 * 24 * 30,        # 30 天
        httponly=True,                    # 推薦:防止 JavaScript 存取
        samesite='Lax',                   # 推薦的安全設定
        # secure=True,                    # 上線時建議開啟(需 HTTPS)
    )

    return response


def forget_me(request):
    """刪除 cookie,讓下次變成第一次造訪"""
    response = redirect('village:home')
    response.delete_cookie('visited_suijing')
    return response

3.APP路由程式:village/urls.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# village/urls.py

from django.urls import path
from . import views

app_name = 'village'

urlpatterns = [
    path('', views.home, name='home'),
    path('forget-me/', views.forget_me, name='forget_me'),
]

4.主路由程式:suijing_village/urls.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""suijing_village URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('village.urls')),
]

5.設定程式:suijing_village/settings.py
  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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
Django settings for suijing_village project.

Generated by 'django-admin startproject' using Django 4.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-5d5y6c=nx8d1x+tn!r2!tl7fnlj)6_+l2_o90czc#d907!q6df'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['cmlin.pythonanywhere.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'village',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'suijing_village.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]

WSGI_APPLICATION = 'suijing_village.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# default static files settings for PythonAnywhere.
# see https://help.pythonanywhere.com/pages/DjangoStaticFiles for more info
MEDIA_ROOT = '/home/cmlin/suijing_village/media'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/cmlin/suijing_village/static'
STATIC_URL = '/static/'

沒有留言:

張貼留言