如何将模型表单中的输入字段绑定到vue中

v-model通常您可以通过以下方式获取手动创建的输入字段:


<input class="form-control mb-2" name="username" v-model="username" type="text" placeholder="username" autocomplete="off">

<input class="form-control" type="password" v-model="password" placeholder="password">

但是,如何抓取并放置v-modelDjango 通过模型表单自动生成的字段?


<form method="post">

    {% csrf_token %}

    {{ user_form }}

    <button class="btn btn-primary mt-3">Register</button>

</form>


30秒到达战场
浏览 65回答 1
1回答

缥缈止盈

您可以在您的forms.pyclass UserForm(forms.ModelForm):&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; model = User&nbsp; &nbsp; &nbsp; &nbsp; fields = '__all__'&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super(UserForm, self).__init__(*args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for name, field in self.fields.items():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # add v-model to each model field&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.widget.attrs.update({'v-model': name})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python