简体   繁体   English

DJANGO - 多用户

[英]DJANGO - Multiple Users

I am building a platform in django and for the User registration I followed some tutorials, at this point I think it would be nicer if I can add a "new user registration".我正在 django 中构建一个平台,对于用户注册,我遵循了一些教程,此时我认为如果我可以添加“新用户注册”会更好。 In my case "students" can register, login and navigate the platform but I would like also to make a new type of user ( staff) where they can registrate as staff, login and visit only one page where they can create some new quiz.在我的情况下,“学生”可以注册、登录和导航平台,但我也想创建一种新类型的用户(员工),他们可以在其中注册为员工,登录并仅访问一个页面,在那里他们可以创建一些新的测验。

I tried to look online but I can't figure out how to do it from this point I have already created.我试图在网上查看,但从我已经创建的这一点开始,我无法弄清楚如何去做。

Could you please give me some advice/resources on how can I solve this problem?你能给我一些关于如何解决这个问题的建议/资源吗?

account/forms.py帐户/forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class FormRegistrazione(UserCreationForm):
    email = forms.CharField(max_length=30, required=True, widget=forms.EmailInput())

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

account/views.py帐户/views.py

from django.shortcuts import render, HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from accounts.forms import FormRegistrazione

# Create your views here.

def registrazioneView(request):
    if request.method == "POST":
        form = FormRegistrazione(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password1"]
            User.objects.create_user(username=username, password=password, email=email)
            user = authenticate(username=username, password=password)
            login(request, user)
            return HttpResponseRedirect("/")
    else:
        form = FormRegistrazione()
    context = {"form": form}
    return render(request, 'accounts/registrazione.html', context)

core/views.py核心/views.py

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView

# Create your views here.
from quiz.models import Questions
from jobs.models import post_job





def homepage(request):
    return render(request, 'core/homepage.html')

def userProfileView(request, username):
    user= get_object_or_404(User, username=username)
    jobs = post_job.objects.all()
    categories = Questions.CAT_CHOICES
    scores = []
    for category in categories:
        score = Questions.objects.filter(category=category[0], student= request.user).count()
        scores.append(score)
    context = {

    'user' : user, 'categories_scores' : zip( categories,scores),
    'jobs': jobs



    }
    return render(request, 'core/user_profile.html' , context)



class UserList(LoginRequiredMixin, ListView):
    model = User
    template_name = 'core/users.html'

there are basically two ways of going about this depending how serious this is.基本上有两种方法可以解决这个问题,具体取决于它的严重程度。

The simplest and easier way is to simply give some users access to admin area and allow them to use the /admin area and create Quizzes there provided you registered your Quiz model in admin.py, if you go this route remember to give them only the permissions required and no more, you can do this in the admin area in the user section.最简单和更容易的方法是简单地让一些用户访问管理区域并允许他们使用 /admin 区域并在那里创建测验,前提是您在 admin.py 中注册了您的测验 model,如果您 go 这条路线记住只给他们只需要权限,您可以在用户部分的管理区域中执行此操作。

The second more serious way would be to be an approach followed by this excellent article here .第二种更严肃的方法是成为这篇优秀文章所遵循方法。 If you follow the article it seems to be exactly what you want.如果您按照文章进行操作,它似乎正是您想要的。 For the quiz creation form and view you can simply make a protected view that requires the user to be teacher as show in the article.对于测验创建表单和视图,您可以简单地制作一个受保护的视图,要求用户是老师,如文章中所示。

Depending on your specific needs and use case (do you trust staff members? How many people do you expected to use it?) I would probably lean more towards the second approach as allowing end users to access your admin area is a bad practice and can be a security vulnerability.根据您的特定需求和用例(您信任员工吗?您希望有多少人使用它?)我可能会更倾向于第二种方法,因为允许最终用户访问您的管理区域是一种不好的做法,并且可以成为安全漏洞。 It does require you to write more code tho.它确实需要您编写更多代码。 Hope this helps.希望这可以帮助。

From what I understand from your question, you want to authenticate users and give them permissions (authorisation) accordingly.根据我从您的问题中了解到的情况,您希望对用户进行身份验证并相应地授予他们权限(授权)。 I personally prefer using JWT tokens for authentication: https://pypi.org/project/djangorestframework-simplejwt/我个人更喜欢使用 JWT 令牌进行身份验证: https://pypi.org/project/djangorestframework-simplejwt/

It is easy to implement and a secure way to authenticate.它易于实施且是一种安全的身份验证方式。 Django offers multiple ways to do authorisation, you can start from documentations directly: https://docs.djangoproject.com/en/3.0/topics/auth/default/#permissions-and-authorization Django 提供多种授权方式,您可以直接从文档开始: https://docs.djangoproject.com/en/3.0/topics/auth/default/#permissions-and-authorization

Basically, you will be authenticating users on login/signup for their identity and then giving certain permissions based on the role.基本上,您将在登录/注册时验证用户的身份,然后根据角色授予某些权限。 If the user isn't authenticated, you will throw 401, and if the user is not authorised for performing certain task, you will throw 403.如果用户没有通过身份验证,你会抛出 401,如果用户没有被授权执行某些任务,你会抛出 403。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM