@login_required
def question(request, cpk, qpk):
    context = {}
    template = 'contest/question.html'

    context['contest'] = get_object_or_404(models.Contest, pk=cpk)
    context['question'] = get_object_or_404(models.Question, pk=qpk)

    if not context['contest'].live:
        return redirect('contest', cpk=context['contest'].pk)

    profile_user = models.Profile.objects.filter(user=request.user)
    try:
        profile = profile_user.filter(contest=context['contest'])[0]
    except __HOLE__:
        return redirect('not_registered')
    last_attempt = profile.profile_attempt.filter(question=context['question']).order_by('-stamp').first()
    context['last_attempt'] = last_attempt

    if request.method == 'GET':
        context['answer_form'] = AttemptForm(instance=last_attempt) if last_attempt else AttemptForm()
    elif request.method == 'POST':
        form = AttemptForm(request.POST, request.FILES)
        if form.is_valid():
            attempt = form.save(commit=False)
            attempt.profile = profile
            attempt.question = context['question']
            attempt.save()
            return redirect('question', cpk=cpk, qpk=qpk)
        else:
            context['answer_form'] = form
    return render(request, template, context)
dataset/ETHPy150Open theSage21/openJudge/openjudge/contest/views.py/question