抢红包小程序 红包大战
# 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后端代码,包含了登录、创建红包、参与红包、查看红包详情、摇一摇获取红包、查看个人红包记录等功能。具体的前端代码需要根据实际需求进行开发。希望对你有帮助!文章来源:https://www.toymoban.com/news/detail-657156.html
本文由 mdnice 多平台发布文章来源地址https://www.toymoban.com/news/detail-657156.html
到了这里,关于抢红包小程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!