6.djnago實現問卷調查系統

人走茶涼csc發表於2020-09-26

一、功能實現

1、登入介面

2、表單介面

3、後臺介面

4、excel匯出功能

5、過濾器

2、程式碼實現

直接貼程式碼吧

model.py

from django.db import models


class FormInfo(models.Model):
    """表單資訊"""
    # 1.門診編號
    od_number = models.CharField('門診編號', max_length=48)
    # 2.姓名
    name = models.CharField('姓名', max_length=24, default=None, blank=True)
    # 3.性別
    sex = models.CharField('性別', max_length=12, default=None, blank=True, null=True,)
    # 4.出生年月
    birthday = models.CharField('出生年月', max_length=128, default=None, blank=True, null=True,)
    # 5.學歷
    education = models.CharField('學歷', max_length=24, default=None, blank=True, null=True,)
    # 6.手機號,
    phone = models.CharField('手機號', max_length=20, default=None, blank=True, null=True,)
    # 7.微訊號
    chatNumber = models.CharField('微訊號', max_length=48, default=None, blank=True, null=True,)
    # 8.現居地址
    address = models.CharField('現居地址', max_length=256, default=None, blank=True, null=True,)
    # 9.初次性行為年齡
    age = models.IntegerField('初次性行為年齡', max_length=12, default=None, blank=True, null=True,)
    # 10.性行為物件
    nature_object = models.CharField('性行為物件', max_length=12, default=None, blank=True, null=True,)
    # 11.性行為人數
    nature_number = models.IntegerField('性行為人數', max_length=12, default=None, blank=True, null=True,)
    # 12.近六個月性伴侶人數
    sixNatureNumber = models.IntegerField('近六個月性伴侶人數', max_length=12, default=None, blank=True, null=True,)
    # 13.是否有多人性行為
    many_nature = models.CharField('是否有多人性行為', max_length=12, default=None, blank=True, null=True,)
    # 14.平均性生活時間
    nature_time = models.CharField('平均性生活時間', max_length=24, default=None, blank=True, null=True,)
    # 15.男-男性方式
    twoMan_nature = models.CharField('男-男性方式', max_length=24, default=None, blank=True, null=True,)
    # 16.男-女性方式
    manWoman_nature = models.CharField('男-女性方式', max_length=24, default=None, blank=True, null=True,)
    # 17.女-女性方式
    twoWoman_nature = models.CharField('女-女性方式', max_length=24, default=None, blank=True, null=True,)
    # 18.女-男性方式
    womanWman_nature = models.CharField('女-男性方式', max_length=24, default=None, blank=True, null=True,)
    # 20.幾人同時
    sexnumber = models.CharField('幾人同時', max_length=48, default=None, blank=True, null=True,)
    # 21.幾男幾女
    mannumber = models.CharField('幾男幾女', max_length=48, default=None, blank=True, null=True,)


class SubmitPassword(models.Model):
    """提交密碼"""
    password = models.CharField(max_length=128)

url配置

from django.contrib import admin
from django.urls import path, include
from formtest import views

urlpatterns = [
    # 登入頁
    path('login/', views.login),
    # 登入驗證進入主頁
    path('index/', views.index),
    # 提交
    path('submit/', views.submit),
]

檢視

from django.shortcuts import render, redirect
from formtest import models


# Create your views here.

# 登入頁
def login(request):
    return render(request, 'formtest/login.html')

# 登入驗證進入表單
def index(request):
    q = request.GET.get('check')
    if not q:
        return render(request, 'formtest/login.html')
    else:
        try:
            user = models.SubmitPassword.objects.get(password=q)
            return render(request, 'formtest/questionnaire.html')
        except:
            return render(request, 'formtest/login.html')


