首页 > 编程语言 >抢红包小程序

抢红包小程序

时间:2023-08-18 16:46:16浏览次数:40  
标签:models 程序 抢红包 activity request packet time red

抢红包小程序 红包大战

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('login/', views.login, name='login'),
    path('create_red_packet/', views.create_red_packet, name='create_red_packet'),
    path('join_red_packet/<int:red_packet_id>/', views.join_red_packet, name='join_red_packet'),
    path('red_packet_detail/<int:red_packet_id>/', views.red_packet_detail, name='red_packet_detail'),
    path('shake_red_packet/<int:red_packet_id>/', views.shake_red_packet, name='shake_red_packet'),
    path('my_red_packets/', views.my_red_packets, name='my_red_packets'),
]

# models.py
from django.db import models
from django.contrib.auth.models import User

class RedPacket(models.Model):
    creator = models.ForeignKey(User, on_delete=models.CASCADE)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.IntegerField()
    rules = models.TextField()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    created_time = models.DateTimeField(auto_now_add=True)

class RedPacketActivity(models.Model):
    red_packet = models.ForeignKey(RedPacket, on_delete=models.CASCADE)
    participant = models.ForeignKey(User, on_delete=models.CASCADE)
    amount_received = models.DecimalField(max_digits=10, decimal_places=2, null=True)
    received_time = models.DateTimeField(null=True)

# views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import RedPacket, RedPacketActivity

@login_required
def create_red_packet(request):
    if request.method == 'POST':
        amount = request.POST['amount']
        quantity = request.POST['quantity']
        rules = request.POST['rules']
        start_time = request.POST['start_time']
        end_time = request.POST['end_time']
        red_packet = RedPacket.objects.create(
            creator=request.user,
            amount=amount,
            quantity=quantity,
            rules=rules,
            start_time=start_time,
            end_time=end_time
        )
        return redirect('red_packet_detail', red_packet.id)
    return render(request, 'create_red_packet.html')

@login_required
def join_red_packet(request, red_packet_id):
    red_packet = RedPacket.objects.get(id=red_packet_id)
    if request.method == 'POST':
        red_packet_activity = RedPacketActivity.objects.create(
            red_packet=red_packet,
            participant=request.user
        )
        return redirect('shake_red_packet', red_packet_activity.id)
    return render(request, 'join_red_packet.html', {'red_packet': red_packet})

@login_required
def red_packet_detail(request, red_packet_id):
    red_packet = RedPacket.objects.get(id=red_packet_id)
    return render(request, 'red_packet_detail.html', {'red_packet': red_packet})

@login_required
def shake_red_packet(request, red_packet_activity_id):
    red_packet_activity = RedPacketActivity.objects.get(id=red_packet_activity_id)
    if request.method == 'POST':
        # generate random amount for the user
        red_packet_activity.amount_received = random_amount
        red_packet_activity.received_time = timezone.now()
        red_packet_activity.save()
        return redirect('red_packet_detail', red_packet_activity.red_packet.id)
    return render(request, 'shake_red_packet.html', {'red_packet_activity': red_packet_activity})

@login_required
def my_red_packets(request):
    red_packets = RedPacket.objects.filter(creator=request.user)
    return render(request, 'my_red_packets.html', {'red_packets': red_packets})

# templates/create_red_packet.html
{% extends 'base.html' %}

{% block content %}
<h1>Create Red Packet</h1>
<form method="POST" action="{% url 'create_red_packet' %}">
    {% csrf_token %}
    <label for="amount">Amount:</label>
    <input type="text" name="amount" id="amount">
    <br>
    <label for="quantity">Quantity:</label>
    <input type="number" name="quantity" id="quantity">
    <br>
    <label for="rules">Rules:</label>
    <textarea name="rules" id="rules"></textarea>
    <br>
    <label for="start_time">Start Time:</label>
    <input type="datetime-local" name="start_time" id="start_time">
    <br>
    <label for="end_time">End Time:</label>
    <input type="datetime-local" name="end_time" id="end_time">
    <br>
    <button type="submit">Create</button>
</form>
{% endblock %}

# templates/join_red_packet.html
{% extends 'base.html' %}

{% block content %}
<h1>Join Red Packet</h1>
<h2>Red Packet Details:</h2>
<p>Amount: {{ red_packet.amount }}</p>
<p>Quantity: {{ red_packet.quantity }}</p>
<p>Rules: {{ red_packet.rules }}</p>
<p>Start Time: {{ red_packet.start_time }}</p>
<p>End Time: {{ red_packet.end_time }}</p>
<form method="POST" action="{% url 'join_red_packet' red_packet.id %}">
    {% csrf_token %}
    <button type="submit">Join</button>
</form>
{% endblock %}

# templates/red_packet_detail.html
{% extends 'base.html' %}

{% block content %}
<h1>Red Packet Detail</h1>
<h2>Red Packet Details:</h2>
<p>Amount: {{ red_packet.amount }}</p>
<p>Quantity: {{ red_packet.quantity }}</p>
<p>Rules: {{ red_packet.rules }}</p>
<p>Start Time: {{ red_packet.start_time }}</p>
<p>End Time: {{ red_packet.end_time }}</p>
{% if red_packet_activity.amount_received %}
    <p>Amount Received: {{ red_packet_activity.amount_received }}</p>
    <p>Received Time: {{ red_packet_activity.received_time }}</p>
{% else %}
    <form method="POST" action="{% url 'shake_red_packet' red_packet_activity.id %}">
        {% csrf_token %}
        <button type="submit">Shake Red Packet</button>
    </form>
{% endif %}
{% endblock %}

# templates/shake_red_packet.html
{% extends 'base.html' %}

