饮冰三年-人工智能-Python-33博客园山寨版之报障管理

报障单的内容和其实和文章管理内容类似

1:创建数据模型

class Trouble(models.Model):
    '''报障单'''
    uuid = models.UUIDField(primary_key=True,auto_created=True, default=uuid.uuid4, editable=False)
    title = models.CharField(verbose_name="报障标题", max_length=1000)
    detail = models.TextField(verbose_name='报障详情')
    reportUser = models.ForeignKey(verbose_name='报修人', to="UserInfo", to_field="uid", related_name="reportUsers",
                                   on_delete=models.CASCADE, null=True)
    processUser = models.ForeignKey(verbose_name='处理人', to="UserInfo", to_field="uid", related_name="processUsers",
                                    on_delete=models.CASCADE, null=True)
    processSolution = models.TextField(verbose_name='处理方法',null=True)
    createTime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    processTime = models.DateTimeField(verbose_name='处理时间', auto_now_add=True)
    type_status = [
        (0, '已删除'),
        (1, '待处理'),
        (2, '处理中'),
        (3, '已处理'),
    ]
    pj_choices = (
        (1, '不满意'),
        (2, '一般'),
        (3, '很好'),
    )
    status = models.IntegerField(choices=type_status, default=1)
    evaluate = models.IntegerField(choices=pj_choices, null=True, default=2)
models.py

2:url设置

from django.conf.urls import url
from django.conf.urls import include
from .views import user,trouble

urlpatterns = [
    url(r'^index.html$', user.index),
    url(r'article-(?P<ms_Type>\d+)-(?P<classification_id>\d+).html$', user.article, name='article'),
    url(r'^add-article.html$',user.add_article),  #创建文章路由
    url(r'^del-article-(?P<nid>\d+).html$', user.del_article),#删除文章路由
    url(r'^edit-article-(?P<nid>\d+).html$', user.edit_article),#修改文章路由

    # 一般用户: 提交报障单,查看,修改(未处理),评分(处理完成,未评分)
    url(r'^trouble-list.html$', trouble.trouble_list),
    url(r'^trouble-create.html$', trouble.trouble_create),
    url(r'^trouble-edit-([a-zA-Z0-9-]+).html$', trouble.trouble_edit),

]
urls.py

3:Form表单

# 引入文件
from django import forms
from django.forms import fields
from django.forms import widgets
from  repository import models

class TroubleForm(forms.Form):
    title = fields.CharField(
        max_length=32,
        widget=widgets.TextInput(attrs={'class': 'form-control', 'placeholder': '报障单标题'})
    )
    detail = fields.CharField(
        widget=widgets.Textarea(attrs={'id': 'detail', 'class': 'kind-content'})
    )
Form/trouble.py

4:增删操作。View设置后台逻辑

from django.shortcuts import render,redirect,HttpResponse
from repository import models
from forms.trouble import TroubleForm  #引用form中的文章表单,用于添加、修改文章
import datetime #引入时间模块
def trouble_list(request):
    # user_info = request.session.get('user_info') # {id:'',}
    current_user_id = request.session['user_info']['uid']
    result = models.Trouble.objects.filter(reportUser_id=current_user_id).order_by('status').\
        only('uuid','title','status','createTime','processUser')
    return render(request,'Backend/backend_trouble_list.html',{'result': result})

def trouble_create(request):
    if request.method == 'GET':
        form = TroubleForm()
    else:
        form = TroubleForm(request.POST)
        if form.is_valid():
            dic = {}
            dic['reportUser_id'] = request.session['user_info']['uid'] # session中获取
            dic['createTime'] = datetime.datetime.now()
            dic['status'] = 1
            dic.update(form.cleaned_data)
            models.Trouble.objects.create(**dic)
            return redirect('/backend/trouble-list.html')
    return render(request, 'Backend/backend_trouble_create.html',{'form':form})

def trouble_edit(request,uuid):
    if request.method == "GET":
        obj = models.Trouble.objects.filter(uuid=uuid, status=1).only('uuid', 'title', 'detail').first()
        if not obj:
            return HttpResponse('已处理中的保单章无法修改..')
        # initial 仅初始化
        form = TroubleForm(initial={'title': obj.title,'detail': obj.detail,'uuid':obj.uuid})
        # 执行error会进行验证
        return render(request,'Backend/backend_trouble_edit.html',{'form':form,'uuid':uuid})
    else:
        form = TroubleForm(data=request.POST)
        if form.is_valid():
            # 受响应的行数
            v = models.Trouble.objects.filter(uuid=uuid, status=1).update(**form.cleaned_data)
            if not v:
                return HttpResponse('已经被处理')
            else:
                return redirect('/backend/trouble-list.html')
        return render(request, 'Backend/backend_trouble_edit.html', {'form': form,'uuid':uuid})
trouble.py

5:前台页面

