捕获的URL参数形式 [英] Captured URL parameters in form
问题描述
我正在使用Userena,并且试图捕获URL参数并将其保存到表单中,但是我不知道该怎么做。
I am using Userena and I am trying to capture URL parameters and get them to my form but I'm lost how to do this.
我想要什么在我的模板中要做的事情是:
What I would like to do in my template is:
<a href="/accounts/signup/freeplan">Free Plan</a><br/>
<a href="/accounts/signup/proplan">Pro Plan</a><br/>
<a href="/accounts/signup/enterpriseplan">Enterprise Plan</a><br/>
然后在我的urls.py
And then in my urls.py
url(r'^accounts/signup/(?P<planslug>.*)/$','userena.views.signup',{'signup_form':SignupFormExtra}),
然后,理想情况下,我想在forms.py中使用该planslug来设置用户计划在配置文件中。
Then, ideally, I'd like to use that planslug in my forms.py to set the user plan in the profile.
我不知道如何将捕获的URL参数转换为自定义表单。我可以使用extra_context吗,是否必须覆盖Userena注册视图?
I'm lost how to get the captured URL parameter into the custom form. Can I use the extra_context, do I have to override the Userena signup view?
推荐答案
如果使用基于类的视图,则可以覆盖FormMixin类的def get_form_kwargs()方法。在这里,您可以将所需的任何参数传递给表单类。
If you use class based views, you can overwrite the def get_form_kwargs() method of the FormMixin class. Here you can pass any parameters you need to your form class.
在urls.py中:
url(r'^create/something/(?P<foo>.*)/$', MyCreateView.as_view(), name='my_create_view'),
in views.py:
in views.py:
class MyCreateView(CreateView):
form_class = MyForm
model = MyModel
def get_form_kwargs(self):
kwargs = super( MyCreateView, self).get_form_kwargs()
# update the kwargs for the form init method with yours
kwargs.update(self.kwargs) # self.kwargs contains all url conf params
return kwargs
informs.py:
in forms.py:
class MyForm(forms.ModelForm):
def __init__(self, foo=None, *args, **kwargs)
# we explicit define the foo keyword argument, cause otherwise kwargs will
# contain it and passes it on to the super class, who fails cause it's not
# aware of a foo keyword argument.
super(MyForm, self).__init__(*args, **kwargs)
print foo # prints the value of the foo url conf param
希望这会有所帮助:-)
hope this helps :-)
这篇关于捕获的URL参数形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!