我正在尝试自动填充我的 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")}
关于python - Django - 从 url <pk> 填充 createview 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574803/