Django 2.2模型模板布尔值更改

时间:2019-08-25 20:56:54

标签: django django-models django-templates django-views

我正在尝试创建一个非常简单的优惠券系统。您注册了一个电子邮件列表,并获得了一个数据库条目,其中包含优惠券标识,活动状态和电子邮件的值。我已经能够毫无问题地注册他们,但是我在思想上很难找到如何兑换优惠券的麻烦。我有一个页面/模板设置来兑换优惠券,只是不知道如何提交才能更改状态,以及如果已经使用过查询则发回查询。

另外,很抱歉,如果我只是缺少一些非常明显的东西,今天我感觉不是100%。

-- Template --

{% extends 'base_layout.html' %}

{% block content %}

<style type="text/css">
    input {
  background-color: white;
  color: black;
}
</style>
<div style="background-color: purple; opacity: 0.7; margin: 10px;">
<br />
<p style="padding: 10px; font-size: 25px;">
  Enter id<br />

</p>
<form method='POST' style="margin: 20px;" >
    {% csrf_token %}
    {{ form.as_p }}
    <br />
    <input type="submit" value="Submit" style="color: black;" />
</form>
<br />
</div>
{% endblock content %}






-- urls.py --

from django.contrib import admin
from django.urls import path
from . views import coupon_view, coupon_redeem



app_name = 'coupons'

urlpatterns = [
    path('', coupon_view, name = 'main'),
    path('redeem/', coupon_redeem, name='redeem'),
]






-- Models.py --

from django.db import models

# Create your models here.
class Coupon(models.Model):
    first = models.CharField(max_length = 80)
    last = models.CharField(max_length = 80, default="")
    identification = models.IntegerField(unique = True)
    active = models.BooleanField(default = True)
    email = models.EmailField(max_length=70, null=True, blank=True, unique=True)

    def __str__(self):
        return self.last




-- Views.py --

from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from .models import Coupon
from .forms import CouponForm, CouponApplyForm
# Create your views here.

def coupon_view(request):
    form = CouponForm(request.POST or None)
    if form.is_valid():
        form.save()
        form = CouponForm()
    context = {
        'form' : form
    }
    return render(request, 'coupons/coupon.html', context)

def coupon_redeem(request):
    form = CouponApplyForm(request.POST or None)
    if form.is_valid():
        identification = form.cleaned_data['identification']
        try:
            coupon = Coupon.objects.get(identification__iexact=identification,
                                    active=True)
        except Coupon.DoesNotExist:
            request.session['coupon_id'] = None
    # Return to non-homepage, invalid code'
    context = {
        'form':form
    }
    return render(request, 'coupons/redeem.html', context)

当前结果,提交匹配,状态不变。

0 个答案:

没有答案