django11-シンプルな酔っぱらいの自慢のメニューと一人当たりの支払い額のクエリページを作成する

I.はじめに

グループで食事を注文する場合、レストランのメニューを人数や一人当たりの量で考えることが多いため、大鍋料理のパートナーにとって注文は学習が難しく、難しい問題になっています。実用的な問題を解決するために巧妙にアイデアをコーディングします。たとえば、コードの問題に変換すると、以下は酔った注文システムのダモです。

したがって、これらの主要な順序付け機能を実現するには、次のようにします。

  • 毎日正午に食事を注文する効率を改善し、注文時間を20分から1分に最適化します。
  • どの支払い金額の範囲内でどのメニューを選択できるかを知ってください。
  • 一人当たりの支払い額を知っています。
  • WeChatに必要な支払いの合計額を把握します。
  • 注文のコピーがレストランのオーナーに送られました。
  • 現在の注文メニューにある特定の料理の名前と価格。

以前に学んだ知識ポイントを組み合わせて、djangoフレームワークのMTVのアイデアを使用して、簡単なhtmlページを開発し、同じローカルエリアネットワーク内の会社の同僚が操作して酔った誇り高いメニューを照会し、必要なデータを取得できるようにします。

第二に、関連する完全なプロセスの操作ステップ

1.最初のステップは、djangoプロジェクト[helloworld]のルートパス[helloworld /]の下に空のファイル[utils]を手動で追加し、自分で作成したpyスクリプトをファイル[utils]に配置することです。

詳細:

  • ツールはデフォルトで同じフォルダーAに配置され、フォルダーAはユーザーが手動で作成します。
  • フォルダAのデフォルト名はutilsで、デフォルトでプロジェクトのルートディレクトリに配置されます(ほとんどのフレームワークと業界関係者は、デフォルトで仕様に従います)。

2. 2番目のステップは、[zuideyiMenu.py]のクラス[Zuideyi]のコード関数を単体テストして、正常に実装できることを確認することです。

#!/usr/bin/python
# coding:utf-8

"""
@file: zuideyiMenu.py
@author: lucas
@createTime: 2021/3/2 10:37 上午
@Function: 定义一个醉得意点菜功能菜单类
"""

import random


