使用基于函数的视图更新视图 [英] Update view using function based view

查看:22
本文介绍了使用基于函数的视图更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在呈现页面时将对象传递到模型表单中以预填充该字段?我想做一些类似于在基于 Django UpdateView 类的视图中构建的事情,但使用基于函数的视图.

How can I pass an object into a model form to pre-populate the field when the page is rendered? I want to do something similar to the build in Django UpdateView class based view but with a function based view.

推荐答案

只需从模型中获取对象并将该对象作为实例传递给表单.然后将表单传递给模板.像下面的例子一样写下你的观点.

Just get the object from model and pass that object as instance to the form. Then pass the form to the template. Write your view like below example.

def func(request, id):

    object = Model.objects.get(id=id)
    form = ModelForm(instance=object)

    return render(request, 'my_template.html', {'form':form})

这篇关于使用基于函数的视图更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