2

In my settings.py there are some custom settings, a few data structures that I use. These are rarely changed. I've noticed that changing them from a view wont work. How can I force django to reload the settings file at runtime ? So far the only way that works is to restart the apache web server. But I want to avoid that.

user3599803
  • 6,435
  • 17
  • 69
  • 130
  • possible duplicate of [Changing Django settings at runtime](http://stackoverflow.com/questions/6528723/changing-django-settings-at-runtime) – rnevius May 01 '15 at 08:35
  • But no answer in this post. – user3599803 May 01 '15 at 08:37
  • 1
    You shouldn't be trying to change your settings at runtime. You should reconsider what you're doing and the architecture of your project. If you want to post what you're attempting to do, maybe we can help figure out a cleaner solution. – schillingt May 01 '15 at 14:15

3 Answers3

1

In my code. I needed to kindof reinvent the @login_required decorator for a protected (admin only restricted) page. This is what I did. In the project/settings.py I created a variable called ACCESS_CP = False In the app/views.py I imported

from django.conf import settings

In the def login_form

# (if the user is authenticated and the user is an Active Directory Domain Admin)
settings.ACCESS_CP = True

In the def ControlPanel:

 if request.method == "GET" and settings.ACCESS_CP == False:
      return redirect('login')
 elif request.method == "GET" and settings.ACCESS_CP == True:
      settings.ACCESS_CP = False
      return render(request, 'controlpanel.html')
0

For your application you can store configuration parameters in redis or django model.

Here is the helpful reusable app for that https://github.com/jezdez/django-constance

Also you can check other existing solutions for django configuration management:

https://www.djangopackages.com/grids/g/configuration/

kmmbvnr
  • 5,863
  • 4
  • 35
  • 44
0

Import in view.py

import django.conf as conf

def home(request):

conf.settings.SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
conf.settings.DATABASES['default']['NAME'] = 'testdb'
Community
  • 1
  • 1
jahurul25
  • 159
  • 4
  • 1
    HI, you can set it like this, but the program is not executed, restart the program, it will return to the old value, I have been troubled how to solve this problem – Kevin Jul 03 '20 at 16:55