updateform, A user with that username already exists. 에러
1558
3 asked
updateform에서 disabled = True로 변경하여 url로 접속해보면 제대로 적용이 되는 것을 확인할 수 있습니다.
하지만 비밀번호를 입력하고 제출을 누르면 계속해서 A user with that username already exists. 메시지만 뜨고 있는 상황입니다. ㅠㅠㅠ
Answer 3
1
저도 같은 문제로 찾아봤어요(+chatgpt활용)
views.py
# (3)수정뷰
class AccountUpdateView(UpdateView):
#해당 CBV를 통해 활용할 모델객체 파라미터
model = User #User 로 모델 지정
#기존 django 제공 UserCreationForm을 리팩토리한 forms.py에서 정의한 AccountUpdatedForm 활용
form_class = AccountUpdateForm
#해당 CBV를 통해 반환활 페이지
success_url = reverse_lazy("accountapp:hello_world")
# 해당 CBV를 통해 볼 페이지
template_name = "accountapp/update.html"
def get_success_url(self):
return reverse("accountapp:detail", kwargs={'pk': self.object.pk})from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
# UserCreationForm 상속 및 재활용
class AccountUpdateForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'password1', 'password2']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# UserCreationForm 필드 중 username 필드는 수정 못 하도록 잠금
self.fields["username"].disabled = True
def clean_username(self):
# 현재 사용자명은 이미 존재하는 것으로 간주
return self.instance.username해결이용!
1
장고 기능이 바뀐 건지 저도 같은 에러 뜨는데 그냥 비밀번호 변경하는 PasswordChangeView import 해서 사용해도 될 것 같아요
accountapp/views.py
from django.contrib.auth.views import PasswordChangeView
class AccountUpdateView(PasswordChangeView):
model = User
template_name = "accountapp/update.html"
success_url = reverse_lazy("accountapp:hello_world") #-- 성공한 경우 되돌아갈 페이지 지정 함수형 view -> reverseaccountapp/urls.py
from django.urls import path, reverse_lazy
from .views import hello_world
from .views import AccountCreateView, AccountDetailView, AccountUpdateView
from django.contrib.auth.views import LoginView, LogoutView
app_name = "accountapp"
urlpatterns = [
path('hello_world/', hello_world, name = "hello_world"),
path('login/', LoginView.as_view(template_name = "accountapp/login.html"), name = "login"),
path('logout/', LogoutView.as_view(), name = "logout"),
path('detail/<int:pk>', AccountDetailView.as_view(), name = "detail"),
path('update/<int:pk>', AccountUpdateView.as_view(),name = "update"),
#-- 몇 번 유저한테 접근할지 primary key 필요
path('create/', AccountCreateView.as_view(), name = "create"),
]

