efeozkesici
efeozkesici

Reputation: 585

How to pass data from a template to the Django side

I just started to code Python Django. I just wanted to make a basic login project. I tried to pass login variables from the HTML side to file views.py.

Here is my HTML code:

<form action="/index" method="post">

    {% csrf_token %}

    <div class="form-group">
      <label for="username">Username</label>
      <input class="form-control" id="username" type="email"/>
    </div>

    <div class="form-group">
      <div class="d-flex justify-content-between">
        <label for="password">Password</label><a class="fs--1">Forgot Password?</a>
      </div>
      <input class="form-control" id="password" type="password" />
    </div>

    <div class="custom-control custom-checkbox">
      <input class="custom-control-input" type="checkbox" id="card-checkbox" checked="checked"/>
      <label class="custom-control-label" for="card-checkbox">Remember me</label>
    </div>

    <div class="form-group">
      <button class="btn btn-primary btn-block mt-3"
              type="submit"
              id= "login"
              name="submit">Log in</button>
    </div>
</form>

My urls.py file:

urlpatterns = [
    path('', views.loginPage, name="login"),
    path('index', views.home, name="index")
]

My forms.py file:

from django import forms

class LoginForm(forms.Form):
    username = forms.EmailField(label="Username"),
    password = forms.CharField(label="Password")

And my views.py file:

def home(request):
    context =  {}
    if request.method == 'POST':
        form = LoginForm(request.POST)
        print('form:', form)
        if form.is_valid():
            print('niceee')
        else:
            return render(request, 'index.html', context)
    else:
        form = LoginForm()

I just want to see username and password values into views.py. However, I can't see variables into views.py. How can I fix this?

Upvotes: 1

Views: 1275

Answers (2)

Jaap Joris Vens
Jaap Joris Vens

Reputation: 3560

You are a halfway there! Once the form has been validated by Django, you can access the form's variables in its cleaned_data attribute:

def home(request):
    context =  {}
    if request.method == 'POST':
        form = LoginForm(request.POST)
        print('form:', form)
        if form.is_valid():
            print('You submitted this username:', form.cleaned_data['username'])
            print('You submitted this password:', form.cleaned_data['password'])
        else:
            return render(request, 'index.html', context)
    else:
        form = LoginForm()

However, consider reusing django.contrib.auth.views.LoginView because then you won't have to write any Python code at all. Just customize the registration/login.html template.

Upvotes: 0

mukhtar.b_
mukhtar.b_

Reputation: 44

def home(request):

    if request.method == 'POST':
        form = LoginForm(request.POST)
        print('form:', form)
        if form.is_valid():
            print('username:', form.cleaned_data.get('username'))
            print('password:', form.cleaned_data.get('password'))
            print('niceee')

    else:
        form = LoginForm()

    context =  {}
    return render(request, 'index.html', context)

Check the server for username and password values.

Upvotes: 1

Related Questions