После проверки повторяющейся записи вызываемый URL, похоже, меняется и генерирует 404 - PullRequest
0 голосов
/ 01 октября 2019

Действительно странная ошибка:

Я хочу вставить запись участника для того, кто появляется на мероприятии. Первый код ниже работает нормально, но позволяет кому-то зарегистрироваться дважды. Второй код выполняет дополнительную проверку, чтобы идентифицировать дублирующуюся запись, и правильно сообщает, что, если дубликат не обнаружен, запись не добавляется и выдает ошибку 404 для пользователя.

Буквально единственным изменением является дополнительная проверка (закомментировано в первом коде). Кто-нибудь может пролить свет на это, пожалуйста?

Успешный код:

if form.validate_on_submit():

# Ensure the specified user is a valid user for this club - userref could be user number or name 
          so we must handle both
    usercheck = User.query.filter_by(usernum=form.userref.data,club=current_club).first()
    if usercheck is None:
        usercheck = User.query.filter_by(username=form.userref.data,club=current_club).first()
        if usercheck is None:
            flash(_('***** Attendee unknown to this club *****', category='error'))
            return redirect(
                url_for('main.attendee_register', requesting_user=current_user.username, 
                        clubnum=current_user.club))


    usernum = usercheck.usernum

    attendeeadd = Attendees(usernum=usernum, activitynumber=form.activitynum.data,
                        activitydate=form.activitydate.data, club=current_club)
    duplicatecheck = Attendees.query.filter_by(usernum = usernum, activitydate = 
             form.activitydate.data,activitynumber = form.activitynum.data).first_or_404()

#       if duplicatecheck is not None:
#           flash(_('***** Attendee already registered *****', category = 'error'))
#       else:
    print(current_user.username)
    print(current_user.club)
    attendeeadd = Attendees(usernum=usernum, activitynumber=form.activitynum.data,
                                        activitydate=form.activitydate.data, club=current_club)
    db.session.add(attendeeadd)
    db.session.commit()
    flash(_('Attendee Registered.', category="message"))

    return redirect(url_for('main.attendee_register', 
             requesting_user=current_user.username,clubnum=current_user.club))
elif request.method == 'GET':
    form.club = current_club
return render_template('activityattendees.html', title=_('Activity Attendees'),
                       form=form,club=club)

Неудачный код:

if form.validate_on_submit():

   # Ensure the specified user is a valid user for this club - userref could be user number or name 
               so we must handle both
    usercheck = User.query.filter_by(usernum=form.userref.data,club=current_club).first()
    if usercheck is None:
        usercheck = User.query.filter_by(username=form.userref.data,club=current_club).first()
        if usercheck is None:
            flash(_('***** Attendee unknown to this club *****', category='error'))
            return redirect(
                url_for('main.attendee_register', requesting_user=current_user.username, 
                         clubnum=current_user.club))


    usernum = usercheck.usernum

    attendeeadd = Attendees(usernum=usernum, activitynumber=form.activitynum.data,
                        activitydate=form.activitydate.data, club=current_club)
    duplicatecheck = Attendees.query.filter_by(usernum = usernum, activitydate = 
            form.activitydate.data,activitynumber = form.activitynum.data).first_or_404()

    if duplicatecheck is not None:
        flash(_('***** Attendee already registered *****', category = 'error'))
    else:
        print(current_user.username)
        print(current_user.club)
        attendeeadd = Attendees(usernum=usernum, activitynumber=form.activitynum.data,
                                        activitydate=form.activitydate.data, club=current_club)
        db.session.add(attendeeadd)
        db.session.commit()
        flash(_('Attendee Registered.', category="message"))

    return redirect(url_for('main.attendee_register', 
            requesting_user=current_user.username,clubnum=current_user.club))
elif request.method == 'GET':
    form.club = current_club
return render_template('activityattendees.html', title=_('Activity Attendees'),
                       form=form,club=club)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...