class Zuideyi:

    def __init__(self, total, couponMoney, everyPlanMinimalMoney, everyPlanMaximalMoney):
        '''
        total: 就餐总人数
        couponMoney: 优惠券金额
        everyPlanMinimalMoney: 人均最低消费金额
        everyPlanMaximalMoney: 人均最高消费金额
        '''

        self.total = total
        self.couponMoney = couponMoney
        self.everyPlanMinimalMoney = everyPlanMinimalMoney
        self.everyPlanMaximalMoney = everyPlanMaximalMoney
        # 自助费= 5元/人 (该属性值暂时固定)
        self.supportFee = 5
        # 折扣率 = 实际充值金额/最终获得金额  (该属性值暂时固定)
        self.discountRate = 1000 / 1120
        # 菜品集合(该属性值暂时固定)
        self.menu = {
            "招牌菜": {"酱椒片片鱼": 68},
            "6大必点菜": {"鲜花椒牛肉粒": 58, "梅菜扣肉": 46, "香酥脆皮鸭": 32, "汽锅时蔬": 28, "台式三杯鸡": 42},
            "醉经典": {"得意醉排骨": 12, "肉末茄子": 23, "铁板黑椒牛肉": 48, "大碗有机花菜": 22, "肉沫蒸蛋": 18, },
            "下饭南方菜": {"农家烧笋干": 32, "海味紫菜煲": 32, "干锅千叶豆腐": 23, "红烧日本豆腐": 23, },
            "当季时蔬": {"油淋芥兰": 18, },
            "店长推荐": {"闽南醋肉": 36, },
        }

        # 大菜集合
        # self.bigDish = self.menus()[0]
        # 小菜集合
        # self.sideDish = self.menus()[1]

        # # 新增一个空dict,用于存储相关需要展示到前端页面的打印信息
        # self.printAll = {}

    def total_price_range_of_dishes(self):
        '''
        return: 可以点的菜品总价区间 = [人均最低消费金额*就餐总人数 / 折扣率 + 优惠券金额,
                                    人均最高消费金额*就餐总人数 / 折扣率 + 优惠券金额]
        '''
        total_price_range_of_dishes = [self.everyPlanMinimalMoney * self.total / self.discountRate + self.couponMoney,
                                       self.everyPlanMaximalMoney * self.total / self.discountRate + self.couponMoney]

        return total_price_range_of_dishes

    def menus(self):
        '''
        return: 返回一个整理过的菜品集合
        '''
        # 定义单价大于等于30元的菜品为【大菜】,定义单价小于30元的菜品为【小菜】
        # 大菜数据集合:bigDish,小菜数据集合:smallDish
        bigDish = []
        smallDish = []
        for key1, value1 in self.menu.items():
            for key2, value2 in value1.items():
                if value2 >= 30:
                    bigDish.append({key2: value2})
                else:
                    smallDish.append({key2: value2})
        dish = [bigDish, smallDish]
        print("大菜集合如下:")
        print(bigDish)
        print("小菜集合如下:")
        print(smallDish)
        return dish

    def amount_of_payment_of_everyOne(self, totalMoney):
        '''
        totalMoney: 醉得意点餐菜单总金额(不扣除相关优惠券金额,但包含就餐人数的总自助费)
        return: 实际人均需付餐费
        '''
        amount_of_payment_zuiDeyi = (totalMoney - self.couponMoney)
        print("今天醉得意公众号里的会员卡扣款金额:%s" % amount_of_payment_zuiDeyi)

        amount_of_payment_weChat = amount_of_payment_zuiDeyi * self.discountRate
        print("今天微信实际支付扣款金额:%s" % amount_of_payment_weChat)
        amount_of_payment_of_everyOne = amount_of_payment_weChat / self.total
        print("今天醉得意可以点的菜品总金额为%s元,点餐人数为:%s人,实际人均需付餐费:%s元" % (totalMoney, self.total, amount_of_payment_of_everyOne))

        result1 = {
            "今天醉得意公众号里的会员卡扣款金额:": amount_of_payment_zuiDeyi,
            "今天微信实际支付扣款金额:": amount_of_payment_weChat,
            "今天醉得意可以点的菜品总金额为:": totalMoney,
            "今天醉得意点餐人数为:": self.total,
            "今天醉得意实际人均需付餐费为:": amount_of_payment_of_everyOne
        }

        return result1

    def chioce(self, bigDishCounts, smallDishCounts):
        '''
        bigDishCounts: 大菜个数
        smallDishCounts: 小菜个数
        '''

        dish = self.menus()
        a1 = random.sample(dish[0], bigDishCounts)
        b1 = random.sample(dish[1], smallDishCounts)
        # 可以点的点餐菜品
        neededMenu = a1 + b1
        # 可以点的菜品总金额,初始值为0
        totalMoney = 0
        # 在微信里要写给醉得意老板的菜单
        stringA = ""
        for value3 in neededMenu:
            for key4, value4 in value3.items():
                # print(value4)
                totalMoney = totalMoney + value4
                stringA = stringA + key4 + ","
        stringA = stringA[:-1]
        # 总菜单金额 = 总菜品金额+ 总自助费
        totalMoney = totalMoney + self.supportFee * self.total

        # 判断totalMoney 是否在可消费总金额区间
        if self.total_price_range_of_dishes()[0] <= totalMoney <= self.total_price_range_of_dishes()[1]:

            result = {
                "今天醉得意可以点的点餐菜品:": neededMenu,
                "今天醉得意可以点的点餐菜品总数:": bigDishCounts + smallDishCounts,
                "今天可以微信里发给醉得意老板的点餐文案:--->老板,今天点餐人数%s,11:45可以陆续上菜,桌上饮料杯子不用放,餐桌号安排好了之后麻烦说一声。点菜菜单如下:" % self.total: stringA,
                "今天总消费金额区间:": self.total_price_range_of_dishes()
            }
            result1 = self.amount_of_payment_of_everyOne(totalMoney)
            print(
                "======================================================================================================================================")

            newResult = dict(result, **result1)
            return newResult

        else:
            return {"error:": "该场景下没有符合要求的醉得意菜单"}

    def run(self):
        # 当11个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;3个大菜,7个小菜;   4个大菜,3个小菜;4个大菜,4个小菜; 4个大菜,5个小菜;4个大菜,6个小菜;
        # 当10个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;3个大菜,7个小菜;   4个大菜,3个小菜;4个大菜,4个小菜; 4个大菜,5个小菜;4个大菜,6个小菜;
        # 当9个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;                      2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;2个大菜,7个小菜;
        # 当8个人:3个大菜,2个小菜;3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;    2个大菜,3个小菜;2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;
        # 当7个人:2个大菜,3个小菜;2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;
        # 当6个人:2个大菜,3个小菜;2个大菜,2个小菜;2个大菜,1个小菜;   1个大菜,5个小菜;1个大菜,4个小菜;1个大菜,3个小菜;1个大菜,2个小菜;1个大菜,1个小菜;
        if self.total == 10 or 11:
            a1 = self.chioce(bigDishCounts=3, smallDishCounts=3)
            a2 = self.chioce(bigDishCounts=3, smallDishCounts=4)
            a3 = self.chioce(bigDishCounts=3, smallDishCounts=5)
            a4 = self.chioce(bigDishCounts=3, smallDishCounts=6)
            a5 = self.chioce(bigDishCounts=3, smallDishCounts=7)
            a6 = self.chioce(bigDishCounts=4, smallDishCounts=3)
            a7 = self.chioce(bigDishCounts=4, smallDishCounts=4)
            a8 = self.chioce(bigDishCounts=4, smallDishCounts=5)
            a9 = self.chioce(bigDishCounts=4, smallDishCounts=6)
            b = [a1, a2, a3, a4, a5, a6, a7, a8, a9]
            return {"all": b}

        if self.total == 9:
            a1 = self.chioce(bigDishCounts=3, smallDishCounts=3)
            a2 = self.chioce(bigDishCounts=3, smallDishCounts=4)
            a3 = self.chioce(bigDishCounts=3, smallDishCounts=5)
            a4 = self.chioce(bigDishCounts=3, smallDishCounts=6)
            a5 = self.chioce(bigDishCounts=2, smallDishCounts=4)
            a6 = self.chioce(bigDishCounts=2, smallDishCounts=5)
            a7 = self.chioce(bigDishCounts=2, smallDishCounts=6)
            a8 = self.chioce(bigDishCounts=2, smallDishCounts=7)
            b = [a1, a2, a3, a4, a5, a6, a7, a8]
            return {"all": b}

        if self.total == 8:
            a1 = self.chioce(bigDishCounts=3, smallDishCounts=2)
            a2 = self.chioce(bigDishCounts=3, smallDishCounts=3)
            a3 = self.chioce(bigDishCounts=3, smallDishCounts=4)
            a4 = self.chioce(bigDishCounts=3, smallDishCounts=5)
            a5 = self.chioce(bigDishCounts=3, smallDishCounts=6)
            a6 = self.chioce(bigDishCounts=2, smallDishCounts=3)
            a7 = self.chioce(bigDishCounts=2, smallDishCounts=4)
            a8 = self.chioce(bigDishCounts=2, smallDishCounts=5)
            a9 = self.chioce(bigDishCounts=2, smallDishCounts=6)
            a10 = self.chioce(bigDishCounts=2, smallDishCounts=7)
            b = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
            return {"all": b}

        if self.total == 7:
            a1 = self.chioce(bigDishCounts=2, smallDishCounts=3)
            a2 = self.chioce(bigDishCounts=2, smallDishCounts=4)
            a3 = self.chioce(bigDishCounts=2, smallDishCounts=5)
            a4 = self.chioce(bigDishCounts=2, smallDishCounts=6)
            b = [a1, a2, a3, a4]
            return {"all": b}

        if self.total == 6:
            a1 = self.chioce(bigDishCounts=2, smallDishCounts=3)
            a2 = self.chioce(bigDishCounts=2, smallDishCounts=2)
            a3 = self.chioce(bigDishCounts=2, smallDishCounts=1)
            a4 = self.chioce(bigDishCounts=1, smallDishCounts=5)
            a5 = self.chioce(bigDishCounts=1, smallDishCounts=4)
            a6 = self.chioce(bigDishCounts=1, smallDishCounts=3)
            a7 = self.chioce(bigDishCounts=1, smallDishCounts=2)
            a8 = self.chioce(bigDishCounts=1, smallDishCounts=1)
            b = [a1, a2, a3, a4, a5, a6, a7, a8]
            return {"all": b}


