我有这个ModelForm,我试图在html中渲染一个类,但是它不起作用。
这就是我所拥有的:
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = (
'first_name',
'profile_pic',
'location',
'title',
'user_type',
'website',
'twitter',
'dribbble',
'github'
)
widget = {
'first_name':forms.Textarea(attrs={'class':'form-control'}),
'profile_pic':forms.TextInput(attrs={'class':'form-control'}),
'location':forms.TextInput(attrs={'class':'form-control'}),
'title':forms.TextInput(attrs={'class':'form-control'}),
'user_type':forms.TextInput(attrs={'class':'form-control'}),
'website':forms.URLInput(attrs={'class':'form-control'}),
'twitter':forms.TextInput(attrs={'class':'form-control'}),
'dribbble':forms.TextInput(attrs={'class':'form-control'}),
'github':forms.TextInput(attrs={'class':'form-control'}),
}
我试过这个......
class UserProfileForm(forms.ModelForm):
first_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
class Meta:
model = UserProfile
fields = (
'first_name',
'profile_pic',
'location',
'title',
'user_type',
'website',
'twitter',
'dribbble',
'github'
)
修改 以下是我尝试在模板中呈现表单的两种方法
自动:
<div class="form-group">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-primary" type="submit" value="Save" />
</form>
</div>
手动渲染每个字段: 请原谅我在这里的自举设置。
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="form-row">
<div class="form-group col">
{{ form.first_name.errors }}
<label for="{{ form.first_name.id_for_label }}">Name:</label>
{{ form.first_name }}
{{ form.profile_pic.errors }}
<label for="{{ form.profile_pic.id_for_label }}">Profile Picture:</label>
{{ form.profile_pic }}
</div>
<div class="col-3">
{{ form.location.errors }}
<label for="{{ form.location.id_for_label }}">Location:</label>
{{ form.location }}
</div>
</div>
<div class="fieldWrapper">
{{ form.location.errors }}
<label for="{{ form.location.id_for_label }}">Location:</label>
{{ form.location }}
</div>
<div class="fieldWrapper">
{{ form.title.errors }}
<label for="{{ form.title.id_for_label }}">Title:</label>
{{ form.title }}
</div>
<div class="fieldWrapper">
{{ form.user_type.errors }}
<label for="{{ form.user_type.id_for_label }}">User Type:</label>
{{ form.user_type }}
</div>
<div class="fieldWrapper">
{{ form.website.errors }}
<label for="{{ form.website.id_for_label }}">Website:</label>
{{ form.website }}
</div>
<div class="fieldWrapper">
{{ form.about.errors }}
<label for="{{ form.about.id_for_label }}">About:</label>
{{ form.about }}
</div>
<div class="fieldWrapper">
{{ form.twitter.errors }}
<label for="{{ form.twitter.id_for_label }}">Twitter:</label>
{{ form.twitter }}
</div>
<div class="fieldWrapper">
{{ form.dribbble.errors }}
<label for="{{ form.dribbble.id_for_label }}">Dribbble:</label>
{{ form.dribbble }}
</div>
<div class="fieldWrapper">
{{ form.github.errors }}
<label for="{{ form.github.id_for_label }}">Github:</label>
{{ form.github }}
</div>
<input class="btn btn-primary" type="submit" value="Save" />
</form>
观点:
class UserEditProfileView(LoginRequiredMixin,UpdateView):
login_url = '/login/'
model = UserProfile
fields = [
'first_name',
'profile_pic',
'location',
'title',
'user_type',
'about',
'website',
'twitter',
'dribbble',
'github'
]
template_name_suffix = '_edit_form'
def get_success_url(self):
userid = self.kwargs['pk']
return reverse_lazy('users:user_profile',kwargs={'pk': userid})
他们都没有工作,我一直在寻找如何让这个工作,但我似乎无法弄明白。
答案 0 :(得分:1)
默认情况下,UpdateView会为您生成一个表单,可以进一步使用。
指定您的自定义表单类:
class UserEditProfileView(LoginRequiredMixin,UpdateView):
model = UserProfile
form_class = UserProfileForm
它隐藏在文档中。您需要浏览UpdateView使用的mixins。 FormMixin为您提供此属性。
如果您不提供一个 - ModelForm is smart enought to create one。