开发者

How to pass a variable from settings.py to a view?

开发者 https://www.devze.com 2023-01-20 13:01 出处:网络
I\'m getting the project path in my Django app in settings.py using: PROJECT_PATH = os.pa开发者_JS百科th.realpath(os.path.dirname(__file__))

I'm getting the project path in my Django app in settings.py using:

PROJECT_PATH = os.pa开发者_JS百科th.realpath(os.path.dirname(__file__))

I would like to use the PROJECT_PATH value in other views, such as for locating the path to files in my static path. How can I make this into an accessible variable?


Use from django.conf import settings but mind that settings is not a module. The documentation clearly explains that and your use case.


from django.conf import settings as conf_settings
project_path = conf_settings.PROJECT_PATH


  1. in settings.py add

    DOMAIN = "example.com"
    
  2. views.py

    from django.conf import settings
    
    DOMAIN = settings.DOMAIN
    
  3. lets try to output it:

    print(DOMAIN)
    
    print(type(DOMAIN)
    
  4. output will be:

    example.com
    
    <class 'str'>
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号