私はこのModelFormを持っており、htmlでクラスをレンダリングしようとしていますが、動作しません。ここで私のDjangoフォームがウィジェットクラスをレンダリングしない理由は何ですか?
は私が持っているものです。
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'
)
EDIT が、私はテンプレート
でフォームをレンダリングしようとしている2つの方法があり自動:
<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
@dahrensちょうどそれを示すために投稿を編集しました。 – Garrett
ビューを投稿する必要があります。そこには、使用するテンプレートを定義する場所があります。 –