【问题标题】:Database data is not updating on my apache2 website unless I restart apache2 service *EDIT除非我重新启动 apache2 服务,否则数据库数据不会在我的 apache2 网站上更新 *编辑
【发布时间】:2022-01-18 08:08:03
【问题描述】:

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

*EDIT- 我已按照第一位评论者的建议更改了我的代码以使用模型表单(我认为我做得正确),但我遇到了同样的问题。

我的意见.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

我的表单.py

from django import forms
from .models import RelayList


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

【问题讨论】:

    标签: python django sqlite apache2


    【解决方案1】:

    罪魁祸首:

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

    由于您在表单类主体中声明了这一点,因此在导入 forms.py (here's a short explanation) 时设置表单的初始值 - 即当 wsgi 应用程序启动时。在应用程序重新导入 forms.py 之前,这些值将保持固定。


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

    【讨论】:

    • 非常感谢您的回复和建议。我需要一些时间来实施。我对 Stack Overflow 有点陌生。我应该将此标记为已回答还是等到我实施更改并验证它是否正常工作?
    • 先看看这是否解决了您的问题,然后您可以将其标记为已回答。
    • 我认为我正确地实现了它,但我遇到了同样的问题。
    • GET 请求的响应形式也需要模型实例:else: form = RelayControl(instance=curList)。现在,如果您希望能够编辑的不仅仅是一个 Relay (id=1),您需要创建单独的视图来创建和编辑,并相应地设置 url 配置:docs.djangoproject.com/en/3.2/intro/tutorial03/…
    • 我应该明白这一点,谢谢。现在一切正常。非常感谢。
    猜你喜欢
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2021-02-26
    • 2014-08-14
    • 2016-04-13
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多