{% block content %}
<h1>Shake Red Packet</h1>
<h2>Red Packet Details:</h2>
<p>Amount: {{ red_packet_activity.red_packet.amount }}</p>
<p>Quantity: {{ red_packet_activity.red_packet.quantity }}</p>
<p>Rules: {{ red_packet_activity.red_packet.rules }}</p>
<p>Start Time: {{ red_packet_activity.red_packet.start_time }}</p>
<p>End Time: {{ red_packet_activity.red_packet.end_time }}</p>
<form method="POST" action="{% url 'shake_red_packet' red_packet_activity.id %}">
    {% csrf_token %}
    <button type="submit">Open Red Packet</button>
</form>
{% endblock %}

# templates/my_red_packets.html
{% extends 'base.html' %}

{% block content %}
<h1>My Red Packets</h1>
{% for red_packet in red_packets %}
    <h2>Red Packet Details:</h2>
    <p>Amount: {{ red_packet.amount }}</p>
    <p>Quantity: {{ red_packet.quantity }}</p>
    <p>Rules: {{ red_packet.rules }}</p>
    <p>Start Time: {{ red_packet.start_time }}</p>
    <p>End Time: {{ red_packet.end_time }}</p>
{% endfor %}
{% endblock %}

以上是一个简单的红包抢夺小程序的Django后端代码,包含了登录、创建红包、参与红包、查看红包详情、摇一摇获取红包、查看个人红包记录等功能。具体的前端代码需要根据实际需求进行开发。希望对你有帮助!

本文由mdnice多平台发布

标签:models,程序,抢红包,activity,request,packet,time,red
From: https://www.cnblogs.com/wqmxh/p/17640929.html

相关文章

  • 陪诊小程序|服务到家就医陪伴更不可少
    随着科技的不断发展和医疗服务的日益完善,陪诊小程序成为了当下为炙手可热的医疗软件之一。无论是患者还是医护人员,陪诊小程序都为他们提供了全新的就医体验和便捷的服务。下面让我们一同探索一下陪诊小程序的软件开发功能。 一、陪诊小程序功能: 1、陪诊人员管理:陪诊小程序可以对......
  • C++快速入门 第二讲:从一个小程序说起
    cout(cout<<i表示变量i流向屏幕显示)是一个输出流对象,属于basic_ostream类的对象。ostream类在iostream头文件中定义。同理cin(回车后,键盘输入缓冲区内容流向cin流的内部缓冲区,cin>>xx操作便从这个缓冲区提取数据,即键盘输入流向程序)为输入流对象,C++标准库所使用的所有标识符(即类......
  • C#程序变量统一管理例子 - 开源研究系列文章
            今天讲讲关于C#应用程序中使用到的变量的统一管理的代码例子。        我们知道,在C#里使用变量,除了private私有变量外,程序中使用到的公共变量就需要进行统一的存放和管理。这里笔者使用到的公共变量管理库划分为:1)窗体;2)路径;3)对象;所以笔者对这几个库进行......
  • 如何运行一个Go程序
    学习自:golang如何运行-Golang-PHP中文网 1、Go程序内容packagemainimport"fmt"funcmain(){fmt.Println("Hello,world");}保存为hello.go2、在cmd中进入hello.go所在目录下cdC:\Users\Le\Desktop\go3、运行gorunxxx.gogorunhello.go ......
  • 解决小程序报错 getLocation:fail the api need to be declared in the requiredPriva
    一、unipp项目打开uniapp项目的配置文件manifest.json,选择“源码视图”。/*小程序特有相关*/"mp-weixin":{"appid":"你的开发者id","setting":{"urlCheck":true,"es6":true,"postcss":......
  • 10.4K Star!程序员为程序员针对性优化的开源免费笔记
    平时我一直用Notion来记录内容为主,但也一直关注着其他开源产品。上周正好看到一款非常受欢迎的开源免费笔记,今天就推荐给大家:VNote。VNote一个由程序员为程序员打造的开源笔记应用,基于Qt开发,专注于使用Markdown来写作的群体。它提供完美的编辑体验和强大的笔记管理功能,使得使......
  • 程序员必知的Android 代码规范
    前言虽然我们项目的代码时间并不长,也没经过太多人手,但代码的规范性依然堪忧,目前存在较多的比较自由的「代码规范」,这非常不利于项目的维护,代码可读性也不够高,此外,客户端和后端的研发模式也完全不同,后端研发基本都是基于SOA思想的,通常一个子系统3个人一起维护就已经是很充分的人......
  • Python 矢量数据库和矢量索引:构建 LLM 应用程序
    推荐:使用NSDT场景编辑器助你快速搭建可二次编辑的3D应用场景由于使用其硬件创建的生成式AI应用程序,Nvidia经历了显着的增长。另一项软件创新,矢量数据库,也正在乘着生成式人工智能的浪潮。开发人员正在向量数据库上用Python构建AI驱动的应用程序。通过将数据编码为向量,他们可以利......
  • php发送小程序订阅消息
    微信文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html#%E8%AE%A2%E9%98%85%E6%B6%88%E6%81%AF%E8%AF%AD%E9%9F%B3%E6%8F%90%E9%86%921.小程序后台创建消息模板 2.小程序用户点击按钮订阅dingyue(){//console.log('订阅......
  • 构建LLM应用程序时需要了解的5件事
    推荐:使用NSDT场景编辑器助你快速搭建可二次编辑的3D应用场景1.幻觉使用LLM时应注意的主要方面之一是幻觉。在LLM的背景下,幻觉是指产生不真实的,不正确的,无意义的信息。LLM非常有创意,它们可以用于不同的领域,但仍然存在的一个非常关键的未解决问题是它们的幻觉。由于LLM不是搜索引......