Django form redirect

2019-07-29 08:26发布

I have a form that and after the user fills in this form, I want to redirect to a URL that looks something like this

r'^user/(?P<id>\d+)/$'

The <id> is the primary key in the database...how do I go about this coz I'm stuck at this point

2条回答
成全新的幸福
2楼-- · 2019-07-29 08:37

Ok, so in your .html file, you should have an action= in your form, which sort of looks like that regular expression. At the very least it should look something similar to:

action="/user/{{ object.id }}/"

object.id would be replaced with whatever object you passed to the html file in your view.

查看更多
Summer. ? 凉城
3楼-- · 2019-07-29 08:48

The url template tag allows you to save some repetition here. It also prevents you from breaking the form if you change urls.py. If your urlpattern looks like this:

(r'^user/(?P<id>\d+)/$', 'user.views.detail')

Then you could use the template tag like this:

<form action="{% url user.views.detail object.id %}">
查看更多
登录 后发表回答