Django - 从 url <pk>填充创建视图输入



我正在尝试自动填充我的创建视图的隐藏输入。

我的网址模式:

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>自动填充"身份验证"查菲尔德,如何?

您可以从FormMixin覆盖get_initial

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")}

最新更新