尝试在视图函数中访问外键的SyntaxError

时间:2017-04-29 16:30:13

标签: python django django-templates

该功能不接受外键。它正在获得语法错误。 这是函数的代码。 views.py

def projectoraccept(request, eventprojector_id):
    eventprojector = get_object_or_404(Eventprojector, pk=eventprojector_id)
    if request.method == 'POST':
        eventprojector.is_accept = eventprojector.is_accept+1
        eventprojector.save(update_fields=['is_accept'])

    if eventprojector.projector.sec.sec_name == 'CS_dept'
        eventprojector.projector.cs_no = eventprojector.projector.cs_no-1
        eventprojector.projector.save(update_fields=['sec'])
    elif eventprojector.projector.sec.sec_name is EC dept
        eventprojector.projector.ec_no = eventprojector.projector.ec_no-1
        eventprojector.projector.save(update_fields=['sec'])
    elif eventprojector.projector.sec.sec_name is EEE dept
        eventprojector.projector.eee_no = eventprojector.projector.eee_no-1
        eventprojector.projector.save(update_fields=['sec'])
    elif eventprojector.projector.sec.sec_name is IEEE
        eventprojector.projector.ieee_no = eventprojector.projector.ieee_no-1
        eventprojector.projector.save(update_fields=['sec'])
    elif eventprojector.projector.sec.sec_name is TCP dept
        eventprojector.projector.tcp_no = eventprojector.projector.tcp_no-1
        eventprojector.projector.save(update_fields=['sec'])
return render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })

错误是

if eventprojector.projector.sec_name == 'CS_dept' ^ SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:3)

在if条件之后,你没有冒号:字符;例如他们应该是:

if eventprojector.projector.sec.sec_name == 'CS_dept':

,等等。

错误说" SyntaxError"原因是:)

答案 1 :(得分:1)

你必须在每个if和elif之后添加一个冒号 例如:

if eventprojector.projector.sec.sec_name == 'CS_dept' :
    # your if condition code
elif eventprojector.projector.sec.sec_name is EC dept :
   # your elif condition code