Reputation: 61
I'm very new to Django. I am trying to desing my login module and trying to print the values i submit.
login.html (please excuse indentation mistakes)
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="{% static 'css/lstyle.css' %}">
</head>
<body>
{% block content %}
{% if form.errors %}
<script> alert("Your username and password didn't match. Please try
again."); </script>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<script> alert("Your account doesn't have access to this page. To
proceed,please login with an account that has access."); </script>
{% else %}
<script> alert('Please login'); </script>
{% endif %}
{% endif %}
<div class="login">
<div class="login-screen">
<div class="app-title">
<h1>Login</h1>
</div>
<div class="login-form">
<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<p class="bs-component">
<table>
<tr>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password }}</td>
</tr>
<tr>
<td>{{ form.schoolID }}</td>
</tr>
</table>
</p>
<p class="bs-component">
<center>
<input class="btn btn-success btn-sm"
type="submit" value="login" />
</center>
</p>
<input type="hidden" name="next" value="{{ next }}"
/>
</form>
</div>
</div>
</div>
</body>
{% endblock %}
</html>
Forms.py
from django.contrib.auth.forms import AuthenticationForm
from django import forms
# If you don't do this you cannot use Bootstrap CSS
class LoginForm(AuthenticationForm):
username = forms.CharField(max_length=30,
widget=forms.TextInput(attrs={'class':
'form-control', 'name': 'username', 'placeholder': 'Username'}))
password = forms.CharField(max_length=30,
widget=forms.PasswordInput(attrs={'class':
'form-control', "name": 'password', 'placeholder': 'Password'}))
schoolID = forms.CharField(max_length=100,
widget=forms.TextInput(attrs={'class':
'form-control', 'name': 'schoolid', 'placeholder': 'School ID'}))
Views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from home.forms import LoginForm
from home.models import UserProfile
from django.views.generic import TemplateView
from django.contrib.auth.models import User
@login_required(login_url="login/")
def home(request):
if request.method == 'POST':
myform = LoginForm(request.POST)
print(myform.data['schoolID'])
return render(request, "home.html")
Things working:
html file renders perfectly.
User authentication works
if the "return render(request, "home.html")" is placed outside the if block, the code runs perfectly, and i am redirected to home.html - by entering matching username and password.
Things not working:
Unable to print schoolID. i.e, the "if request.method == 'POST':" is not being executed.
The error: The view home.views.home didn't return an HttpResponse object. It returned None instead.
what am i doing wrong here. I just want to print the form data and do operations with it before i execute the return statement. Please help.
Upvotes: 0
Views: 784
Reputation:
use myform = LoginForm(data=request.POST)
OR myform = LoginForm(None , request.POST)
because request is not the first parameter.
Upvotes: 1