Django - (1)

이정인·2023년 5월 10일
0

Django_기말

목록 보기
1/11
post-thumbnail


# view.py

class AccountCreateView(CreateView):

    model = User
    
    form_class = UserCreationForm
    
    success_url = reverse_lazy('accountapp:hello_world')
    # 계정 만들기에 성공했다면 어느 경로로 다시 연결 할 것인가
    
    template_name = 'accountapp/create.html'
    # 어떤 html 파일을 이용해 볼 것인지
def hello_world(request):

    if request.method == "POST":

        ...

        return HttpResponseRedirect(reverse('accountapp:hello_world'))

    else:
        ...

# reverse / reverse_lazy 차이점

  • 함수(def)형 redirect : reverse( ) / 클래스(class) : reverse_lazy( )
  • reverse / reverse_lazy 차이점
    -> 큰 차이는 없다. 함수와 클래스가 파이썬에서 불러와지는 방식의 차이가 존재하는데, class에서 reverse를 그대로 사용할 수 없기 때문에 reverse_lazy를 사용한다.
  • class 에서 reverse를 사용하면 오류 발생함.

# urls.py

urlpatterns = [

	# 함수(def)형
    path('hello_world/', hello_world, name='hello_world'),
	
    # 클래스(class) 형
    path('create/', AccountCreateView.as_view(), name='create')
    
]

# create.html

{% extends 'base.html' %}

{% block content %}

  <div>
	
    # action에는 요청을 보내는 url을 넣어준다.
    <form action="{% url 'accountapp:create' %}" method="post">

      <input type="submit" class="btn btn-primary">

    </form>

  </div>

{% endblock %}



profile
둉이닝

0개의 댓글

관련 채용 정보