【发布时间】:2018-04-24 22:06:24
【问题描述】:
我有这个 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})
它们都不起作用,我一直在寻找如何使它起作用,但我似乎无法弄清楚。
【问题讨论】:
-
如何在模板中渲染表单?
-
@dahrens 刚刚编辑了帖子以显示这一点。
-
你应该发布你的观点,在那里你可以定义使用哪个模板。
-
@guillermochamorro 刚刚添加了视图
-
从
UserEditProfileView的角度来看,您的模型和表单类之间的粘合剂在哪里?