if __name__ == "__main__":
    test = Zuideyi(9, 5, 19, 22.5)
    test.run()

3. 3番目のステップは、指定されたアプリケーション[hello]のディレクトリ[templates]に[search_form.html]を書き込むことです。これは、ユーザーが関連データを入力および送信できるようにするために使用されます。

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <title>醉得意点餐页面</title>

</head>

<body>

<form action="{% url 'search_interface' %}" method="get">

    请输入就餐的人数:<input type="text" name="total" placeholder="请输入就餐人数" value="7"/>

    <br>

    请输入优惠券金额:<input type="text" name="couponMoney" placeholder="请输入优惠券金额" value="5"/>

    <br>

    人均最低消费金额:<input type="text" name="everyPlanMinimalMoney" placeholder="人均最低消费金额" value="22"/>

    <br>

    人均最高消费金额:<input type="text" name="everyPlanMaximalMoney" placeholder="人均最高消费金额" value="30"/>

    <br>

    <br>

    <input type="submit" value="开始搜索相关合适的菜单信息"/>

</form>

</body>

</html>

詳細:

①。フォーム属性のアクション要素の値の割り当ての意味

./は現在のディレクトリを表し、?はクエリ文字列が空であることを表します 

action = "" //通常は空にすることができます。ここに二重引用符を
付ける必要があります。つまり、船荷証券が自分に送信されます(つまり、現在のページが処理されます)action = "a.php" //現在のディレクトリに送信されますa。php処理 


