Database data is not updating on my apache2 website unless I restart apache2 service
I have Django installed and am using Apache2 to host the website (using WSGI). I have checkboxes on my website and when I click a checkbox and then click submit it saves the change to the SQLite3 database and refreshes my website. If I log out of my website and then back in the checkbox is no longer clicked but the related database item is showing TRUE in the Django admin site. If I restart Apache2 using "sudo service apache2 restart" and refresh the website it then has the proper boxes checked. I am brand new to Django/Apache2 so I apologize if this is a stupid mistake, but restarting apache2 every time I make a change seems wrong.
my views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import RelayList
from .forms import RelayControl
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def index(response):
curList = RelayList.objects.get(id=1) #retrieves the database object and places it into a variable
if response.method == "POST":
form = RelayControl(response.POST)
if form.is_valid():
curList.relay1 = form.cleaned_data["relay1"]
curList.relay2 = form.cleaned_data["relay2"]
curList.relay3 = form.cleaned_data["relay3"]
curList.save()
else:
form = RelayControl() # creates an instance of the for defined in class RelayControl in forms.py
#return HttpResponse('<h1>Hello World, from index in views.py</h1>')
#"pageHeader" is the variable it looks for in the html file and places whatever is after the ":" in its spot
return render(response, "power_relay/home.html", {"pageHeader":"Power Relay Controls", "controlForm":form, "curList":curList})
my main site HTML
{% extends 'power_relay/base.html' %}
{% block title %}
Power Relay Site- Controls
{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{controlForm.as_p}}
<button type="submit", name="saveChanges">Save Changes</button>
</form>
<hr><br><br>
<a href="{% url 'settings'%}">Settings</a>
{% endblock %}
my models.py
from django.db import models
class RelayList(models.Model):
name = models.CharField(max_length=20)
relay1 = models.BooleanField(verbose_name="Relay 1")
relay2 = models.BooleanField(verbose_name="Relay 2")
relay3 = models.BooleanField(verbose_name="Relay 3")
#relaylist = models.ForeignKey(RelayList, on_delete=models.CASCADE)
#relayName = models.CharField(max_length=300)
#onOff = models.BooleanField()
def __str__(self):
return self.name
my forms.py
from django import forms
from .models import RelayList
class RelayControl(forms.Form):
curList = RelayList.objects.get(id=1) #retrieves the relay list from database
relay1 = forms.BooleanField(label="Relay 1", required=False, initial=curList.relay1)
relay2 = forms.BooleanField(label="Relay 2", required=False, initial=curList.relay2)
relay3 = forms.BooleanField(label="Relay 3", required=False, initial=curList.relay3)
The culprit:
class RelayControl(forms.Form):
curList = RelayList.objects.get(id=1) #retrieves the relay list from database
Since you declare this in the form class body, the initial values for your form are set when your forms.py is imported (here's a short explanation)- i.e. when the wsgi application starts. The values will remain fixed until the application re-imports forms.py.
A better, more reusable approach, is to use a ModelForm. In the view, you then pass the model instance (here: curList = RelayList.objects.get(id=1)
) to it and django figures out the rest for you.