【问题标题】:ValueError at /modifier_produit/1 ModelForm has no model class specified/modifier_produit/1 处的 ValueError ModelForm 没有指定模型类
【发布时间】:2022-02-14 00:29:25
【问题描述】:

这是我的 forms.py :

class stockupdateform(forms.ModelForm):
    class Meta:
    model:stock
    fields =['categorie', 'nom', 'quantite'] 

我的意见.py:

 def list_nom(request):
      title='liste des produits' 
      form =stockSearchForm(request.POST or None)
      queryset=stock.objects.all()
      context= {
     "title":title,
     "queryset":queryset,
     "form":form, 
     }
     if request.method == 'POST':
     queryset = stock.objects.filter(categorie=form['categorie'].value(),
        nom=form['nom'].value()) 
     context= {
     "title":title,
     "queryset":queryset,
     "form":form, 
       }
      return render(request, "list_nom.html",context) 

我的 list_nom.html :

<table class='table'>
   <thead>
    <tr>
    <th>id</th>
    <th>categorie</th>
    <th>produit</th> 
    <th>quantite</th>
     </tr>  
    </thead> 
   {% for instance in queryset %} 
     <tr>
    <td>{{forloop.counter}}</td>
   <td>{{instance.categorie}}</td>
   <td><a href="{% url 'modifier_produit' instance.id  %}">{{instance.nom}} </a> </td> 
    <td>{{instance.quantite}}</td>
   </tr> 
   {% endfor %} 
   </table> 

我有这个问题我能做什么: /modifier_produit/1 处的 ValueError ModelForm 没有指定模型类。

【问题讨论】:

    标签: python html django view


    【解决方案1】:

    您不使用: 设置变量,而是使用=。假设您在models 中有stock 作为一个类,您必须将您的stockupdateform 更改为:

    class stockupdateform(forms.ModelForm):
        class Meta:
            model = stock
            fields = ['categorie', 'nom', 'quantite'] 
    

    PS 请使用与您的代码完全相同的缩进共享代码。以这种方式分享它很重要,因为我们可能认为您的缩进不正确(此代码将 100% 导致它下降)。

    【讨论】:

    • 非常感谢它现在可以使用
    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 2012-02-19
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多