このディレクトリのデフォルトのホームページに送信します。action = "?"の場合は、自分に送信します。

②form属性でaction要素の値を割り当てる方法の公式説明については、この新人チュートリアルを参照してください。

③。アクション値の使用シナリオの分析

4、[書き込まれた] zuideyi_result.htmlの[テンプレート]内の指定されたアプリケーションディレクトリ[hello]の最初のステップ4は、関連データを誇らしげに飲んだメニューを表示するクエリです。

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>符合点餐要求的醉得意</title>

</head>

<body>

{#<h4> {
   
   {views_dict}}</h4>#}

{#<h2> {
   
   {views_dict.b}}</h2>#}

{#<br>==============<br>#}

{##}

{##}

{#<h4> {
   
   {views_dict.all}}</h4>#}

{#<br>==============<br>#}

{##}

{##}

<ul>

<h4> {% for thing in views_dict.all %}

        {% for i,j in thing.items %}

            {
   
   { i }}{
   
   { j }}<br>

            {% endfor %}

        <br>

      ========================================

        <br>

        <br>

      {% endfor %}



</h4>

</ul>

</body>

</html>

5. 5番目のステップは、指定されたアプリケーション[hello]の[views.py]にビュー関数/インターフェイス[search_form]を記述して、[search_form.html]を返すことです。

6. 6番目のステップは、指定されたアプリケーション[hello]の[views.py]にビュー関数/インターフェイス[search]を記述して、[zuideyi_result.html]を返すことです。

# 接收请求数据

from utils.zuideyiMenu import Zuideyi

def search(request):

    request.encoding = 'utf-8'

    # if 'total' in request.GET and request.GET['total']:

    #     message = '你搜索的内容为: ' + request.GET['total']

    # else:

    #     message = '你提交了空表单'



    total = request.GET['total']

    couponMoney = request.GET['couponMoney']

    everyPlanMinimalMoney = request.GET['everyPlanMinimalMoney']

    everyPlanMaximalMoney = request.GET['everyPlanMaximalMoney']



    # list = [total, couponMoney, everyPlanMinimalMoney, everyPlanMaximalMoney]



    total = int(total)

    couponMoney = int(couponMoney)

    everyPlanMinimalMoney = float(everyPlanMinimalMoney)

    everyPlanMaximalMoney = float(everyPlanMaximalMoney)



    zuideyi = Zuideyi(total=total, couponMoney=couponMoney, everyPlanMinimalMoney=everyPlanMinimalMoney,

                      everyPlanMaximalMoney=everyPlanMaximalMoney)

    run = zuideyi.run()

    views_dict = run

    return render(request, 'zuideyi_result.html', {"views_dict": views_dict})

7. 7番目のステップは、パス[helloworld / helloworld / urls.py/]のdjangoプロジェクト[helloworld]に2つの異なるURLマッチングルールを記述することです。

8、最初のステップ8はdjangoプロジェクト[helloworld]サービスを開始します。

9. 9番目のステップでは、任意のブラウザーでアドレス[http://127.0.0.1:8000/search-form/]またはアドレス[http:// PC IP:8000 / search-form /]を入力します。正常にアクセスされました[DrunkDeyi OrderingPage]という名前のhtmlページに移動します。

10. 10番目のステップで、9番目のステップで取得した[Drunk Deyi注文ページ]で[関連する適切なメニュー情報の検索を開始]ボタンをクリックすると、[注文に一致するDrunkDeyi]という名前のページに正常にアクセスできます。要件] htmlページ、htmlページには必要な関連コンテンツが表示されます。

 

おすすめ

転載: blog.csdn.net/LYX_WIN/article/details/114312646