python - Django - 从 url <pk> 填充 createview 输入

标签 python django

我正在尝试自动填充我的 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/

相关文章:

python - 如何在redis缓存中使用django-redis hset操作

django - 在Django中,为什么模型中的blank=True没有使表单字段成为非强制字段?

python - Django 与 Heroku 上的 PostgreSQL 应用程序不同步

python - 为什么Fabric通常会输出一个额外的空行?

python - 如何将图像添加到列表中

python - django 模板中不显示表单字段,仅显示提交按钮

python - 在 Scrapy 所需的 Python 2.7 上安装 Zope Interface 4.0.3 时出错

python - 训练后如何保存/恢复模型?

部署在 Ubuntu/Apache 服务器上时,Django 日志记录不工作

python - django-oscar 注册错误 : Socket error 10061 - Connection refused