Hey,
I have a Model with a Model Form and I want to use for every Field of the Model another Form so I can use them on diffrent pages.
Is that possible?
I tried something by my own
forms.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.forms import ModelForm
from StartSite.models import Account
class AccountInfoPrenameForm(ModelForm):
class Meta:
model = Account
fields = ['prename']
class AccountInfoSurnameForm(ModelForm):
class Meta:
model = Account
fields = ['surname']
class AccountInfoPictureForm(ModelForm):
class Meta:
model = Account
fields = ['profile_pic']
#extra Form weil Picture
class AccountInfoCountryForm(ModelForm):
class Meta:
model = Account
fields = ['country']
#extra Form weil Country
class AccountInfoCompanyForm(ModelForm):
class Meta:
model = Account
fields = ['company']
And heres my model:
from django.db import models
from django.contrib.auth.models import User
from django_countries.fields import CountryField
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
prename = models.CharField(max_length=20, null=True)
surname = models.CharField(max_length=40, null=True)
company = models.CharField(max_length=40, null=True)
profile_pic = models.ImageField(blank=True, null=True)
country = CountryField(blank=True)
packet_premium = models.BooleanField(null=True)
packet_business = models.BooleanField(null=True)
def __str__(self):
return self.user.username
And my views.py for the first Form but at the page there is just my submit Button shown
def AccountInfoPrename(request):
if request.method == 'POST':
form = AccountInfoCountryForm(request.POST)
if form.is_valid():
account = form.save(comtmit=False)
account.prename = form.cleaned_data['prename']
account.save()
return redirect('home:profile')
else:
form = AccountInfoCountryForm
return render(request,
'home/profile.html',
{'form': form}
)
This is my html page:
<form action="{% url 'home:profile' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button class="button" type="submit">Submit</button>
</form>