5.1列表页面

{% extends 'Backend/backend_layout.html' %}

{% block conent %}
    <ol class="breadcrumb" style="margin-bottom: 0;">
        <li><a href="#">保障管理</a></li>
        <li class="active">保障列表</li>
    </ol>
    <div>

        <div class="clearfix"  style="height: 36px;line-height: 35px;padding: 0 15px;border-top: 1px solid #dddddd;background-color: #f1f0f0">
            <i class="fa fa-table" aria-hidden="true"></i>
            <a target="_blank" href="/backend/trouble-create.html" class="right"
               style="display: inline-block;padding:0 10px;background-color: #428bca;color: #ffffff;">
                <i class="fa fa-plus-circle" aria-hidden="true"></i>
                创建报障单
            </a>
        </div>

        <table class="table table-bordered">
            <thead>
            <tr>
                <th>保障标题</th>
                <th>状态</th>
                <th>创建时间</th>
                <th>处理人</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for row in result %}
                <tr>
                    <td>{{ row.title }}</td>
                    <td>{{ row.get_status_display }}</td>
                    <td>{{ row.createTime }}</td>
                    <td>{{ row.processUser.username }}</td>
                    <td>
                        <a class="btn btn-danger btn-xs">
                            <i class="fa fa-times" aria-hidden="true"></i>
                            删除
                        </a>
                        |
                        <a class="btn btn-primary btn-xs" href="/backend/trouble-edit-{{ row.uuid }}.html">
                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                            编辑
                        </a>
                    </td>
                </tr>
            {% endfor %}

            </tbody>
        </table>
        <div class="clearfix">
            <ul class="pagination right" style="margin-top: 0">
               {{ page_str }}
            </ul>
        </div>
    </div>


{% endblock %}

{% block js %}

{% endblock %}
backend_trouble_list.html

5.2 新增页面

{% extends 'Backend/backend_layout.html' %}

{% block css %}
    <link rel="stylesheet" href="/static/plugins/kindeditor/themes/default/default.css"/>
    <style>
        .kind-content {
            width: 100%;
            min-height: 500px;
        }
    </style>
{% endblock %}

{% block conent %}
    <ol class="breadcrumb" style="margin-bottom: 0;">
        <li><a href="#">保障管理</a></li>
        <li class="active">创建报障单</li>
    </ol>
    <div style="padding: 5px 8px;">
        <form method="POST" action="/backend/trouble-create.html" novalidate>
            {% csrf_token %}
            <div class="form-group">
                <label for="{{ form.title.id_for_label }}">标题 <span>{{ form.title.errors.0 }}</span></label>
                {{ form.title }}
            </div>
            <div class="form-group">
                <label for="detail">内容 <span>{{ form.detail.errors.0 }}</span></label>
                {{ form.detail }}
            </div>

            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="保 存">
            </div>
        </form>
    </div>


{% endblock %}

{% block js %}
    <script charset="utf-8" src="/static/plugins/kindeditor/kindeditor-all-min.js"></script>
    <script charset="utf-8" src="/static/plugins/kindeditor/lang/zh-CN.js"></script>
    <script>
        KindEditor.ready(function (K) {
            var editor = K.create('#detail', {
                resizeType: 1
            });
        });
    </script>
{% endblock %}
backend_trouble_create.html

5.3 编辑页面

{% extends 'Backend/backend_layout.html' %}

{% block css %}
    <link rel="stylesheet" href="/static/plugins/kindeditor/themes/default/default.css"/>
    <style>
        .kind-content {
            width: 100%;
            min-height: 500px;
        }
    </style>
{% endblock %}

{% block conent %}
    <ol class="breadcrumb" style="margin-bottom: 0;">
        <li><a href="#">保障管理</a></li>
        <li class="active">修改报障单</li>
    </ol>
    <div style="padding: 5px 8px;">
        <form method="POST" action="/backend/trouble-edit-{{ uuid }}.html" novalidate>
            {% csrf_token %}
            <div class="form-group">
                <label for="{{ form.title.id_for_label }}">标题 <span>{{ form.title.errors.0 }}</span></label>
                {{ form.title }}
            </div>
            <div class="form-group">
                <label for="detail">内容 <span>{{ form.detail.errors.0 }}</span></label>
                {{ form.detail }}
            </div>

            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="保 存">
            </div>
        </form>
    </div>


{% endblock %}

{% block js %}
    <script charset="utf-8" src="/static/plugins/kindeditor/kindeditor-all-min.js"></script>
    <script charset="utf-8" src="/static/plugins/kindeditor/lang/zh-CN.js"></script>
    <script>
        KindEditor.ready(function (K) {
            var editor = K.create('#detail', {
                resizeType: 1
            });
        });
    </script>
{% endblock %}
backend_trouble_edit.html

猜你喜欢

转载自www.cnblogs.com/YK2012/p/10927556.html