0
@app.route('/quiz/<quiz_id>/<question_id>', methods=['GET', 'POST'])
@login_required
def quiz(quiz_id, question_id): 
    quiz = Quiz.query.filter_by(id=quiz_id).first()
    qa = Question.query.filter_by(quiz_id=quiz_id).filter_by(id=question_id).first()
    length = Question.query.filter_by(quiz_id=quiz_id).count()

    if request.method =='POST':
        test = request.form[str(question_id)]
        ufq=UserFilledQuiz(user_id=current_user.id, quiz_id=quiz_id, question_id = question_id, \
        answer = request.form[str(question_id)])
        db.session.add(ufq)
        db.session.commit()
        return render_template(url_for('quiz', quiz_id=1, question_id=1), title='Quiz', quiz=quiz, qa=qa, length=length)

    return render_template('quiz.html', title='Quiz', quiz=quiz, qa=qa, length=length)

Hi, i want to use one view to make quiz in which one site is one question and answers. If i write links by hand for example quiz/1/1 or quiz/1/2 - it works. If i submit form with answer i recieve message: jinja2.exceptions.TemplateNotFound: /quiz/1/1 When i press enter in url bar it works.

1 Answer 1

2

On the line return render_template(url_for('quiz', quiz_id=1, question_id=1), title='Quiz', quiz=quiz, qa=qa, length=length), the call to url_for should be replaced with the name of the actual template, much like 'quiz.html' on the line below..


For a further diagnosis of the issue you're having here:

When you enter the URL manually, you're sending a GET request and hitting the lower render_template which renders a template called quiz.html (which presumably exists, since there is no error). So far so good.

On making a POST request by submitting a form, you're hitting the upper render_template, which has url_for in its first parameter. Since you're accessing the page /quiz/1/1, this is equivalent to writing '/quiz/1/1' as the name of the template in the render_template call. It seems obvious that this is incorrect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.