0
這是用於編輯通過profile_edit視圖的ModelForm代碼:編輯視圖不顯示django中的選擇字段數據?
models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
city = models.CharField(default='kottayam', choices=CITY_CHOICES, max_length=150)
state = models.CharField(default='kerala', choices=STATE_CHOICES, max_length=150)
is_active = models.BooleanField()
def __unicode__(self):
return str(self.user)
forms.py
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
exclude = ['is_active']
views.py
def profile_edit(request):
profile, created = Profile.objects.get_or_create(user=request.user)
form = ProfileForm(request.POST or None, request.FILES or None, instance=profile)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('profiles:profile', username=request.user.username)
context = {
"title": 'Edit Profile',
"form":form,
}
return render(request, 'profiles/form.html', context)
form.html
<form method='POST' action='' enctype='multipart/form-data' role="form">{% csrf_token %}
{{ form|crispy }}
<input type='submit' class='btn btn-success' value='Add/Edit Profile' />
</form>
Charfield與選擇,情況是不可見的編輯,任何人都可以請幫助?
沒有,它仍然是一樣的,沒有顯示在編輯表單的選擇領域。 – Biju