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

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

问题描述

在呈现页面时,如何将对象传递到模型表单中以预填充字段?我想做一些与基于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天全站免登陆