如何使用django进行注册和登录系统? [英] How to make a registration and login system using django?

查看:49
本文介绍了如何使用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 unicode_literals
from django.db import models


class profile(models.Model):
    WORK = (
            ('School', 'School'),
            ('Collage', 'Collage'),
            ('Job', 'Job')
        )
    name = models.CharField(max_length=150)
    email = models.EmailField(max_length=254)
    username = models.CharField(max_length=100, unique=True)
    password = models.CharField(max_length=80)
    age = models.IntegerField() 
    work = models.CharField(max_length=10, choices=WORK)


    def __unicode__(self):
        return self.username 

Forms.py 用于用户注册

from django import forms
from .models import profile



class registration_form(forms.ModelForm):
    class Meta:
        model = Post 
        fields={
        "name",
        "email",
        "username",
        "password",
        "work",
        "age"
        }

我想使用上述数据库对用户进行身份验证,我该怎么做?(我也不想使用任何类似django-registration的第三方库)

I want to use the above database to authenticate the user how can i do that ?(also I dont want to use any third party library like django-registration)

推荐答案

@Alasdair是正确的,您不需要django.contrib.admin即可使用django.contrib.auth.

@Alasdair is right, you don't need the django.contrib.admin in order to use the django.contrib.auth.

下一个解决方案,利用基于django泛型类的视图.

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>

注意:

  • 为了创建自己的用户模型,您需要配置 AUTH_USER_MODEL ,并从 AbstractUser 继承.
  • AuthenticationForm 将验证用户名/密码并触发form_valid.
  • 我正在使用django.contrib.auth.views import login 中的来导入django登录视图,该视图负责在表单验证后执行登录操作.
  • In order to make your own User model, you need to configure the AUTH_USER_MODEL, and inherit from AbstractUser.
  • 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.

这篇关于如何使用django进行注册和登录系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