Django not rendering Crispy Form Fields from models.py

Hi i’m new to Django and In my first project I am trying to construct a new project form to fill in information to store online. I’m using bootstrap4 as the default template pack with django_crispy_forms. I have been trying to render a form with the crispy tags/filter. I have created another form for enquires and had success in rendering the fields from forms.py. However this time I need to render the form from a model. The button and title render on the template but no matter what I try from other examples of modelforms I cant seem to make my fields render to the template like it should.

I have tried renaming the models.py field name, stripping the code back to just the basics,I have also tried starting a new django project with just projects and crispy forms as the only user installed apps. However I still get the same issue of only the button appearing with no form fields rendered from models.py. So I don’t believe it is bug related to other installed apps…I am a bit lost as what to try next…I have got form fields showing in another app based off information within the forms.py but trying to render form fields from the models.py information doesn’t seem to work. Getting information from the model is critical for the rest of my project to function. Does anyone have any suggestions of what to try next?

Any help would be greatly appreciated.

Models.py

from django.db import models

# Create your models here.
class Project(models.Model):
project_address = models.CharField(max_length=200, null=True)

Forms.py

from django import forms
from .models import Project

class projectForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = [
            'project_id',
        ]

Views.py

from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.conf import settings 

from .models import Project
from .forms import projectForm

def projectinfo(request):
    form = projectForm(request.POST or None)
    context = {}
    template = 'projectinfo.html'
    return render(request,template,context)

HTML

{% load crispy_forms_tags %}

{% block content %}

            <form action="{% url 'projectinfo' %}" method="POST">{% csrf_token %}
                {{ form|crispy }}               
                <input type="submit" value="Save Project" class="btn btn-default" role="button" style="background-color: #007bff; color: #ffffff;"/>
            </form>

{% endblock %}

Settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',


    'projects’,
    'crispy_forms',

]

Screenshot

In your forms file, the form name is ProjectForm but in views you’re using the name projectForm. I would expect this to cause your application to not run at all, so is this a copy/paste error?

Hi Ken, Thanks for your help, it was a typo sorry. I have updated the forms.py code above.

You’re not including the form in your context to be rendered.

Your view:

def projectinfo(request):
    form = projectForm(request.POST or None)
    context = {}
    template = 'projectinfo.html'
    return render(request,template,context)

On line 2, you’re creating a form - but you’re not making part of your context on line 3.

You might find it helpful to review the Django Forms documentation, particularly the section on The view.

Ken

Thanks Ken, I have updated my view to:

def projectinfo(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = projectForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required
        # ...
        # redirect to a new URL:
        return HttpResponseRedirect('/Project Updated/')

# if a GET (or any other method) we'll create a blank form
else:
    form = projectForm()

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

and the form field has finally appeared in the template. Thanks so much for your help, it is greatly appreciated.