6.djnago实现问卷调查系统

一、功能实现

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)

前端页面就不贴了  困了  睡觉!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页