Home > Software engineering >  Django Forms not working. Does not display anything. Any helps?
Django Forms not working. Does not display anything. Any helps?

Time:12-20

I was doing some How-To tutorials about Django Forms. But it will not display anything for me. Any ideas why? Picture below illustrates my problem.

This is my Login -> index.html

    <body>
        <div  id="box">
            <h2>Log In</h2>
            <form action = "" method = "post">
                {% csrf_token %}
                {{form.as_p}}
                <input type="submit" value="Submit">
            </form>
        </div>
    </body>

this is forms.py

class InputForm(forms.Form):
   
    first_name = forms.CharField(max_length = 200)
    last_name = forms.CharField(max_length = 200)
    password = forms.CharField(widget = forms.PasswordInput())

this is views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import InputForm

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

def home_view(request):
    context ={}
    context['form']= InputForm()
    return render(request, "index.html", context)

def main(request):
    return render(request, 'login/main.html')

this is urls.py

from django.contrib import admin
from django.urls import path, include
from login.views import index, main

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', index),
    path('main/', main),
    path('accounts/', include('django.contrib.auth.urls')),
]

Login Page

CodePudding user response:

You are not passing the form context for the login route. In your index function, you don't have any context, and you put the form in home_view function which is not the view function being called by the /login route.

  • Related