Wednesday, May 27, 2020

Re: Redirect to login page without losing content of form

Ok. Now I've dealt with this returned None problem, but I still have no idea what to do to keep my form content after clicking submit button. Does next parmeter generated by login_required contain only link or also form content?

W dniu środa, 27 maja 2020 15:58:22 UTC+2 użytkownik Jan Gregorczyk napisał:
I want answer to be added if user is logged in otherwise redirect user to login page, let him login and then add his answer. My problem is that I lose content of the form.
At the moment I'm getting this errror:
The view questions.views.answer didn't return an HttpResponse object. It returned None instead.


def question(request, question_id):
question = get_object_or_404(Question, pk=question_id)
form = AnswerForm()
return render(request, 'questions/question.html', {'question': question, 'form' : form},)

@login_required
def answer(request, question_id):
question = get_object_or_404(Question, pk=question_id)
form = AnswerForm(request.POST) if request.method == 'POST' else AnswerForm()
if form.is_valid():
answer = form.save(commit=False)
answer.author = request.user
answer.question = question
answer.save()
return HttpResponseRedirect(reverse('question', args=(question.id,)))



<form method="POST" action="{% url 'answer' question.id %}">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Odpowiedz">
</form>

form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="hidden" name="next" value="{{request.GET.next}}" />
<input type="submit" value="login" />
</form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cd4abaec-face-4f8f-8a69-72258fe611b1%40googlegroups.com.

No comments:

Post a Comment