创建对象后重新定向,Django [英] Redirection after creating an object, Django
问题描述
我是Django的新手,并且正在创建一个运动应用。
我允许用户创建一个匹配项(选择他们的团队和风格),并希望在使用表单创建匹配项后将其重定向到特定页面(匹配项之一)。
I'm new with Django and I'm creating a sport app. I allow users to create a match (choosing their team and their style), and I want to redirect them to a specific page (the one of their match) after they create it with a form.
这是我的表单:
{% if user.is_authenticated %}
<form method="post">
{% csrf_token %}
<input type="radio" name="supporting" value={{teamA.id}} /> {{teamA}}
<input type="radio" name="supporting" value="" /> Neutre
<input type="radio" name="supporting" value={{teamB.id}} /> {{teamB}}
<hr>
<input type="radio" name="style" value="1" /> Funny
<input type="radio" name="style" value="3" /> Mixed
<input type="radio" name="style" value="2" /> Technical
</br></br>
<input type="submit" value="Create your match!" />
</form>
{% else %}
我能够重定向用户当他选择已经创建的匹配项时。但是我不知道如何使用用于创建匹配项的相同按钮重定向到特定页面。
I'm able to redirect a user when he chooses a match that is already created. But I don't know how to redirect to a specific page with the same button that is used to create the match.
感谢您的帮助
推荐答案
在您看来,在处理表单之后,可以使用 HttpResponseRedirect()
来设置适当的网址重定向到新页面。请参阅文档:使用视图中的表单
In your view, using HttpResponseRedirect()
to appropriate url after processing a form you can redirect to new page. Refer doc: Using a form in a view
更新:重定向到ID为id的匹配页面
Update : to redirect to match page with id
如果您的比赛详细信息页面的网址是这样的:
If your url for match detail page is something like this:
url(r'^match_detail/(?P<id>\d+)/$', 'myapp.views.match_detail', name='un_mdetail'),
然后您可以使用此重定向
Then you can redirect using this
HttpResponseRedirect(reverse('un_mdetail', args=(match.id,)))
这篇关于创建对象后重新定向,Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!