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로 돌아온다.
'기타 > django' 카테고리의 다른 글
작정하고 장고 - Bootstrap을 이용한 Form 디자인 정리 (0) | 2023.05.10 |
---|---|
작정하고 장고 - login, logout 구현 (0) | 2023.05.09 |
작정하고 장고 - for 루프, 디버깅 (0) | 2023.04.11 |
작정하고 장고 - GET, POST (0) | 2023.04.10 |
작정하고 장고 - models.py, db 연동 (0) | 2023.04.07 |
댓글