I created signup/login with 'UserCreationForm'. How can I make update possible by using 'UserChangeForm'?
users/models.py
from django.contrib.auth.models
import AbstractUser
# Create your models here.
class CustomUser(AbstractUser): pass
def __str__(self): return self.username
users/forms.py
from django.contrib.auth.forms
import UserCreationForm, UserChangeForm
from.models
import CustomUser
class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm): model = CustomUser fields = ('first_name', 'last_name', 'username', 'email')
class CustomUserChangeForm(UserChangeForm): class Meta: model = CustomUser fields = ('first_name', 'last_name', 'username', 'email')
users/views.py
from django.shortcuts import render, redirect
# Create your views here.
from django.urls import reverse_lazy from django.views.generic.edit import CreateView from django.views import View
from .forms import CustomUserCreationForm, CustomUserChangeForm from .models import CustomUser
class SingUpView(CreateView): form_class = CustomUserCreationForm success_url = reverse_lazy('login') template_name = 'signup.html'
#выдает ошибку
class CustomUserUpdateView(View):
def get(self, request, *args, **kwargs):
user_id = kwargs.get("id")
user = CustomUser.objects.get(id=user_id)
form = CustomUserChangeForm(instance=user)
return render(
request, "users/update.html", {"form": form, "user_id": user_id}
)
def post(self, request, *args, **kwargs):
user_id = kwargs.get("id")
user = CustomUser.objects.get(id=user_id)
form = CustomUserChangeForm(request.POST, instance=user)
if form.is_valid():
form.save()
return redirect("users_list")
return render(
request, "users/update.html", {"form": form, "user_id": user_id}
)
I've been trying to create update with inheritance of the View class including 'get/post' methods, but it raises an error
CustomUser matching query does not exist.
users/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.IndexView.as_view(), name='users_list'),
path('signup/', views.SingUpView.as_view(), name='signup'),
path('<int:user_id>/update/', views.CustomUserUpdateView.as_view(), name='users_update'),
]
urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path("", views.index, name='home'),
path("users/", include("task_manager.user.urls")),
path('users/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]
I did everything Google told me to activate get/post (I checked user in the settings, tried to add try/except and use get_user_model), but none of that has worked out.
CustomUserUpdateViewyou’re trying to fetch a user with anidthat doesn’t exist in the database, check your database and tell me if there is data with that id in your query. also add yoururls.pyid. I would imagine your url path doesn’t look something like this, which is the cause most likely of your error.path("users/<int:pk>/edit/", CustomUserUpdateView.as_view(), name="user_edit"),