# 提交到資料庫
def submit(request):
    # 1.門診編號
    od_number = request.GET.get('odnumber')
    print("門診編號"+od_number)
    # 2.姓名
    name = request.GET.get('name')
    print("姓名" + name)
    # 3.性別 male為男性,female為女性
    sex = request.GET.get('Sex')
    print('性別', sex)
    # 4.出生年月
    birthYear = request.GET.get('birthYear')
    birthMonth = request.GET.get('birthMonth')
    birthDay = request.GET.get('birthDay')
    birth = birthYear + "_" + birthMonth + "_" + birthDay
    print("出生日期"+birth)
    # 5.學歷
    education = request.GET.get('education')
    print("學歷", education)
    # # 6.手機號,
    phone = request.GET.get('phoneNumber')
    print("手機號", phone)
    # # 7.微訊號
    chatNumber = request.GET.get('chatNumber')
    print("微訊號", chatNumber)
    # # 8.現居地址
    # 省
    address1 = request.GET.get('address1')
    # 市
    address2 = request.GET.get('address2')
    # 區
    address3 = request.GET.get('address3')
    print("居住地", address1, address2, address3)
    # 詳細住址
    address = request.GET.get('address')
    print("詳細住址", address)
    # # 9.初次性行為年齡
    age = request.GET.get('firstSexAge')
    print("初次性行為年齡", age)
    # # 10.性行為物件
    sextrue = request.GET.get('sexObject')
    print("性行為物件", sextrue)
    # # 11.性行為人數
    nature_number = request.GET.get('sexNum')
    print("性行為人數", nature_number)
    # # 12.近六個月性伴侶人數
    sixNatureNumber = request.GET.get('sixSexNum')
    print("近六個月性伴侶人數", sixNatureNumber)
    # # 13.是否有多人性行為 有為true沒有為false
    many_nature = request.GET.get('multiSex')
    print("多人性行為", many_nature)
    # # 14.平均性生活時間
    nature_time = request.GET.get('sexTime')
    print("平均性生活時間", nature_time)
    # # 15.男-男性方式
    twoMan_nature = request.GET.get('m2m')
    print("男男性方式", twoMan_nature)
    # # 16.男-女性方式
    manWoman_nature = request.GET.get('m2f')
    print("男女性方式", manWoman_nature)
    # # 17.女-女性方式
    twoWoman_nature = request.GET.get('f2f')
    print("女女性方式", twoWoman_nature)
    # # 18.女-男性方式
    womanWman_nature = request.GET.get('f2m')
    # print("女女性方式", womanWman_nature)
    # 19.幾人同時
    mannumber = request.GET.get('sameTimeSex')
    print('幾人同時', mannumber)
    # 20.男女比例
    manwuman = request.GET.get('manwuman')
    print('男女比例', manwuman)
    # 向資料庫新增
    models.FormInfo.objects.create(
        od_number=od_number,
        name=name,
        sex=sex,
        birthday=birth,
        education=education,
        phone=phone,
        chatNumber=chatNumber,
        address=address,
        age=age,
        nature_object=sextrue,
        nature_number=nature_number,
        sixNatureNumber=sixNatureNumber,
        many_nature=many_nature,
        nature_time=nature_time,
        # 男男
        twoMan_nature=twoMan_nature,
        # 男女
        manWoman_nature=manWoman_nature,
        #女女
        twoWoman_nature=twoWoman_nature,
        #女男
        womanWman_nature=womanWman_nature,

        sexnumber=mannumber,
        mannumber=manwuman,
    )
    return render(request, 'formtest/questionnaire.html')
admin頁的設定
from django.contrib import admin
from django.http import HttpResponse
from .models import *
import xlwt

# 行
line = 1
# 列
row = 0
# 後臺匯出為Excel
# @admin.register(Blog)
class WLIndexAdmin(admin.ModelAdmin):
    # 顯示列表頁欄位
    list_display = ['od_number', 'name', 'sex', 'birthday', 'education', 'phone', 'chatNumber', 'address', 'age',
                    'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature', 'nature_time', 'twoMan_nature',
                    'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', 'sexnumber', 'mannumber']
    # 過濾器
    list_filter = ['sex', 'education', 'age', 'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature',
                   'nature_time', 'twoMan_nature', 'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', ]

    # 搜尋功能(支援所有欄位搜尋)
    search_fields = ['od_number', 'name', 'sex', 'birthday', 'education', 'phone', 'chatNumber', 'address', 'age',
                    'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature', 'nature_time', 'twoMan_nature',
                    'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', 'sexnumber', 'mannumber']

    # 匯出excel功能
    actions = ['export_excel']

    def export_excel(self, request, queryset):
        """
        後臺匯出表單資料,匯出格式為excel
        :param request:
        :param queryset:
        :return:
        """
        meta = self.model._meta  # 用於定義檔名, 格式為: app名.模型類名
        # 模型所有欄位名
        field_names = [field.name for field in meta.fields]
        # 響應內容型別
        response = HttpResponse(content_type='application/vnd.ms-excel')
        # 定義響應資料格式
        response['Content-Disposition'] = 'FormInfo.csv'

        # 開始建立excel
        wb = xlwt.Workbook(encoding='utf-8')
        # 表名
        LST_test = wb.add_sheet('流行病學調查統計表', cell_overwrite_ok=True)
        # excel中的表頭
        LST_test.write(0, 0, '序號', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 1, '門診編號', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 2, '姓名', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 3, '性別', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 4, '出生年月', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 5, '最高學歷', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 6, '手機號', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 7, '微訊號', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 8, '現居地址', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 9, '初次性行為年齡', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 10, '性行為物件', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 11, '性行為人數', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 12, '近6個月性伴侶人數', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 13, '是否有多人性行為', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 14, '平均性生活時間', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 15, '男-男性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 16, '男-女性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 17, '女-女性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '女-男性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '幾人同時', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '幾男幾女', xlwt.easyxf('font: height 240, colour_index red,'))
        global line
        global row
        for obj in queryset:
            for field in field_names:
                data = [f'{getattr(obj, field)}']
                print(data)
                LST_test.write(line, row, "".join(data))
                row += 1
            line += 1
            row = 0
        wb.save(response)
        return response

    export_excel.short_description = '匯出為Excel'


# 登錄檔單模型
admin.site.register(FormInfo, WLIndexAdmin)
# 註冊提交密碼模型
admin.site.register(SubmitPassword)

前端頁面就不貼了  困了  睡覺!

相關文章