-3
안녕하세요, 인프런 AI 인턴이에요.
A user with that username already exists. 에러는 이미 존재하는 사용자 이름으로 새로운 사용자를 생성하려고 할 때 발생하는 에러입니다. 이 경우 신규 사용자 이름을 변경하거나, 이미 존재하는 사용자 이름을 가진 사용자의 정보를 업데이트 해야 합니다.
하지만 이 경우 비밀번호를 입력하고 제출하면 계속해서 오류 메시지가 발생한다는 것으로 보아, 신규 사용자를 생성하는 것이 아니라 이미 존재하는 사용자를 업데이트하면서 발생한 문제일 가능성이 높아 보입니다. 이 문제를 해결하기 위해서는 사용자 이름과 비밀번호를 모두 확인해야 합니다.
가능한 해결 방법으로는, 이미 존재하는 사용자의 정보를 확인하고, 해당 사용자의 비밀번호를 변경하거나, 사용자 이름을 수정하여 새로운 사용자를 생성하는 방법이 있습니다. 또한, 데이터베이스에 중복된 사용자 정보가 있는 경우에도 이러한 문제가 발생할 수 있으므로, 데이터베이스를 확인해 보시는 것도 좋은 방법입니다.
해당 문제를 해결하기 위해서는 조금 더 자세한 정보가 필요합니다. 현재 사용하시는 언어, 프레임워크, 데이터베이스 등을 알려주시면 더욱 정확한 답변을 드릴 수 있을 것 같습니다. 감사합니다.
0
저도 질문 주신 분과 같은 에러를 맞았습니다. python django를 활용하고 있고 sqlite를 사용하고 있습니다.
url은 다음과 같이 설정되어 있습니다.
urlpatterns = [
path('hello_world/', hello_world, name="hello_world"),
path('create/', AccountCreateView.as_view(), name="create"),
path('login/', LoginView.as_view(template_name='accountapp/login.html'), name="login"),
path('logout/', LogoutView.as_view(), name="logout"),
path('detail/<int:pk>', AccountDetailView.as_view(), name="detail"),
path('update/<int:pk>', AccountUpdateView.as_view(), name="update"),
path('delete/<int:pk>', AccountDeleteView.as_view(), name="delete"),
]
아래와 같이 forms.py를 설정하고
# class AccountUpdateForm(UserCreationForm):
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.fields['username'].disabled = True
update.view의 form_class에 아래와 같이 적용하였습니다.
class AccountUpdateView(UpdateView):
model = User
context_object_name = "target_user"
form_class = AccountUpdateForm
template_name = 'accountapp/update.html'
def get_success_url(self):
return reverse('accountapp:detail', kwargs={'pk': self.object.pk})
update.html은 이렇게 구성되어 있습니다.
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style="max-width: 400px; text-align:center; margin: 2rem auto;">
<h2>Change Info</h2>
<form action="{% url 'accountapp:update' pk=target_user.pk %}" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Change" class="btn btn-dark rounded-pill mt-3 col-3">
</form>
</div>
{% endblock %}
detail.html은 이렇게 구성되어 있습니다.
{% extends 'base.html' %}
{% block content %}
<div style="text-align:center; max-width:500px; margin: 4rem auto;">
<p> {{ target_user.date_joined }}</p>
<p> {{ target_user.username }}</p>
{% if target_user == user %}
<a href="{% url 'accountapp:update' pk=user.pk %}">
<p>Change info</p>
</a>
<a href="{% url 'accountapp:delete' pk=user.pk %}">
<p>Quit</p>
</a>
{% endif %}
</div>
{% endblock %}
그 후 runserver를 실행하면 username부분은 disabled처리가 되어있고
password와 password confirm을 입력하고 제출하게 되면 A user with that username already exists 에러가 나타납니다. 해결방법을 알려주시면 감사하겠습니다.

강의에 나왔던 js 파일이 깃허브에 없습니다
1
64
1
모바일 디버깅, 반응형 레이아웃 4분48초 질문
0
43
1
decorator 관련질문입니다.
0
49
1
PasswordChangeView
0
92
2
로그아웃뷰 작동 관련 (2025년 3월)
1
129
1
실행에러질문
0
146
1
@login_required 데코레이터 사용시 리다이렉트는 어디서 참조하여 설정을 하는걸까요?
0
109
1
CacheBackend 관련 에러
0
169
1
21강 CreateView를 통한 회원가입 구현 질문
0
318
1
53강 disallowedhost
0
330
2
502 Bad Gateway
0
467
1
mariadb 접근권한 오류
0
532
2
logout 후 빈 화면으로 이동합니다 ㅠㅠ
0
450
2
로그아웃 후 빈 화면으로 이동
0
324
1
서버 운영 관련 질문 드립니다.(Unable to retirve...)
0
227
1
static/base.css파일을 인식을 못합니다
0
373
1
안녕하세요 19강 디버깅 설정 질문있습니다.
0
287
1
프로필 update편 질문있습니다.
0
371
1
수업질문
0
387
1
COOP error
0
523
1
61강 Dockerfile error
0
589
1
static안에 base.css 에서 정의한 클래스가 적용되지 않습니다.
0
481
1
static 파일 중 jpg 파일만 로드 불가
0
408
1
58강 static 파일 적용 안됨
0
538
1

