Tanmay Kumar
Tanmay Kumar

Reputation: 17

the content of Django form is not showing

I am little new to programming and I am trying to make the home page and login page in the same page the email and the password field are not showing

index.html

<div class="container-fluid">

        <div class="row">
          <div class="col-md-8">
            <img src="{% static 'img/sampleImage.jpg' %}" width="100%" height="100%" class="d-inline-block align-top" alt="">
          </div>
          <div class="col-md-4">
            <form method="POST">
              {% csrf_token %}
              {{ form }}
              <div class="form-check">
                  <span class="fpswd">Forgot <a href="#">password?</a></span>
              </div>
              <button type="submit" class="btn btn-primary">Submit</button>
            </form>



          </div>
        </div>
      </div>

app/views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from homePage.forms import SignInForm
# Create your views here.
def homePage(request):

    if request.method == 'POST':
        sign_in_detail = SignInForm(request.POST)
        if sign_in_detail.is_valid():
            return render(request, "index2.html",{})
    else:
        sign_in_detail = SignInForm()
    return render(request, "index.html",{"form":'sign_in_detail'})

app/forms.py

from django import forms
from django.core import validators

class SignInForm(forms.Form):
    email   = forms.EmailField(widget=forms.EmailInput(
                attrs={
                    "class": 'form-control',
                    "placeholder":'Enter E-mail',
                    "id": 'exampleInputEmail1'
                        })
                )
    password    = forms.PasswordInput(
                        attrs={  "class":'form-control',
                                "placeholder":'Enter Password',
                                "id":'exampleInputPassword1'
                                })

the output is just a string "sign_in_detail"

Upvotes: 0

Views: 144

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

You need to remove single quotation around your sign_in_detail:

return render(request, "index.html",{"form":sign_in_detail})

And also why are you sending an empty dictionary for the POST request.You need to change your view like this:

if request.method == 'POST':
    sign_in_detail = SignInForm(request.POST)
    if sign_in_detail.is_valid():
       sign_in_detail.save()
       return redirect('some_view')
else:
    sign_in_detail = SignInForm()
return render(request, "index.html",{"form":sign_in_detail})

Upvotes: 1

Related Questions