Search code examples
pythondjangodjango-forms

Django forms not rendering CharFields


In my Django form, only CharFields are exclusively not showing up. Other fields work just fine (e.g. the EmailField and a TurnstileField using django-turnstile). I cannot figure out why.

I followed the Django docs and tried to build a form.

Code is as follows:

auth.html

{% block body %}
        <form action="https://artimaths.com/auth/{{ action }}/" method="POST">
            {% csrf_token %}
            {{ form }}
            <input type="submit" value="Submit">
        </form>
{% endblock %}

views.py

from django.core.exceptions import ValidationError
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail
from django.template import loader
from .models import Member
from .forms import SignupForm, LoginForm
from modules import context
import hashlib
import re

def signage(request, action):
    t = loader.get_template("auth.html")
    if action != "register" and action != "login" and action != "logout":
        return HttpResponse(STATUS_DICT[str(400)] + str(400))

    if action == "register":
        req_context["form"] = SignupForm
        req_context["form_class"] = SignupForm
    elif action == "login":
        req_context["form"] = LoginForm
        req_context["form_class"] = LoginForm


    if req_context["status"] is not 200:
        return HttpResponse(STATUS_DICT[str(req_context["status"]) + req_context["status"]])
    return HttpResponse(t.render(req_context, request))

forms.py

from django import forms
from turnstile.fields import TurnstileField
from .models import Member

class SignupForm(forms.Form):
    username = forms.CharField(label="Username"),
    password = forms.CharField(label="Password"),
    email = forms.EmailField(label="Email")
    terms = forms.BooleanField(label="I agree with the Terms of Service and Privacy Policy of Artimaths")
    captcha = TurnstileField(theme="dark", size="compact")
    class Meta:
        model = Member
        fields = ("username", "password", "email", "terms", "captcha")

class LoginForm(forms.Form):
    username = forms.CharField(label="Username"),
    password = forms.CharField(label="Password"),
    captcha = TurnstileField(theme="dark", size="compact")

    class Meta:
        model = Member
        fields = ("username", "password", "captcha")

models.py

import uuid
from django.db import models

class Member(models.Model):
    userid = models.UUIDField(primary_key = True, default = uuid.uuid4, editable = False)
    name = models.CharField(max_length = 255)
    email = models.EmailField()
    password_hash = models.CharField(max_length = 512)

    def __str__(self):
        return f"{self.name} ({self.userid})"

The turnstile fields always render correctly, as well as the Email field. I also tried widget_tweaks by rendering each field one by one, but that did not work either, produced similar results like this:

Widget_tweaks results

Addition: I also tried the adding things to widgets under the Meta class, did not work either. Meta.wdigets results


Solution

  • remove the comma at the end of the lines in the login form and the signup form and this will hopefully solve your problem. The comma is stopping the field rendering in the template.

    Let me know if the problem persists.