Cli*_*sel 2 django django-users
我正在使用Userena,我正在尝试捕获URL参数并将它们添加到我的表单中,但我失去了如何做到这一点.
我想在模板中做的是:
<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/>
Run Code Online (Sandbox Code Playgroud)
然后在我的urls.py中
url(r'^accounts/signup/(?P<planslug>.*)/$','userena.views.signup',{'signup_form':SignupFormExtra}),
Run Code Online (Sandbox Code Playgroud)
然后,理想情况下,我想在forms.py中使用该planslug在配置文件中设置用户计划.
我迷失了如何将捕获的URL参数放入自定义表单中.我可以使用extra_context,是否必须覆盖Userena注册视图?
如果使用基于类的视图,则可以覆盖FormMixin类的def get_form_kwargs()方法.在这里,您可以将所需的任何参数传递给表单类.
在urls.py中:
url(r'^create/something/(?P<foo>.*)/$', MyCreateView.as_view(), name='my_create_view'),
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :-)
| 归档时间: |
|
| 查看次数: |
5844 次 |
| 最近记录: |