How to register different types of users in Django?

  • Certainly! Here's an example code github link in Django that demonstrates how to save user details obtained from an HTML template:


    from django.http.response import HttpResponse
    from django.shortcuts import redirect, render
    from django.contrib.auth.models import User
    from django.contrib import messages
    from django.views.decorators.csrf import csrf_exempt

    def home(request):
        return render(request, "pages/home.html")

    @csrf_exempt
    def create_superuser(request):
        if request.method == "POST":
            username1 = request.POST['username1']
            email1 = request.POST['email1']
            password1 = request.POST['password1']
            user = User.objects.create_user(username1, email1, password1)
            user.is_staff = True
            user.is_superuser = True
            user.save()
            messages.success(request, "SuperUser Created Successfully")
            return redirect('/')
        else:
            return HttpResponse("<h1>404 - Not Found</h1>")

    @csrf_exempt
    def create_adminuser(request):
        if request.method == "POST":
            username2 = request.POST['username2']
            email2 = request.POST['email2']
            password2 = request.POST['password2']
            user = User.objects.create_user(username2, email2, password2)
            user.is_staff = True
            user.save()
            messages.success(request, "AdminUser Created Successfully")
            return redirect('/')
        else:
            return HttpResponse("<h1>404 - Not Found</h1>")

    @csrf_exempt
    def create_normaluser(request):
        if request.method == "POST":
            username3 = request.POST['username3']
            email3 = request.POST['email3']
            password3 = request.POST['password3']
            User.objects.create_user(username3, email3, password3).save()
            messages.success(request, "NormalUser Created Successfully")
            return redirect('/')
        else:
            return HttpResponse("<h1>404 - Not Found</h1>")

  • In the above code, we define a view functions that handles the registration process. It expects a `POST` request from an HTML form submission.
  • Inside the function, we retrieve the user details from the `request.POST` dictionary, which contains the data sent from the HTML form. We extract the values for `username`, `email`, and `password`.
  • Then, using the `create_user` method from Django's built-in `User` model, we create a new user object with the obtained details. After that, we save the user object to persist it in the database using the `save` method.
  • Finally, you can customize the response by rendering a success message or redirecting to template.
  • Note: This code assumes you have properly set up Django and have the necessary configurations in place.

No comments:

Post a Comment

PHASE 1 — Topic 4: How Socket.IO Works Internally (Engine.IO, Transports, and Fallback)

We now understand what Socket.IO is and why it is useful. In this post, we go one level deeper and look at what actually happens behind the ...