除非我重新启动 apache2 服务*编辑,否则数据库数据不会在我的 apache2 网站上更新

Database data is not updating on my apache2 website unless I restart apache2 service *EDIT

我安装了 Django 并使用 Apache2 托管网站(使用 WSGI)。我的网站上有复选框,当我单击一个复选框然后单击提交时,它会将更改保存到 SQLite3 数据库并刷新我的网站。如果我退出我的网站然后返回,则不再单击该复选框,但相关的数据库项目在 Django 管理站点中显示为 TRUE。如果我使用“sudo service apache2 restart”重新启动 Apache2 并刷新网站,它就会选中正确的框。我是 Django/Apache2 的新手,所以如果这是一个愚蠢的错误,我深表歉意,但每次我进行更改时重新启动 apache2 似乎是错误的。

*编辑-我已经按照第一位评论者的建议更改了我的代码以使用模型形式(我认为我做得很好),但我遇到了同样的问题。

我的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"})

我的主站点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 %}

我的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

我的forms.py

from django import forms
from .models import RelayList


class RelayControl(forms.ModelForm):
    class Meta:
        model = RelayList
        fields = ['relay1', 'relay2', 'relay3']

罪魁祸首:

class RelayControl(forms.Form):
        curList = RelayList.objects.get(id=1) #retrieves the relay list from database

由于您在表单 class 正文中声明了此内容,因此在导入 forms.py 时设置表单的初始值 () - 即 wsgi 应用程序启动时.这些值将保持不变,直到应用程序重新导入 forms.py.


更好、更可重用的方法是使用 ModelForm。在视图中,然后将模型实例(此处:curList = RelayList.objects.get(id=1))传递给它,django 会为您计算其余部分。