我正在尝试自动填充我的 Createview 的隐藏输入。
我的网址格式:
url(r'^user/add/(?P<pk>[0-9]+)/$',', UserCreateView.as_view(), name='user-create'),
我的观点:
class UserCreateView(CreateView):
model = UserMeta
fields = [
'auth', # This is the hidden input that I want to autofill
'email',
'city',
]
template_name = 'forms/user_create.html'
问题
我想用 <pk> 自动填充 'auth' 字符字段在我的网址中,如何?
请您参考如下方法:
您可以覆盖 get_initial来自 FormMixin :
class UserCreateView(CreateView):
model = UserMeta
fields = [
'auth', # This is the hidden input that I want to autofill
'email',
'city',
]
template_name = 'forms/user_create.html'
def get_initial(self):
return {"auth": self.kwargs.get("pk")}
