본문 바로가기
기타/django

작정하고 장고 - CreateView를 통한 회원가입 구현

by 방배킹 2023. 5. 8.

21강

views.py

클래스형 view로 다음과 같이 코드를 작성해준다.

class AccountCreateView(CreateView):
    model = User
    form_class = UserCreationForm
    success_url = reverse_lazy('accountapp:hello_world')
    template_name = 'accountapp/create.html'

함수형 view에서는 다음과 같이reverse('accountapp:hello_world')) reverse를 사용하지만

클래스형 view에서는 reverse_lazy('accountapp/hello_world.html') 처럼 reverse_lazy를 사용한다.

이후 accountapp./urls.py에

path('create/', AccountCreateView.as_view(), name='create')

를 추가해준다.

그리고 이제 create.html 파일을 만들어준다

{% extends 'base.html' %}

{% block content %}

  <div>
    <form action="{% url 'accountapp:create' %}" method="post">
      {% csrf_token %}
      {{ form }}
      <input type="submit" class="btn btn-primary">
    </form>
  </div>

{% endblock %}

서버를 실행하고 http://127.0.0.1:8000/account/create/ 에 접속을하면

잘 실행이된다.

아이디와 비밀번호를 입력하고 실행을 하면

hello_world.html로 돌아온다.

댓글