상세 컨텐츠

본문 제목

Django URL with View

Django

by BenzhaminKim 2020. 10. 29. 12:48

본문

views.py

There are two kinds of ways to make redirection.

 

1. Function based view

2. Class based view

 

1. Functiom based View

 

First, you need to make a function having request as the first parameter, and I put *args and **kwargs just in case.

urls.py

The function is going to connect with urls.py as a view.

 

2. Class based view

class KirrRedirectView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse("Hello again")

Class based view has inheritance of View from django.views

And you need to declare 'get' method which also has request.

The get method is for 'GET' request.

urls.py

One thing you need to keep in mind is as_view(). When you use class based view, you need to use as_view() method to connect the class with URL.

'Django' 카테고리의 다른 글

Django with Docker  (0) 2021.01.15
Relationship between URLS and VIEWS  (0) 2020.10.29
Set Global Variable using settings.py  (0) 2020.10.29
Django Architecture  (0) 2020.10.29

관련글 더보기

댓글 영역