Home > database >  Keep getting Django form error "this field is required" even though the field has been fil
Keep getting Django form error "this field is required" even though the field has been fil

Time:03-30

I am creating a Django web app and I'm starting out on using Django forms. I keep getting this error even though this project name field is entered.

<tr><th><label for="id_title">projectName:</label></th><td><ul ><li>This field is required.</li></ul><input type="text" name="title" maxlength="100" required id="id_title"></td></tr>

Below is my code. I'm not sure on what I'm missing.

index.html:

<form id="project_form" action="{% url 'createproject' %}" method="post">
  {% csrf_token %}
      <div >
          <label for="projectName" >Project Name:</label>    &nbsp;
          <div >
              <input type="text"  placeholder="Enter Project Name" name="projectName" id="projectName">
          </div> 
      </div> 
      <input type="submit"  id="submit-project-btn">
</form>

forms.py:

class CreateEditProjectForm(forms.Form):
    title = forms.CharField(label="projectName", max_length=100, required=True)

models.py:

class TestProject(models.Model):
    creator = models.ForeignKey(User,on_delete=models.CASCADE,related_name="testcreatorId") 
    projectName = models.CharField(max_length=200, default=None)

    created_dt = models.DateTimeField(auto_now_add=True, auto_now=False)
    last_modified_dt = models.DateTimeField(auto_now_add=False, auto_now=True)

views.py:

def create_project(request):
    print("in create_project")
    print(request.method)
    if (request.method == "POST"):
        form = forms.CreateEditProjectForm(request.POST)
        print(form) 
        if form.is_valid():
            projectName = form.cleaned_data["projectName"]
            print(projectName)
            if (len(projectName) > 0):
                creator = User.objects.get(id=request.session['_auth_user_id'])
                project = TestProject(projectName=projectName, creator=creator)
                print(project)
                project.save()
                return HttpResponseRedirect(reverse("index"))
            return render(request, "index.html", {
                "form": form
            })
        return render(request, "index.html", {
            "form": form
        })
    else:
        form = forms.CreateEditProjectForm()
        return render(request, "index.html", {
            "form": form
        })

CodePudding user response:

Looks like you are not using the form fields that are rendered by django view in your html file. If you are using html inputs in your form, you might need to use ajax for submitting form. Follow the below approach to render django form fields in your html file.

Ref:https://docs.djangoproject.com/en/4.0/topics/forms/

    <form id="project_form" action="{% url 'createproject' %}" method="post">
        {% csrf_token %}
        <div >
            <label for="{{ form.title.id_for_label }}">Project Name:</label>
            <div >
                {{form.title}}
            </div>
        </div>
        <input type="submit"  id="submit-project-btn">
    </form>

CodePudding user response:

I've figured out why the error occurred. It's because of my forms.py.

class CreateEditProjectForm(forms.Form):
    title = forms.CharField(label="projectName", max_length=100, required=True)

The title should be projectName.

  • Related