python - 使用 session 的Django View 中 “def”的语法错误

标签 python django session view syntax-error

它位于Django项目中的应用程序中,位于该应用程序的views文件中。该应用程序安装在settings.py中,但在调用 View 之前会引发错误。

我在CouponForm的“def”中的“f”处遇到错误,它说这是一个SyntaxError,但我无法解决问题。 urlconf仅引用manageCoupons函数。
在此先感谢您的帮助!

views.py:

from models import Coupon
from django.shortcuts import render
from django.http import HttpResponse

def CouponList(req):
    c = Coupon.objects.all()
    return render(req,'coupon_list.html',{'title':'Coupons','coupons':c}

def CouponForm(req1):
    if 'name' in req1.GET and 'desc' in req1.GET:
        name1 = req1.GET['cname']
        desc = req1.GET['desc']
        c = Coupon(name=name1, description=desc)
        c.save()
        success = True
    else:
        success = False
    return render(req1,'coupon.html',{'title':'New Coupon','success':success,})

def CouponDelete(req2):
    if 'name' in req2.GET:
        name1 = req2.GET['name']
        c = Coupon.objects.filter(name=name1)
        c.delete()
        success = True
    else:
        success = False
    return render(req2,'coupon_delete.html',{'title':'Delete Coupon','success':success}


def manageCoupons(request):
    if 'passwrd' in request.GET:
        psswrd = request.GET['passwrd']
        if psswrd == 'password0':
            return CouponList(request)
        elif psswrd == 'password1':
            request.session["function"] = 'cform'
            return CouponForm(request)
        elif psswrd == 'password2':
            request.session["function"] = 'cdelete'
            return CouponDelete(request)
    elif "function" in request.session:
        if request.session["function"] == 'cdelete':
            return CouponDelete(request)
        elif request.session["function"] == 'cform':
            return CouponForm(request)
    else:
        return HttpResponse("<a href='/'>Home</a>")

最佳答案

def CouponList(req):
    c = Coupon.objects.all()
    return render(req,'coupon_list.html',{'title':'Coupons','coupons':c})
                                                                        ^
                                                                        |
                                                              You are missing this bracket

关于python - 使用 session 的Django View 中 “def”的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151503/

相关文章:

asp.net - session 未清除

python - 如果我在 `pprint()` 上导入 `print()` 会爆炸什么?

python - 接口(interface)错误: Error binding parameter 0 - probably unsupported type when runing my django script

php - 将 URL 转换为屏幕截图(脚本)

php - 无法写入 session 数据(文件)

asp.net - 为什么iis中的 session 自动注销?

python - 为什么我收到错误 : "NameError: global name ' pupiluserinputbox' is not defined"

python - 什么是 [1 :] do in a recursive function

python - sklearn中有设置惩罚阈值的参数吗?

python - 未知命令 : shell_plus and --settings