Good night everyone.
I'm developing a project in django just for educational purposes and I could not find a solution to my problem.
I could not generate a form for the user to log in, much less authenticate this user.
I've already been able to generate a registration form, but I still have to authenticate the user.
What should I do?
Note: Django Version: 1.10.4
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import CadastroForm
def index(request):
return render(request, 'index.html')
def cadastro(request):
if request.method == 'POST':
form = CadastroForm(request.POST or None)
if form.is_valid():
return HttpResponseRedirect('/')
else:
form = CadastroForm()
return render(request, 'cadastro.html', {'form': form})
def entrar(request):
return render(request, 'entrar.html', {})
models.py
from django.db import models
# Create your models here.
class Cadastro(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=100)
username = models.CharField(max_length=40)
password = models.CharField(max_length=50)
forms.py
from django import forms
from .models import Cadastro
class CadastroForm(forms.ModelForm):
class Meta:
model = Cadastro
fields = ('name', 'email', 'username', 'password')
widgets = {
'password': forms.PasswordInput(),
}
login.html
{% extends "base.html" %}
{% load widget_tweaks %}
{% block content %}
<div class="container" style="margin-top: 50px; width: 500px;">
<h2 align="center">Login</h2>
<form action="{% url 'blog:index' %}" method="post">
{% csrf_token %}
{% for field in form_login %}
<div class="form-group">
<label for="{{field.id_for_label}}">{{field.label}}</label>
{{field|add_class:'form-control'}}
</div>
{% endfor %}
<div class="submit" align="center" style="margin-bottom: 50px;">
<button type="submit" class="btn btn-success">Enviar</button>
</div>
</form>
</div>
{% endblock %}
For those interested in viewing the project repository: GitHub