V
V
Vova1357982021-07-13 12:27:04
Django
Vova135798, 2021-07-13 12:27:04

Why does it give an error when registering a new user?

After registering a new user, he does not appear in the list of users, but a red inscription "Error" appears in the admin panel

models.py

from django.db import models
from django.contrib.auth.models import User


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    wishlist = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)

    def __str__(self):
        return f'{self.user.username} Profile'


views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse


def user_register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save
            username = form.cleaned_data.get('username')
            messages.success(request, 'Вы успешно зарегестрировались!')
            return redirect('login')
        else:
            messages.error(request, 'Ошибка')
            print('Выдает ошибку')
    else:
        form = UserRegisterForm()
    return render(request, 'register.html', {'form': form})
    print('Выдает ошибку')

forms.py 

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.core.files.images import get_image_dimensions
from .models import Profile
from django.forms import ModelForm


class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2021-07-13
@alternativshik

obviously the form is invalid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question