Kshitij Karkera
Kshitij Karkera

Reputation: 33

Template data not passing into Django view.py

enter image description here

We are trying to get the name, email-ID and password through post method in the form, we get the data as none instead of the data filled in the form.

view.py

from django.http import HttpResponse 
# Create your views here.

def sign_up_in(response):
    email = response.POST.get('eemail')  #Getting the data of email by the name of eemail
    print(email)                         #Trying to print the email that we got from the form
    return render(response, "main/sign_up_in.html",{}) 

HTML code

There are two forms and we are trying to get the data from the first form. The first form is for creating the account and the second is for signing in.

<form method="POST" class="form" id="a-form" action="">
                    <h2 class="form_title title">Create Account</h2>
                    <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use email for registration</span>
                    <input class="form__input" type="text" placeholder="Name" name="eemail">
                    <input class="form__input" type="text" placeholder="Email">
                    <input class="form__input" type="password" placeholder="Password">
                    <button class="form__button button submit">SIGN UP</button>
                </form>
<form method="POST" class="form" id="b-form" action="">
                    <h2 class="form_title title">Sign in to Website</h2>
                    <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use your email account</span>
                    <input class="form__input" type="text" placeholder="Email" name="eemail">
                    <input class="form__input" type="password" placeholder="Password"><a class="form__link">Forgot your password?</a>
                    <button class="form__button button submit">SIGN IN</button>
                </form>

url.py


from . import views
urlpatterns = [
   path("", views.Home, name = "Homes"), 
   path("sign/", views.sign_up_in, name = "sign_up_in") 
]

Upvotes: 1

Views: 50

Answers (1)

Mohit Rathore
Mohit Rathore

Reputation: 71

**You should do it like this ** Write request instead of response and check that the method is GET or POST

def sign_up_in(request):
    if request.method == 'POST':
        email = request.POST.get('eemail') 
        print(email)                         
    return render(request, "main/sign_up_in.html",{}) 

Inside HTML Add action where you want to post data using form (use name of the path) ex: for signup

<form method="POST" class="form" id="a-form" action="{% url 'sign_up_in' %}"> #path name
                <h2 class="form_title title">Create Account</h2>
                <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use email for registration</span>
                <input class="form__input" type="text" placeholder="Email" name="eemail">
                <input class="form__input" type="password" placeholder="Password">
                <button class="form__button button submit">SIGN UP</button>
            </form>

Upvotes: 1

Related Questions