[Answered ]-Django Views: DoesNotExist isn't working

1👍

meeting.objects should probably be Meeting.objects

👤Paul

1👍

There are multiple reformatting and optimization can be done in your code. Rather than using get(), you can use get_or_create to optimize code. You can write the code like this:

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)
👤ruddra

Leave a comment