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.
*EDIT- I have changed my code to use a modelform (I think I did it properly)as recommended by the first commenter, but I am having the same problem.
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, instance=curList)
if form.is_valid():
form.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})
#@login_required
#def modify_model(response):
@login_required
def settings(response):
return render(response, "power_relay/settings.html", {"pageHeader":"Power Relay Settings"})
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.ModelForm):
class Meta:
model = RelayList
fields = ['relay1', 'relay2', 'relay3']
CodePudding user response:
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.