1
Good night to you all.
I am developing a Django project for educational purposes only and I could not find a solution to my problem.
I could not generate a form for the user to log in and much less authenticate this user.
I already managed to generate a form for registration, but I still have to authenticate the user.
What should I do?
Note: Version of Django: 1.10.4
py views.
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', {})
py.models
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 anyone interested in seeing the project repository: Github