본문 바로가기

학습/[The-Origin][SW][Backend] Main Pag

[The Origin][VSCODE][Django] 화면 Data 출력 방식 (Text, HTML, JSON), Data 받기 (주소, Query parameter)

반응형

Django의 Data 출력 방식 (text, HTML, JSON)

1. Test를 위한 url 추가

2-1. view를 통한 화면에 문자 출력

    - 실행 후 확인

2-2. view에서 HTML을 통한 화면에 문자 출력 

    - 실행 후 확인

2-3. view에서 JSON 방식 화면에 문자 출력 

  - 실행 후 확인


View에서 Code에서 HTML Tag 형식으로 볼 수 있는 이유

from django.http import HttpResponse

class HttpResponse(HttpResponseBase):
    """
    An HTTP response class with a string as content.

    This content can be read, appended to, or replaced.
    """

    streaming = False

    def __init__(self, content=b"", *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Content is a bytestring. See the `content` property methods.
        self.content = content​
 

super().__init__(*args, **kwargs)

    def __init__(
        self, content_type=None, status=None, reason=None, charset=None, headers=None
    ):
        self.headers = ResponseHeaders(headers or {})
        self._charset = charset
        if content_type and "Content-Type" in self.headers:
            raise ValueError(
                "'headers' must not contain 'Content-Type' when the "
                "'content_type' parameter is provided."
            )
        if "Content-Type" not in self.headers:
            if content_type is None:
                content_type = "text/html; charset=%s" % self.charset
            self.headers["Content-Type"] = content_type
        self._resource_closers = []
        # This parameter is set by the handler. It's necessary to preserve the
        # historical behavior of request_finished.
        self._handler_class = None
        self.cookies = SimpleCookie()
        self.closed = False
        if status is not None:
            try:
                self.status_code = int(status)
            except (ValueError, TypeError):
                raise TypeError("HTTP status code must be an integer.")

            if not 100 <= self.status_code <= 599:
                raise ValueError("HTTP status code must be an integer from 100 to 599.")
        self._reason_phrase = reason​
 
content_type = "text/html; charset=%s" % self.charset

  ※ Type에 대한 지정이 별도로 없으면 html로 타입이 된다.


데이터 받기

예시 (네이버에 Django 검색)

  - 주소를 통해서 다양한 데이터를 볼 수 있음

경로로 데이터 받기

  - View에 Print를 통한 Data 확인하기

  - 실행 후 확인

 

 

Query 파라미터로 데이터 받기

  - View에 Print를 통한 Data 확인하기

  - 실행 후 확인


int도 가능

 

터미널 log 보기 쉽게 바꾸기


 

반응형