ModelForm isnt working

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>

ah and btw ignore the fact that I used for country and profile pic another form structure I know that it isnt correct, I didnt changed it yet

Your title says “ModelForm isn’t working.” What’s not working? What error message(s) are you receiving? Or, if you’re not getting an error message, what’s happening that you’re not expecting to have happen (or not happening that you’re expecting to see)?

there is no error, on my page where the form should Display there is just the submit button instaed of the form

Review the docs on The view - particularly the example in that section that handles the GET request (the else clause of the if ... POST condition).
Notice how they create the blank form in the example, and compare it to your code.

the only thing I noticed by comparing the two code sections is that I forgot the brackets at the end of my else statement content. But my form still doesnt work.

Parens, not brackets - but yes, that’s an error.

Are you still not seeing the form on your page after that correction?

nope, just the submit button. And thanks Im not from England or America so I thought its brackets.

Ok, I’ve never used that django-countries module with the country field. I’d suggest to temporarily add another field or replace the country field with a different field to help determine if the issue is in that field or in your view / form / model.

But in my case I’m trying to see the form for the Prename, the Countries Module is just another Field for the Form but thats unimportant for the form, isnt it.

Countries Moudule is just another Field from the model*

like I just want to see the Prename Form which is from the Prename Field of the Model the other Fields are unimportant for the Prename Form.

is it even possible to make diffrent Forms for the Fields of one Model?

The issue is that CountryField is not a standard Django field. I don’t know anything about it, so I don’t automatically trust that it’s going to do the right thing.
If you try your form with a standard field and it works, then that’s a demonstration that there is either something wrong with that third-party package, or you’re not using it correctly. Either way, it identifies the issue as being outside Django itself and how you’re creating / using the forms.

And yes, you can create as many forms as you wish with whatever combinations of fields are desired. You can also create forms where the fields to be used are determined at runtime.

so I changed the country and profile pic field now to a CharField but it still doesnt work so I guess something with my form is wrong maybe in the views.py…

Can you repost the model, form, and view?

I have, in the first comment of this topic

Your first post in this thread doesn’t reflect the changes talked about here. That’s why I asked you to repost those items, showing the corrections and changes that have been made.

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 CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']



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']



views.py

from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

from .forms import AccountInfoCountryForm, CreateUserForm
from .models import Account

from django.contrib import messages

def index_view(request):
    return render(request, 'home/index.html')

def login_view(request):
    if request.user.is_authenticated:
        return redirect('home:profile')
    else:
        if request.method == 'POST':
            username = request.POST.get('username')
            password = request.POST.get('password')

            user = authenticate(request, username=username, password=password)

            if user is not None:
                login(request, user)
                return redirect('home:profile')
            else:
                messages.info(request, 'Benutzername oder Passwort ist inkorrekt')

        return render(request, 'home/login.html')


def logout_view(request):
    logout(request)
    return redirect('home:login')


def register_view(request):
    if request.user.is_authenticated:
        return redirect('home:offers')
    else:
        form = CreateUserForm

        if request.method == 'POST':
            form = CreateUserForm(request.POST)
            if form.is_valid():
                form.save()
                messages.success(request, 'Account wurde erstellt')

                return redirect('home:login')

        context = {'form': form}
        return render(request, 'home/register.html', context)

from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

from .forms import CreateUserForm

from django.contrib import messages

def index_view(request):
    return render(request, 'home/index.html')

def login_view(request):
    if request.user.is_authenticated:
        return redirect('home:profile')
    else:
        if request.method == 'POST':
            username = request.POST.get('username')
            password = request.POST.get('password')

            user = authenticate(request, username=username, password=password)

            if user is not None:
                login(request, user)
                return redirect('home:profile')
            else:
                messages.info(request, 'Username or Password is incorrect')

        return render(request, 'home/login.html')


def logout_view(request):
    logout(request)
    return redirect('home:login')


def register_view(request):
    if request.user.is_authenticated:
        return redirect('home:profile')
    else:
        form = CreateUserForm

        if request.method == 'POST':
            form = CreateUserForm(request.POST)
            if form.is_valid():
                Account.user = request.user.id
                form.save()

                messages.success(request, 'Account wurde erstellt')

                return redirect('home:login')

        context = {'form': form}
        return render(request, 'home/register.html', context)


@login_required(login_url='home:login')
def profile_view(request):
    return render(request, 'home/profile.html')





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}

                  )


models.py

from django.db import models
from django.contrib.auth.models import User

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.CharField(max_length=40, null=True)
    country = models.CharField(max_length=40, null=True)
    packet_premium = models.BooleanField(null=True)
    packet_business = models.BooleanField(null=True)


    def __str__(self):
        return self.user.username



There’s got to be something else involved here - I cannot recreate the problem you’re describing from the information you’ve posted.

If you wish to keep digging into it, I think the next thing to check is your urls.py file(s), your settings for TEMPLATES, confirming your directory structures and file locations and verifying that you aren’t seeing any error messages in your runserver console.