抢红包小程序 红包大战
# 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//' , views.join_red_packet, name='join_red_packet'),
path('red_packet_detail//' , views.red_packet_detail, name='red_packet_detail'),
path('shake_red_packet//' , 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 %}
Create Red Packet
{% endblock %}
# templates/join_red_packet.html
{% extends 'base.html' %}
{% block content %}
Join Red Packet
Red Packet Details:
Amount: {{ red_packet.amount }}
Quantity: {{ red_packet.quantity }}
Rules: {{ red_packet.rules }}
Start Time: {{ red_packet.start_time }}
End Time: {{ red_packet.end_time }}
{% endblock %}
# templates/red_packet_detail.html
{% extends 'base.html' %}
{% block content %}
Red Packet Detail
Red Packet Details:
Amount: {{ red_packet.amount }}
Quantity: {{ red_packet.quantity }}
Rules: {{ red_packet.rules }}
Start Time: {{ red_packet.start_time }}
End Time: {{ red_packet.end_time }}
{% if red_packet_activity.amount_received %}
Amount Received: {{ red_packet_activity.amount_received }}
Received Time: {{ red_packet_activity.received_time }}
{% else %}
{% endif %}
{% endblock %}
# templates/shake_red_packet.html
{% extends 'base.html' %}
{% block content %}
Shake Red Packet
Red Packet Details:
Amount: {{ red_packet_activity.red_packet.amount }}
Quantity: {{ red_packet_activity.red_packet.quantity }}
Rules: {{ red_packet_activity.red_packet.rules }}
Start Time: {{ red_packet_activity.red_packet.start_time }}
End Time: {{ red_packet_activity.red_packet.end_time }}
{% endblock %}
# templates/my_red_packets.html
{% extends 'base.html' %}
{% block content %}
My Red Packets
{% for red_packet in red_packets %}
Red Packet Details:
Amount: {{ red_packet.amount }}
Quantity: {{ red_packet.quantity }}
Rules: {{ red_packet.rules }}
Start Time: {{ red_packet.start_time }}
End Time: {{ red_packet.end_time }}
{% endfor %}
{% endblock %}
以上是一个简单的红包抢夺小程序的Django后端代码,包含了登录、创建红包、参与红包、查看红包详情、摇一摇获取红包、查看个人红包记录等功能。具体的前端代码需要根据实际需求进行开发。希望对你有帮助!
本文由 mdnice 多平台发布