Asked 2 years ago by OrbitalVoyager529
How can I permanently update Django settings using views and forms?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 2 years ago by OrbitalVoyager529
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi there,
I am trying to update a variable defined in settings.py through the front-end using a combination of views and forms, and I need the change to be permanent (i.e. persist across server restarts).
For example, in my settings.py I have:
PYTHON# My personal settings NO_OF_DAYS = 0
In my forms.py I have the following form:
PYTHONdef class SettingsForm(forms.Form): no_of_days = forms.CharField(max_length=254)
My views.py contains an update view similar to this:
PYTHONfrom django.conf import settings from .forms import SettingsForm class UpdateSettings(View): def get(self, request): settings_form = SettingsForm() return render(request, "settings-template.html", {"form": settings_form}) def post(self, request): settings_form = SettingsForm(request.POST) if settings_form.is_valid(): form_data = settings_form.cleaned_data settings.NO_OF_DAYS = int(form_data["no_of_days"]) # I want this change to be stored permanently return redirect("/success/")
The issue is that updating the settings variable in this way only affects the runtime variable, not the actual settings.py file. How can I achieve a permanent update to settings.py using the data from a form submission?
Comments 0
•Answers 0
•Views 2.4k
No comments yet.