How To Make A Registration And Login System Using Django?
I want to create a registration and login system I have made the registration form but I'm not able to carry out the authentication part of it. Models.py from __future__ import uni
Solution 1:
@Alasdair is right, you don't need the django.contrib.admin in order to use the django.contrib.auth.
Next a solution, that make use of django generic class based views.
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class Profile(AbstractUser):
WORK = (
('School', 'School'),
('Collage', 'Collage'),
('Job', 'Job')
)
)
age = models.IntegerField()
work = models.CharField(max_length=10, choices=WORK)
settings.py
AUTH_USER_MODEL = 'my_users_app.Profile'
LOGIN_URL = '/login/' # Where our login page is
views.py
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.views import login as django_login_view
from django.views.generic import FormView
class LoginView(FormView):
form_class = AuthenticationForm
template_name = 'my_users_app/login.html'
def form_valid(self, form):
usuario = form.get_user()
django_login_view(self.request, usuario)
return super(LoginView, self).form_valid(form)
urls.py
from my_users_app.views import LoginView
urlpatterns = [
url(r'^login/$', LoginView.as_view(), name='login'),
]
login.html
<form method="post" autocomplete="off">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
Note:
- In order to make your own User model, you need to configure the
AUTH_USER_MODEL
, and inherit fromAbstractUser
. - The
AuthenticationForm
will validate the username/password and will trigger the form_valid. - I'm using
from django.contrib.auth.views import login
to import the django login view who is responsible to make the login action once the form has been validated.
Post a Comment for "How To Make A Registration And Login System Using Django?"