我正在建立一个将主题与问题联系起来的网站。每个问题都与一个主题相关联,每个问题都与一个问题相关联,等等。在主页上,我单击一个主题,它会将我带到一个基于 topic_id 的动态 URL。
url(r'^(?P<topic_id>\d+)/$', 'questions'),
当我添加问题时,jQuery 灯箱会在弹出窗口中呈现此 URL
url(r'^(?P<topic_id>\d+)/add_question/$', 'add_question'),
一旦我提交了问题,它就会通过 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>
一旦我提交,Action 会将我带到 /home/{{ form.topic.id }}/add_question,这是上面的 URL,链接到 add_question 视图。
#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))
现在,它带我回家,返回 HttpResponeRedirect('/home/')。我想做的是将用户重定向回同一页面,但是 URL (/{{ topic.id }}/) 中有一个变量,但是 HttpResponseRedirect 不允许我将变量放入其中。我如何回到同一页面?