H
H
hatman2019-01-07 17:29:05
Django
hatman, 2019-01-07 17:29:05

Why are broken records created in the user database?

I do AJAX registration. Faced with the situation that the user jumps id users (100, 112, 113, 124), etc. User IDs that do not exist give an error 500.
First, I found an error with the csrf token, if the user reloaded the page (removed the check for the token), then there were errors when clicking (the user clicked create an account several times - put the check on an existing user). Still getting 500 errors.
What can be wrong?

@csrf_exempt
def registration(request):
    if request.POST:
        username = request.POST.get('username', '')
        email = request.POST.get('email', '')
        try:
            User.objects.get(email=email)
            json = {
                "error_login": "Такой пользователь уже существует<br>Перезагрузите страницу, возможно вы уже вошли",
                    }
            return JsonResponse(json, safe=False,)
        except User.DoesNotExist:
            password1 = request.POST.get('password1', '')
            password2 = request.POST.get('password2', '')
            if str(request.POST.get('checkox', '')) == "18": // проверка капчей на число от простых ботов
                if password1 == password2:
                    User.objects.create_user(username=username, email=email, password=password1)
                    newuser = auth.authenticate(username=username, password=password1)
                    auth.login(request, newuser)

                    # Созданием one-to-one модель Profile и привязываем её к модели User
                    current_user = request.user
                    profile_obj = Profile(user_id=current_user.id)
                    profile_obj.save()

                    json = {
                        "reload": "reload",
                            }
                    return JsonResponse(json, safe=False,)

                else:
                    json = {
                        'error_login': "пароли не совпадают",
                            }
                    return JsonResponse(json, safe=False,)
            else:
                json = {
                    'error_login': "неверная валидация поля",
                        }
            return JsonResponse(json, safe=False,)

    else:
        json = {
                'error_login': "произошла ошибка сервера",
                    }
        return JsonResponse(json, safe=False,)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question