commit
16ec75c73a
21 changed files with 501 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,16 @@ |
||||
""" |
||||
ASGI config for djg_bbs project. |
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``. |
||||
|
||||
For more information on this file, see |
||||
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ |
||||
""" |
||||
|
||||
import os |
||||
|
||||
from django.core.asgi import get_asgi_application |
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djg_bbs.settings') |
||||
|
||||
application = get_asgi_application() |
@ -0,0 +1,128 @@ |
||||
""" |
||||
Django settings for djg_bbs project. |
||||
|
||||
Generated by 'django-admin startproject' using Django 4.1. |
||||
|
||||
For more information on this file, see |
||||
https://docs.djangoproject.com/en/4.1/topics/settings/ |
||||
|
||||
For the full list of settings and their values, see |
||||
https://docs.djangoproject.com/en/4.1/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.1/howto/deployment/checklist/ |
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret! |
||||
SECRET_KEY = 'django-insecure-oo$7e3(y-&-*5vf(2bbtwz&8v+*r2$)rjmm^7@08z_vtuzgd4(' |
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production! |
||||
DEBUG = True |
||||
|
||||
ALLOWED_HOSTS = [] |
||||
|
||||
|
||||
# Application definition |
||||
|
||||
INSTALLED_APPS = [ |
||||
'post.apps.PostConfig', |
||||
'django.contrib.admin', |
||||
'django.contrib.auth', |
||||
'django.contrib.contenttypes', |
||||
'django.contrib.sessions', |
||||
'django.contrib.messages', |
||||
'django.contrib.staticfiles', |
||||
] |
||||
|
||||
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 = 'djg_bbs.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 = 'djg_bbs.wsgi.application' |
||||
|
||||
|
||||
# Database |
||||
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases |
||||
|
||||
DATABASES = { |
||||
'default': { |
||||
'ENGINE': 'django.db.backends.mysql', |
||||
'NAME': 'django_bbs', |
||||
'USER': 'django', |
||||
'PASSWORD': 'djangopwd', |
||||
'HOST': 'home.rogersun.online', |
||||
'PORT': '3306', |
||||
} |
||||
} |
||||
|
||||
|
||||
# Password validation |
||||
# https://docs.djangoproject.com/en/4.1/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.1/topics/i18n/ |
||||
|
||||
LANGUAGE_CODE = 'zh-Hans' |
||||
|
||||
TIME_ZONE = 'Asia/Shanghai' |
||||
|
||||
USE_I18N = True |
||||
|
||||
USE_TZ = False |
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images) |
||||
# https://docs.djangoproject.com/en/4.1/howto/static-files/ |
||||
|
||||
STATIC_URL = 'static/' |
||||
|
||||
# Default primary key field type |
||||
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field |
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
@ -0,0 +1,128 @@ |
||||
""" |
||||
Django settings for djg_bbs project. |
||||
|
||||
Generated by 'django-admin startproject' using Django 4.1. |
||||
|
||||
For more information on this file, see |
||||
https://docs.djangoproject.com/en/4.1/topics/settings/ |
||||
|
||||
For the full list of settings and their values, see |
||||
https://docs.djangoproject.com/en/4.1/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.1/howto/deployment/checklist/ |
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret! |
||||
SECRET_KEY = 'django-insecure-oo$7e3(y-&-*5vf(2bbtwz&8v+*r2$)rjmm^7@08z_vtuzgd4(' |
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production! |
||||
DEBUG = True |
||||
|
||||
ALLOWED_HOSTS = [] |
||||
|
||||
|
||||
# Application definition |
||||
|
||||
INSTALLED_APPS = [ |
||||
'django.contrib.admin', |
||||
'django.contrib.auth', |
||||
'django.contrib.contenttypes', |
||||
'django.contrib.sessions', |
||||
'django.contrib.messages', |
||||
'django.contrib.staticfiles', |
||||
] |
||||
|
||||
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 = 'djg_bbs.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 = 'djg_bbs.wsgi.application' |
||||
|
||||
|
||||
# Database |
||||
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases |
||||
|
||||
DATABASES = { |
||||
'default': { |
||||
'ENGINE': 'django.db.backends.mysql', |
||||
'NAME': 'django_bbs', |
||||
'USER': 'django', |
||||
'PASSWORD': 'djangopwd', |
||||
'HOST': 'home.rogersun.online', |
||||
'PORT': '3306', |
||||
} |
||||
} |
||||
|
||||
|
||||
# Password validation |
||||
# https://docs.djangoproject.com/en/4.1/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.1/topics/i18n/ |
||||
|
||||
LANGUAGE_CODE = 'zh-Hans' |
||||
|
||||
TIME_ZONE = 'Asia/Shanghai' |
||||
|
||||
USE_I18N = True |
||||
|
||||
USE_TZ = False |
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images) |
||||
# https://docs.djangoproject.com/en/4.1/howto/static-files/ |
||||
|
||||
STATIC_URL = 'static/' |
||||
|
||||
# Default primary key field type |
||||
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field |
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
@ -0,0 +1,21 @@ |
||||
"""djg_bbs URL Configuration |
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see: |
||||
https://docs.djangoproject.com/en/4.1/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 |
||||
|
||||
urlpatterns = [ |
||||
path('admin/', admin.site.urls), |
||||
] |
@ -0,0 +1,16 @@ |
||||
""" |
||||
WSGI config for djg_bbs project. |
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``. |
||||
|
||||
For more information on this file, see |
||||
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ |
||||
""" |
||||
|
||||
import os |
||||
|
||||
from django.core.wsgi import get_wsgi_application |
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djg_bbs.settings') |
||||
|
||||
application = get_wsgi_application() |
@ -0,0 +1,22 @@ |
||||
#!/usr/bin/env python |
||||
"""Django's command-line utility for administrative tasks.""" |
||||
import os |
||||
import sys |
||||
|
||||
|
||||
def main(): |
||||
"""Run administrative tasks.""" |
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djg_bbs.settings') |
||||
try: |
||||
from django.core.management import execute_from_command_line |
||||
except ImportError as exc: |
||||
raise ImportError( |
||||
"Couldn't import Django. Are you sure it's installed and " |
||||
"available on your PYTHONPATH environment variable? Did you " |
||||
"forget to activate a virtual environment?" |
||||
) from exc |
||||
execute_from_command_line(sys.argv) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
main() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,112 @@ |
||||
from django.contrib import admin |
||||
from post.models import Topic, Comment |
||||
|
||||
|
||||
# Register your models here. |
||||
|
||||
# method 1 |
||||
# admin.site.register([Topic, Comment]) |
||||
|
||||
# method 2 |
||||
# class TopicAdmin(admin.ModelAdmin): |
||||
# pass |
||||
# |
||||
# |
||||
# class CommentAdmin(admin.ModelAdmin): |
||||
# pass |
||||
# |
||||
# |
||||
# admin.site.register(Topic, TopicAdmin) |
||||
# admin.site.register(Comment, CommentAdmin) |
||||
|
||||
# method 3 |
||||
@admin.register(Topic) |
||||
class TopicAdmin(admin.ModelAdmin): |
||||
# 列表页面定制 |
||||
list_display = ('title', 'topic_content', 'topic_is_online', 'user') |
||||
actions = ['topic_online', 'topic_offline'] |
||||
search_fields = ['title', 'user__username'] |
||||
ordering = ['title'] |
||||
list_per_page = 1 |
||||
list_max_show_all = 5 |
||||
|
||||
# changeForm页面定制 |
||||
# fields = ['user', 'title', 'is_online'] # 设置编辑新增页面的元素信息 |
||||
# fields = [('user', 'title'), 'content', 'is_online'] # 设置编辑新增页面的元素信息 |
||||
|
||||
# exclude = ['user'] # 设置编辑新增页面不显示的元素 |
||||
|
||||
def topic_online(self, request, queryset): |
||||
rows_updated = queryset.update(is_online=True) |
||||
self.message_user(request, '%s topic online' % rows_updated) |
||||
|
||||
topic_online.short_description = u'上线所选的 %s' % Topic._meta.verbose_name |
||||
|
||||
def topic_offline(self, request, queryset): |
||||
row_updated = queryset.update(is_online=False) |
||||
self.message_user(request, '%s topics offline' % row_updated) |
||||
|
||||
topic_offline.short_description = u'下线所选的 %s' % Topic._meta.verbose_name |
||||
|
||||
def topic_is_online(self, obj): |
||||
return u'是' if obj.is_online else u'否' |
||||
|
||||
topic_is_online.short_description = u'话题是否在线' |
||||
|
||||
def topic_content(self, obj): |
||||
return obj.content[:30] |
||||
|
||||
topic_content.short_description = u'话题内容' |
||||
|
||||
class TitleFilter(admin.SimpleListFilter): |
||||
title = ('标题过滤') |
||||
parameter_name = 'tf' |
||||
|
||||
def lookups(self, request, model_admin): |
||||
return ( |
||||
('001', ('包含001')), |
||||
('!001', ('不包含001')), |
||||
) |
||||
|
||||
def queryset(self, request, queryset): |
||||
if self.value() == '001': |
||||
return queryset.filter(title__contains=self.value()) |
||||
elif self.value() == '!001': |
||||
return queryset.exclude(title__contains=self.value()[1:]) |
||||
else: |
||||
return queryset |
||||
|
||||
list_filter = [TitleFilter, 'user__username'] |
||||
|
||||
def get_ordering(self, request): |
||||
if request.user.is_superuser: |
||||
return ['id'] |
||||
else: |
||||
return self.ordering |
||||
|
||||
# def get_queryset(self, request): |
||||
# return self.model._default_manager.filter(title__contains='1') |
||||
|
||||
# changeForm页面定制 深度定制分组 |
||||
fieldsets = ( |
||||
( |
||||
'Topic Part A', |
||||
{ |
||||
'fields': ('title', 'user'), |
||||
'description': 'Topic的Title和User' |
||||
} |
||||
), |
||||
( |
||||
'Topic Part B', |
||||
{ |
||||
'fields': ('content', 'is_online'), |
||||
'classed': ['collapse', 'wide'], |
||||
'description': 'Topic的Content和is_online' |
||||
} |
||||
) |
||||
) |
||||
|
||||
|
||||
@admin.register(Comment) |
||||
class CommentAdmin(admin.ModelAdmin): |
||||
pass |
@ -0,0 +1,6 @@ |
||||
from django.apps import AppConfig |
||||
|
||||
|
||||
class PostConfig(AppConfig): |
||||
default_auto_field = 'django.db.models.BigAutoField' |
||||
name = 'post' |
@ -0,0 +1,46 @@ |
||||
from django.db import models |
||||
from django.contrib.auth.models import User |
||||
|
||||
|
||||
# Create your models here. |
||||
|
||||
class BaseModel(models.Model): |
||||
""" |
||||
Post应用的Model基类 |
||||
""" |
||||
|
||||
class Meta: |
||||
abstract = True |
||||
ordering = ['-created_time'] |
||||
|
||||
created_time = models.DateTimeField(auto_now_add=True, help_text=u'创建时间') |
||||
last_modified = models.DateTimeField(auto_now=True, help_text=u'修改时间') |
||||
|
||||
def __str__(self): |
||||
raise NotImplementedError |
||||
|
||||
|
||||
class Topic(BaseModel): |
||||
""" |
||||
BBS论坛发布话题 |
||||
""" |
||||
title = models.CharField(max_length=255, unique=True, help_text=u'话题标题') |
||||
content = models.TextField(help_text=u'话题内容') |
||||
is_online = models.BooleanField(default=True, help_text=u'话题是否在线') |
||||
user = models.ForeignKey(to=User, to_field='id', on_delete=models.CASCADE, help_text=u'关联用户表') |
||||
|
||||
def __str__(self): |
||||
return '%d: %s' % (self.id, self.title) |
||||
|
||||
|
||||
class Comment(BaseModel): |
||||
""" |
||||
BBS话题评论 |
||||
""" |
||||
content = models.CharField(max_length=255, help_text=u'话题评论') |
||||
topic = models.ForeignKey(to=Topic, to_field='id', on_delete=models.CASCADE, help_text=u'关联话题表') |
||||
up = models.IntegerField(default=0, help_text=u'支持') |
||||
down = models.IntegerField(default=0, help_text=u'反对') |
||||
|
||||
def __str__(self): |
||||
return '%d:" %s' % (self.id, self.content[0:20]) |
@ -0,0 +1,3 @@ |
||||
from django.test import TestCase |
||||
|
||||
# Create your tests here. |
@ -0,0 +1,3 @@ |
||||
from django.shortcuts import render |
||||
|
||||
# Create your views here. |
Loading…
Reference in new issue