Chris Yin
Chris Yin

Reputation: 791

How to render previous page after jQuery post in Django

I'm building a site that connects topics to questions to answers. Each question is associated with a topic, each answer a question, and so on. On the home page, I click on a topic, and it takes me to a dynamic URL based on the topic_id.

url(r'^(?P<topic_id>\d+)/$', 'questions'),

When I add a question, the jQuery lightbox renders this URL in a pop up

url(r'^(?P<topic_id>\d+)/add_question/$', 'add_question'),

Once I submit the question, it goes through add_question.html

#add_question.html    
<div class="questionPopupForm">
    <h5><div class="questionPopupTitle"> Add Question</div></h5>
    <form action = "/home/{{ form.topic.id }}/add_question/" method = "post">{% csrf_token %}
        <p>{{ form.as_p }}</p>
        <p><div class="addQuestionButton"><input type = "submit" value = "Ask Question"     class="btn btn-success" /></div></p>
        <input type = "hidden" name = "next" value = "{{ next|escape }}" />
    </form>
</div>

Once I Submit, the Action then takes me to /home/{{ form.topic.id }}/add_question, which is the URL above, linking to the add_question view.

#views.py
def add_question(request, topic_id):
    if request.method == "POST":
        form = QuestionForm(request.POST, request.FILES)
        if form.is_valid():
            question = form.save(user = request.user)
            question.topic = Topic.objects.get(pk = topic_id)
            question.save()
            return HttpResponseRedirect("/home/")
    else:
        form = QuestionForm()
        form.q_author = request.user
        form.movie = Topic.objects.get(pk = topic_id)
    return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request))

Now right now, it takes me home with the return HttpResponeRedirect('/home/'). What I would like to do is redirect the user back to the same page, but there is a variable in the URL (/{{ topic.id }}/), but HttpResponseRedirect doesn't let me put a variable into it. How do I get back to the same page?

Upvotes: 1

Views: 608

Answers (3)

Burhan
Burhan

Reputation: 83

could you please try this?

#views.py
def add_question(request, topic_id):
    if request.method == "POST":
        form = QuestionForm(request.POST, request.FILES)
        if form.is_valid():
            question = form.save(user = request.user)
            question.topic = Topic.objects.get(pk = topic_id)
            question.save()
            url = "/home/%s"%(str(topic_id))
            return HttpResponseRedirect(url)
    else:
        form = QuestionForm()
        form.q_author = request.user
        form.movie = Topic.objects.get(pk = topic_id)
    return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request))

Upvotes: 1

augustomen
augustomen

Reputation: 9759

Yes, you can put a variable in the HttpResponseRedirect, but as a string, like this:

return HttpResponseRedirect("/%s/" % topic_id)

Also, try using the url alias using reverse:

return HttpResponseRedirect(reverse('questions', topic_id=topic_id))

Or the Django shortcut redirect():

from django.shortcuts import redirect
return redirect('questions', topic_id=topic_id)

Upvotes: 2

AlwaysBTryin
AlwaysBTryin

Reputation: 1964

HttpResponseRedirect requires a string (URL in this case), but you can construct it on the fly. For example:

return HttpResponseRedirect("/%s/" % topic.id)

Upvotes: 2

Related Questions