21강 CreateView를 통한 회원가입 구현
class AccountCreativeView(CreateView):
model = User
form_class = 계정은 생각보다 중요한 과정이다. 장고가 기본적인 폼을 제공해준다
views.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
# Create your views here.
#view 단의 완성
from django.urls import reverse, reverse_lazy
from django.views.generic import CreateView
from accountapp.models import HelloWorld
def hello_world(request):
if request.method == "POST":
# 포스트가 완료한이후에 겟으로 되돌아가서 남아있는 글들을 지울 수 있게한다.
temp = request.POST.get("hello_world_input")
new_hello_world = HelloWorld()
new_hello_world.text = temp
new_hello_world.save()
hello_world_list = HelloWorld.objects.all() #헬로월드에 모든 데이터를 다 긁어온다.
return HttpResponseRedirect(reverse('accountapp:hello_world'))
# 포스트가 완료한이후에 겟으로 되돌아가서 남아있는 글들을 지울 수 있게한다. redirect
# URL에서 ACCOUNT를 가져온다. 해당 경로를 만들어줄 함수를 쓴다
else:
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world.html',
context={'hello_world_list': hello_world_list})
#view는 만들었고 이제 route해줘야한다.
#특정 주소를 만들어 주는 작업이 필요하다.
class AccountCreateView(CreateView):
model = User
form_class = UserCreationForm
success_url = reverse_lazy('accountapp:hello_world') #여기서 reverse를 사용불가 클래스형
#reverse는 함수형에서 사용한다.
template_name = 'accountapp/create.html'
urls.py
from django.urls import path
from accountapp.views import hello_world, AccountCreateView
app_name = 'accountapp'
#"127.0.0.1:8000/account"
#왜 어카운트앱안에 있는데 또 다시 만드냐
#저위에 라우트를 매번 쓸수 없기에 기능을 사용해서 헬로월드라는 곳으로 바로가라는 함수를
#사용할 수 있다.
urlpatterns = [
path('hello_world/', hello_world, name='hello_world'),
#롸우터의 이름
path('create/', AccountCreateView.as_view(), name='create'),
]
create.pyc
{% extends 'base.html' %}
{% block content %}
<div style="text-align: center">
<form action="{% url 'accountapp:create' %}" method="post">
{% csrf_token %}
<!-- #장고 기본제공 보안시스템 필수 !-->
{{ form }}
<input type="submit" class="btn btn-primary">
</form>
</div>
{% endblock %}
<!--#account앱 내부에있는 create라는 라우터를로 연결해라 (view)-->
<!--#어떤 모든뷰에서 라우트를 다 어떤 라우팅으로 가라는 형식으로 이용하면 코딩하는데 가독성이 높아진다. 일원화-->
'Computer Science > Django_pinterest' 카테고리의 다른 글
210820 장고 23강 Bootstrap 을 이용한 Form 디자인 정리 (0) | 2021.08.21 |
---|---|
210820 장고 22강 Login / Logout 구현 (0) | 2021.08.20 |
210819 장고 20강 CRUD 소개 (0) | 2021.08.19 |
210819 장고 19강 디버깅 (0) | 2021.08.19 |
210819 18강 DB 정보 접근 및 장고 템플릿 내 for loop (0) | 2021.08.19 |