Post方法未更新数据库Django

2024-07-01 08:10:22 发布

您现在位置:Python中文网/ 问答频道 /正文

Post方法未更新数据库 大家好!我是Django和python的新手,我真的需要你的帮助。 我需要修改Anomalie类实例的一个属性(状态)。我用表格来做这件事。问题是当我“提交”更新它时,我的数据库中没有任何内容。我遵循一步一步的教程,但在我的情况下是不工作的

这是我的模型课:

class Anomalie (models.Model):
    ANOMALIE = (
        ("Etiquette absente", "Etiquette absente"),
        ("Etiquette decalee", "Etiquette decalee"),
        ("Etiquette inconnue", "Etiquette inconnue"),
    )

    ANOMALIE_STATE = (
        ("traité", "traité"),
        ("mise à jour", "mise à jour"),
        ("signalé", "signalé"),


    )
    type = models.CharField(
        max_length=200, choices=ANOMALIE, null=False)
    date_report = models.DateTimeField(null=False, blank=False)

  
    localization = models.TextField(max_length=30, null=False, blank=False)
    state = models.CharField(
        max_length=200, choices=ANOMALIE_STATE, null=False)
  
    aisle = models.ForeignKey(Aisle, null=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(
        Product, null=True, on_delete=models.SET_NULL)
   

    def datepublished(self):
        return self.date_report.strftime('%B %d %Y')

    def __str__(self):
        return self.type

这是view.py

def treter_anomalie(request, pk):
    anomalie_pk = Anomalie.objects.get(id=pk)

    if request.method == "POST":
        anomalie_pk.state = 'traité'
        return redirect('/')

    context = {'anomalie_pk': anomalie_pk}

    return render(request, 'anomalie/treter_anomalie.html', context)

这是treter_anomalie.html

{% extends 'base.html'%} {% load static %} {%block content%} <div id="layoutSidenav"> <div id="layoutSidenav_content"> <main> <div class="container-fluid"> <P>treter anomalie {{ anomalie_pk.id }}</P> <form action="{% url 'treter_anomalie' anomalie_pk.id %}" method="POST">{% csrf_token %} <a href="{% url 'fix_anomalie_stock' anomalie_pk.id anomalie_pk.type %}"> cancel</a> <input type="submit" name="confirm"> </form> </div> </main> </div> </div> {%endblock content%}

这是url.py

from anomalie.views import(
    consult_anomalie,
    fix_anomalie_stock,
    update_location_view,
    treter_anomalie,

)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_screen_view, name="home"),
    path('consult/<str:pk>/', consult_anomalie, name="consult_anomalie"),
    path('fix_anomalie_stock/<str:pk>/<str:type_anomalie>',
         fix_anomalie_stock, name="fix_anomalie_stock"),
   
]

因此,当我运行代码时,它会返回主页(在那里我有异常列表),当我查看特定的异常时,我看不到任何更改。 另外,y尝试了anomalie_pk.delete,因为可能with choices不同,但它没有被删除

我在cmd中没有看到错误。我真的不知道发生了什么,所以我非常感谢你的帮助


Tags: selfdividfalsereturnmodelstypestock
1条回答
网友
1楼 · 发布于 2024-07-01 08:10:22

为了保存对象,您需要调用save方法

def treter_anomalie(request, pk):
anomalie_pk = Anomalie.objects.get(id=pk)

if request.method == "POST":
    anomalie_pk.state = 'traité'
    anomalie_pk.save() # this will save the object
    return redirect('/')

context = {'anomalie_pk': anomalie_pk}

return render(request, 'anomalie/treter_anomalie.html', context)

但是POST不用于更新对象。可以创建单独的视图来更新对象

编辑

创建一个单独的视图

在闭包检查时,我看到treter_anomalie仅用于更新对象,即不用于新对象。如果是这种情况,则不需要创建单独的方法(视图)。您只需要将表单上的method属性更改为PUT,并将if语句更改为request.method == "PUT"

POST用于创建

正如我提到的,POST用于创建操作而不是更新操作。但是在实践中,我看到很多次在不同的项目中使用POST来创建和更新

相关问题 更多 >

    热门问题