Form fields not appearing in HTML

I created a form to collect users emails, phone numbers, and their names of course, the form fields are not showing in my html page only the submit button is showing

here are models

from django.db import models

# Create your models here.
class User(models.Model):
    name = models.CharField(max_length=15)
    email = models.EmailField()
    phone = models.CharField(max_length=10)

forms.py

from django.forms import  ModelForm
from landing_page.models import User

class UsersDataForm(ModelForm):
    class Meta:
        model = User
        fields = ['name','email','phone']

views

from django.core.exceptions import ValidationError
from django.shortcuts import render, redirect
from django.core.validators import validate_email
from .forms import UsersDataForm

# Create your views here.
def home(request):
    return render(request, 'home.html')


def users_data(request):

    if(request.method == 'POST'):
        form = UsersDataForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                validate_email(email)
            except ValidationError:
                form.add_error('email', 'تأكد من الايميل الذي ادخلته')
            if not form.errors:
                form.save()
                form = UsersDataForm()
                return redirect('home', {'form': form})

    else:
        form = UsersDataForm()

    return render(request, 'home.html', {'form': form})

template

<form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">تسجيل</button>
    </form>

Chrome developer tools
troubleshooting

Let’s start by fixing the obvious problems:

def users_data(request):

    if request.method == 'POST':
        form = UsersDataForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                validate_email(email)
            except ValidationError:
                form.add_error('email', 'تأكد من الايميل الذي ادخلته')
            if not form.errors:
                form.save()
                return redirect('home')

    else:
        form = UsersDataForm()

    return render(request, 'home.html', {'form': form})

Now that looks correct to me. I would recommend running with django-fastdev installed to get some common mistakes checked.

You might want to check with a simple print(form.fields) that the UsersDataForm form does indeed have any fields.

I have problems with import models to forms.py
and from forms.py to views.py

some how they can’t see each other.

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

That is a totally different problem. You should start a new thread for that, and post the full error when you do.

1 Like

Thank you so much. I really appreciate it.