views.py에서 현재 실행중인 app 정보와 view class경로, 물리파일경로 등을 참조하는 방법

 

views.py

from typing import Any, Dict

import post.urls as urls
from django.apps import apps
from django.views.generic.base import TemplateView

# Create your views here.
class DetailView(TemplateView):
    template_name = "detail.html"
    _appConfig = apps.get_app_config(__package__)

    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        _context = super().get_context_data(**kwargs)

        print(f'__name__ : {__name__},\n__file__: {__file__},\n__package__: {repr(__package__)},\napps.name : {self._appConfig.name},\nurls.app_name : {urls.app_name}')

        return _context

...

 

출력결과

__name__ : post.views,
__file__: C:\Work\Software\python_project\blog.test.com\post\views.py,
__package__: 'post',
apps.name : post,
urls.app_name : post