社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

打分吧!客服小姐姐,评分页面与客户总分页面的 Django 实现

梦想橡皮擦 • 3 年前 • 567 次点击  

橡皮擦,一个逗趣的互联网高级网虫。新的系列,让我们一起进入 Django 世界。

已经完成的文章

十四、打分页面逻辑与实现

14.1 打分页面功能实现

本篇博客主要内容为实现打分系统的打分页面,最终效果为简版格式。
首先在 templates/ttt 目录中新建一个 detail.html 文件,输入如下内容。

{% if customer %}
<h2>正在为客户【{{customer.name}}】打分</h2>
{% if state == "success" %}
<h3>打分成功!</h3>
{% endif %}

<form method="post">
  {% csrf_token %}
  <label for="s">请打分:</label>
  <select name="s" id="s">
    <option value="10">10</option>
    <option value="30">30</option>
    <option value="50">50</option>
    <option value="70">70</option>
    <option value="90">90</option>
    <option value="100">100</option>
  </select>
  <button type="submit">确定打分</button>
</form>
<a href="{% url 'scoring:index' %}">返回列表</a>
{% else %}
<p>无客户</p>
{% endif %}

该页面由上一篇博客中超链接跳转进入。

20210411210329632[1].png

接下来修改 views.py ,补充打分数据保存相关逻辑。

# 客户详情&打分页面
def detail(request, _id):
    # 用户打分状态信息
    state = None
    # 渲染详情页面
    customer = Customer.objects.get(_id=_id)
    # 当用户提交打分信息
    if request.method == "POST":
        user_score_number = request.POST.get("s", 0)

        user_score = Score(customer=customer, score=user_score_number)
        user_score.save()
        state = "success"  # 表示打分成功

    context = {
        "customer": customer,
        "state": state
    }
    return render(request, "ttt/detail.html", context)

在实现该页面逻辑时,发现之前在打分模型中增加的 User 模型的外键出现问题,必须登录才能保存数据,故修改 Score 模型如下,去除 User 模型外键。

class Score(models.Model):
    # 自增主键
    _id = models.AutoField(primary_key=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    score = models.IntegerField(default=0, verbose_name="分数")
    # user_id = models.ForeignKey(User, on_delete=models.CASCADE)

模型修改之后,需要通过 migrate 迁移命令将修改保存到文件中。
这些内容都修改完毕,就可以通过 http://127.0.0.1:8000/scoring/2/ 访问打分页面了,点击确定打分,将数据发送到 views.py 中进行保存。

20210411211105441[1].png

14.2 Django 知识点补充

在上文代码中的 detail.html 页面出现的 {% csrf_token %} 代码块,用于在 post 提交数据时,防止伪造跨站点请求。
request.POST 是一个类字典对象,用于通过关键字获取提交的数据,例如上述代码中,通过 request.POST.get("s", 0) 获取 detail.html 页面提交的下拉列表内容。

对于上述代码,可以进行修改,当小姐姐打分之后,直接跳转到客户列表页面。该编码习惯可以防止用户重复提交数据,建议牢记。

# 客户详情&打分页面
def detail(request, _id):
    # 渲染详情页面
    customer = Customer.objects.get(_id=_id)

    if request.method == "POST":
        user_score_number = request.POST.get("s", 0)
        user_score = Score(customer=customer, score=user_score_number)
        user_score.save()
        # 页面跳转
        return HttpResponseRedirect(reverse("scoring:index"))

    else:
        context = {
            "customer": customer
        }
        return render(request, "ttt/detail.html", context)

14.3 客户分数汇总页面

客户分数查询只需要用到 Django 中对符合条件的列求和即可。该部分细节知识点会在后续博客中进行讲解,本文优先实现最终效果即可。

# 客户分数查阅
def show_score(request, _id):
    customer = Customer.objects.get(_id=_id)
    total = Score.objects.filter(customer=customer).aggregate(nums=Sum('score'))
    print(total['nums'])
    return HttpResponse(f"客户 {id} 的总分是 {total}")

上述代码只会输入一段文本,对其进行修改并匹配上相应的模板,代码如下:

# 客户分数查阅
def show_score(request, _id):
    customer = Customer.objects.get(_id=_id)
    total = Score.objects.filter(customer=customer).aggregate(nums=Sum('score'))

    context = {
        "customer":customer,
        "total": total["nums"]
    }
    return render(request, "ttt/show_score.html", context)

show_score.html 文件代码如下:




    
{% if customer %}
<h2>{{ customer.name }} 的总得分是 {{ total }}</h2>
{% else %}
<p>无客户</p>
{% endif %}

最终无效果的静态页面如下图所示。

20210411213645603[1].png

14.4 本篇博客小节

本篇博客实现了打分系统的评分页面与客户分数汇总展示页面,希望你能在本篇博客中学到知识。

相关阅读

  1. Python 爬虫 100 例教程,超棒的爬虫教程,立即订阅吧
  2. Python 游戏世界(更新中,目标文章数 50+,现在订阅,都是老粉)
  3. Python 爬虫小课,精彩 9 讲,只要 9 块 9

今天是持续写作的第 <font color="red">132</font> / 200 天。
如果你想跟博主建立亲密关系,可以关注同名公众号 <font color="red">梦想橡皮擦</font>,近距离接触一个逗趣的互联网高级网虫。
博主 ID:梦想橡皮擦,希望大家<font color="red">点赞</font>、<font color="red">评论</font>、<font color="red">收藏</font>。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/119752
 
567 次点击