Django视图:DoesNotExist不起作用

时间:2016-04-06 02:38:14

标签: django django-views

在下面的视图中,我阻止创建具有相同日期和时间的会议。但无论如何,这段代码会导致创建一个经常性的会议。

def new_meeting_board(request):
    if User.is_authenticated:
        username = request.user.username
    else:
        return HttpResponseRedirect('/login/board/')

    if request.method == 'POST':
        form = new_meetingForm(request.POST)

        now = datetime.datetime.now()

        if form.is_valid():
            clash = 1
            user = User.objects.get(username = username)

            try:
                meet_check = meeting.objects.get(date = form.cleaned_data['date'], time = form.cleaned_data['time'])
            except meeting.DoesNotExist:
                clash = 0

            if clash == 1:
                form = new_meetingForm()
                variables = RequestContext(request, {
                    'username': username,
                    'form': form,
                })
                return render_to_response('new_meeting_board.html', variables)


            else:
                mem = memo(
                    snd_username = user,
                    rcv_username = form.cleaned_data['reciever'],
                    subject = 'Meeting',
                    date = str(now.year) + '-' + str(now.month) + '-' + str(now.day),
                    time = str(now.hour) + ':' + str(now.minute),
                )
                mem.save()

                mee = mem.meeting_set.create(
                    snd_username = username,
                    rcv_username = mem.rcv_username,
                    status_username = '0',
                    date = form.cleaned_data['date'],
                    time = form.cleaned_data['time'],
                    venue = form.cleaned_data['venue'],
                )
                mee.save()
                return HttpResponseRedirect('/dashboard/board/' + username)
    else:
        form = new_meetingForm()


    variables = RequestContext(request, {
        'username': username,
        'form': form,
    })
    return render_to_response('new_meeting_board.html', variables)

try / except块的except部分由于某种原因似乎不起作用。请帮忙。

2 个答案:

答案 0 :(得分:1)

meeting.objects应该是Meeting.objects

答案 1 :(得分:1)

您可以在代码中进行多次重新格式化和优化。您可以使用get()来优化代码,而不是使用get_or_create。您可以编写如下代码:

def new_meeting_board(request):
    if not request.user.is_authenticated():  # there is a mistake in your code, its not User.is_authenticated.
        return HttpResponseRedirect('/login/board/')

    if request.method == 'POST':
        form = new_meetingForm(request.POST)
        now = datetime.datetime.now()

        if form.is_valid():
            user = request.user

            meet_obj, meet_check = meeting.objects.get_or_create(
                     date = form.cleaned_data['date'],
                     time = form.cleaned_data['time'],
                     subject = 'Meeting',
                     snd_username=user, 
                     rcv_username = form.cleaned_data['reciever']
                  )

            if meet_check is True:
                form = new_meetingForm()
                variables = RequestContext(request, {
                    'form': form,
                })
                return render_to_response('new_meeting_board.html', variables)

            else:
                mee = meet_obj.meeting_set.create(
                    snd_username = username,
                    rcv_username = meet_obj.rcv_username,
                    status_username = '0',
                    date = form.cleaned_data['date'],
                    time = form.cleaned_data['time'],
                    venue = form.cleaned_data['venue'],
                )
                mee.save()
                return HttpResponseRedirect('/dashboard/board/' + username)
    else:
        form = new_meetingForm()


    variables = RequestContext(request, {
        'form': form,   # no need to send username, You can access it in the template by putting {{ request.user.username }}
    })
    return render_to_response('new_meeting_board.html', variables)