1. 程式人生 > >CRM 問卷調查功能組件

CRM 問卷調查功能組件

生成器 rand ima enter shortcuts append footer objects css

目錄結構:

技術分享圖片

母版

技術分享圖片
{% load staticfiles %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>
{% block title %}{% endblock %}</title> <link rel="stylesheet" href="{% static ‘plugins/bootstrap/css/bootstrap.min.css‘ %}"> <body> {% include ‘header.html‘ %} <div class="container-fluid"> {% block content %} {% endblock %} </div> {% include ‘footer.html‘ %} {% block javascript %} {% endblock %}
</body> </html> 
base.html 技術分享圖片
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">CRM系統</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse"
> <ul class="nav navbar-nav navbar-right"> <li><a href="#">任務</a></li> <li><a href="#">通知</a></li> <li><a href="#">消息</a></li> <li><a href="#">登錄</a></li> </ul> </div> </div> </nav>
header.html 技術分享圖片
<div class="container-fluid " style="position:absolute;bottom:0;width:100%;height:100px;background-color: #eeeeee;">
    <hr>
    <p class="text-center"> © 2018 lcg</p>
</div>
footer.html

數據庫設計

models.py

from django.db import models

from django.db import models


class UserInfo(models.Model):
    """
    員工表
    """
    name = models.CharField(max_length=32)

    def __str__(self):
        return self.name


class ClassInfo(models.Model):
    """
    班級表
    """
    name = models.CharField(max_length=32)

    def __str__(self):
        return self.name


class Student(models.Model):
    """
    學生表
    """
    user = models.CharField(max_length=32)
    pwd = models.CharField(max_length=32)
    cls = models.ForeignKey(to=ClassInfo)

    def __str__(self):
        return self.user


class Questionnaire(models.Model):
    """
    問卷表

    """
    title = models.CharField(max_length=64)
    cls = models.ForeignKey(to=ClassInfo)
    creator = models.ForeignKey(to=UserInfo)

    def __str__(self):
        return self.title


class Question(models.Model):
    """
    問題表
    """
    caption = models.CharField(max_length=64)

    question_type = (
        (1, ‘打分‘),
        (2, ‘單選‘),
        (3, ‘評價‘),
    )
    question_type = models.IntegerField(choices=question_type)

    questionnaire = models.ForeignKey(Questionnaire, default=1)

    def __str__(self):
        return self.caption


class Option(models.Model):
    """
    單選題的選項
    """
    option_name = models.CharField(verbose_name=‘選項名稱‘, max_length=32)
    score = models.IntegerField(verbose_name=‘選項對應的分值‘)
    question = models.ForeignKey(to=Question)

    def __str__(self):
        return self.option_name


class Answer(models.Model):
    """
    回答
    """
    student = models.ForeignKey(to=Student)
    question = models.ForeignKey(to=Question)

    # 三選一
    option = models.ForeignKey(to="Option", null=True, blank=True)
    val = models.IntegerField(null=True, blank=True)
    content = models.CharField(max_length=255, null=True, blank=True)

數據庫遷移:

python manage.py makemigrations

python manage.py migrate

創建超級用戶:
name:admin
password:admin123456

渲染編輯頁面

model_forms組件:

from .models import *
from django.forms import ModelForm


class QuestionModelForm(ModelForm):
    class Meta:
        model = Question
        fields = ["caption", "question_type"]

views.py用列表的low方法

from django.shortcuts import render, HttpResponse
from .models import *
from .model_forms import *


def questionnaire(request, questionnaire_id):
    questionnaire_obj=Questionnaire.objects.filter(id=questionnaire_id).first()
    if not questionnaire_obj:
        return HttpResponse("未找到符合要求的問卷")# 處理沒有找到問卷調查對象的情況
    question_list = Question.objects.filter(questionnaire_id=questionnaire_id)
    questionModelForm_list=[]
    for question in question_list:
        questionModelForm=QuestionModelForm(instance=question)
        questionModelForm_list.append(questionModelForm)
    return render(request, ‘survey/questionnaire.html‘, locals())

html頁渲染:

{% for questionModelForm in questionModelForm_list %}
<p>標題:{{ questionModelForm.caption }}</p>
<p>類型:{{ questionModelForm.question_type }}</p>
{% endfor %}

優化列表的low方法,采用生成器,原理參見(http://www.cnblogs.com/0bug/p/8183629.html):

def questionnaire(request, questionnaire_id):
    questionnaire_obj = Questionnaire.objects.filter(id=questionnaire_id).first()
    if not questionnaire_obj:
        return HttpResponse("未找到符合要求的問卷")  # 處理沒有找到問卷調查對象的情況

    def generate_questionModelForm():

        question_list = Question.objects.filter(questionnaire_id=questionnaire_id)
        for question in question_list:
            questionModelForm = QuestionModelForm(instance=question)
            yield questionModelForm

    return render(request, ‘survey/questionnaire.html‘, {"generate_questionModelForm": generate_questionModelForm()})

html渲染

{% extends ‘base.html‘ %}
{% block title %} 調查問卷 {% endblock %}
{% block content %}

    {% for questionModelForm in generate_questionModelForm %}
        <p>標題:{{ questionModelForm.caption }}</p>
        <p>類型:{{ questionModelForm.question_type }}</p>
    {% endfor %}

{% endblock %}

效果是一樣的:

技術分享圖片

CRM 問卷調查功能組件