[Answered ]-How to pass data from a template to the Django side

1👍

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.

0👍

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.

Leave a comment