今天是2019年的2月23日,14时24分,我的发布会签到系统的开发部分算是告一段落了,从今天起,我就要正式迈入测试的环节了噔噔蹬蹬!!!
话虽然这么说,但还有很多功能需要进一步完善,比如登录页面需要增加样式;发布会与嘉宾的添加、修改、批量导入发布会与嘉宾数据等功能;以及签到页面,虽然功能已经实现了,但签到页的设计风格还要与发布会的主题相匹配。如果想让该系统用于实际生产,这些都是接下来需要考虑的问题。
最近学会了如何用github来管理我的项目代码,所以说大家就有福啦~可以直接点进这个链接:https://github.com/zhaiyujiahaokeai/guest 查看我做的发布会签到系统代码。如果有不会用的同学呢,也不怕,老规矩,我会在本文的最后面附上本节前端页面开发中涉及到的代码。
总体设计思路概览:
详细设计思路:
实现代码:
【settings.py】
"""
Django settings for guest project.
Generated by 'django-admin startproject' using Django 1.10.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '64awtw4pq!dsff7wicy#$n!=$ml2*v@uqi1^*w1-ra^&!qwd(^'
# 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',
'sign',
'bootstrap3',
]
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 = 'guest.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'guest.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3308',
'NAME': 'guest',
'USER': 'root',
'PASSWORD': '123456',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
【urls.py】
"""guest URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from sign import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index),
url(r'^index/$', views.index),
url(r'^accounts/login/$', views.index),
url(r'^login_action/$', views.login_action),
url(r'^event_manage/$', views.event_manage),
url(r'^search_name/$', views.search_name),
url(r'^guest_manage/$', views.guest_manage),
url(r'^sign_index/(?P[0-9]+)/$', views.sign_index),
url(r'^sign_index_action/(?P[0-9]+)/$', views.sign_index_action),
url(r'^logout/$',views.logout),
]
【views.py】
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from sign.models import Event, Guest
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request): # 登录表单
return render(request, "index.html")
def login_action(request): # 登录动作
if request.method == 'POST':
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user) # 登录
request.session['user'] = username # 将session信息记录到浏览器
response = HttpResponseRedirect('/event_manage/')
return response
else:
return render(request, 'index.html', {'error': 'username or password error!'})
@login_required
def event_manage(request): # 发布会管理
event_list = Event.objects.all()
username = request.session.get('user', '') # 读取浏览器session
return render(request, "event_manage.html", {"user": username, "events": event_list})
@login_required
def search_name(request): # 发布会名称搜索
username = request.session.get('user', '')
search_name = request.GET.get("name", "")
event_list = Event.objects.filter(name__contains=search_name)
return render(request, "event_manage.html", {"user": username, "events": event_list})
@login_required
def guest_manage(request): # 嘉宾管理
username = request.session.get('user', '')
guest_list = Guest.objects.all()
paginator = Paginator(guest_list, 2)
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# 如果page不是整数,取第一页面数据
contacts = paginator.page(1)
except EmptyPage:
# 如果page不在范围内,取最后一页面
contacts = paginator.page(paginator.num_pages)
return render(request, "guest_manage.html", {"user": username, "guests": contacts})
@login_required
def sign_index(request, eid): # 签到页面
event = get_object_or_404(Event, id=eid)
return render(request, 'sign_index.html', {'event': event})
@login_required
def sign_index_action(request, eid): # 签到动作
event = get_object_or_404(Event, id=eid)
phone = request.POST.get('phone', '')
print(phone)
result = Guest.objects.filter(phone=phone)
if not result:
return render(request, 'sign_index.html', {'event': event, 'hint': 'phone error.'})
result = Guest.objects.filter(phone=phone, event_id=eid)
if not result:
return render(request, 'sign_index.html', {'event': event, 'hint': 'event id or phone error.'})
result = Guest.objects.get(phone=phone, event_id=eid)
if result.sign:
return render(request, 'sign_index.html', {'event': event, 'hint': "user has sign in."})
else:
Guest.objects.filter(phone=phone, event_id = eid).update(sign='1')
return render(request, 'sign_index.html', {'event': event, 'hint': 'sign in success!', 'guest': result})
@login_required
def logout(request): # 退出登录
auth.logout(request) # 退出登录
response = HttpResponseRedirect('/index/')
return response
【event_manage.html】
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
Guest Manage
id 名称 状态 地址 时间 签到
{% for event in events %}
{{ event.id }}
{{ event.name }}
{{ event.status }}
{{ event.address }}
{{ event.start_time }}
sign
{% endfor %}
【guest_manage.html】
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
Guest Manage
id 名称 手机 Email 签到 发布会
{% for guest in guests %}
{{ guest.id }}
{{ guest.realname }}
{{ guest.phone }}
{{ guest.email }}
{{ guest.sign }}
{{ guest.event }}
{% endfor %}
【sign_index.html】
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
Guest Manage
```