0

I made a discount system in django that gets a code from user and if it was valid,it will save it as a session in user browser.

here is models.py :

class Discount(models.Model):
    code = models.CharField(max_length=20)
    discount = models.IntegerField(null=True , blank=True)
    valid_from = models.DateTimeField()
    valid_to = models.DateTimeField()
    active = models.BooleanField(default=False)

and there is its view i'm using:

def Coupon_view(request):
    if request.method == 'POST':
        form = CouponForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            try:
                cop = Discount.objects.get(code__iexact=data['code'] ,  valid_from__lte = now() , valid_to__gte = now() , active=True)
                request.session["coupon"] = cop.id
            except:
                messages.error(request , "code is not exist")
    try:
        url = request.META.get('HTTP_REFERER')
        return redirect(url)
    except:
        return redirect("Order:Checkout_page")

here is the CouponForm:

class CouponForm(forms.Form):
    code = forms.CharField(max_length=30)

at the end , there is the form that i am using :

    <form action="{% url 'Order:Coupon_page' %}" method="post">
        {% csrf_token %}
        <input type="text" name="code" placeholder="code">
        <button type="submit">submit</button>
    </form>

but the problem is it's showing me "code is not exist" error (even if i create the object that matches) ! where is the problem?

there is the query :

[{'sql': 'SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."expire_date" > '2023-06-20T12:55:33.995695+00:00'::timestamptz AND "django_session"."session_key" = '4vdgnlf4z6a6i8wag1viv8xj787f7uq6') LIMIT 21', 'time': '0.001'}, {'sql': 'SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1 LIMIT 21', 'time': '0.000'}, {'sql': 'SELECT "Order_discount"."id", "Order_discount"."created", "Order_discount"."code", "Order_discount"."discount", "Order_discount"."valid_from", "Order_discount"."valid_to", "Order_discount"."used", "Order_discount"."active" FROM "Order_discount" WHERE ("Order_discount"."active" AND UPPER("Order_discount"."code"::text) = UPPER('abc') AND "Order_discount"."valid_from" <= '2023-06-20T12:55:34.009076+00:00'::timestamptz AND "Order_discount"."valid_to" >= '2023-06-20T12:55:34.009079+00:00'::timestamptz) LIMIT 21', 'time': '0.000'}]

  • Try to print the query is preparing while you check for discount coupon https://stackoverflow.com/a/1074224/1079086 – Akash senta Jun 20 '23 at 12:37
  • It's bad to have that broad an except clause because it hides the actual error. Can you remove that and have the error displayed? Also, have you inspected `data`? – dacx Jun 20 '23 at 12:39
  • here is the query: – Alireza javanpour Jun 20 '23 at 12:56
  • and i delete the try and except parts but it still does not showing me any error! – Alireza javanpour Jun 20 '23 at 12:58
  • Maybe change `now()` to `timezone.now()` or `datetime.datetime.now()`, importing the appropriate libraries: https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#naive-and-aware-datetime-objects – raphael Jun 20 '23 at 19:38

0 Answers0