Jestem początkującym Django i otrzymuję ten błąd podczas ładowania serwera.SO proszę o pomoc

To jest mój rejestr / 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, UserUpdateForm, ProfileUpdateForm


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'register/register.html', {'form': form})


@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST,
                                   request.FILES,
                                   instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }

    return render(request, 'register/profile.html', context)

To jest mój rejestr / sygnał.py

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile


@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

To jest mój forms.py

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


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

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

To jest rejestr / apps.py

from django.apps import AppConfig
class RegisterConfig(AppConfig):
    name = 'register'
    def ready(self):
        import register.signals

Błąd jego pokazuje stąd = z .Forms Import Userregisterform, UserUpDateform, profilUpDateform

Proszę spojrzeć, co robię źle ..

0
Avinash Dwivedi 16 styczeń 2020, 10:45

1 odpowiedź

Najlepsza odpowiedź

Twój forms.py ma tylko UserRegisterForm. Błąd skarży się na fakt, że nie może zaimportować UserUpdateForm (i prawdopodobnie, jeśli usuniesz to, będzie narzekać na ProfileUpdateForm, ponieważ to nie jest zdefiniowane). Możesz rozwiązać problem z UserUpdateForm, importując go z modułu django.contrib.auth.forms, a re-eksport . Dla ProfileUpdateForm musisz wdrożyć dodatkowe formularze:

# register/forms.py

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

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

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

class ProfileUpdateForm(forms.ModelForm):

    class Meta:
        model = Profile
        exclude = ['user']
0
Willem Van Onsem 16 styczeń 2020, 08:13