属性错误:模块'django.forms.forms没有属性'ModelForm'

时间:2019-06-27 06:16:41

标签: python

from django.forms import forms
from .models import StudentModel


class StudentForm(forms.ModelForm):
    name = forms.CharField(label='Name', max_length=200, widget=forms.TextInput(attrs={
      'class': 'form-control',
       'placeholder' : 'Enter Name Here '
    }))

    age = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Age '
    }))

    address = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Address '
    }))

    email = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter  Email Here '
    }))

    pin = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter Pin Here '
    }))

    mob = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter Mobile Number Here '
    }))

    class Meta():
        model = StudentModel
        fields = ['name', 'age', 'address', 'email', 'pin', 'mob']

3 个答案:

答案 0 :(得分:0)

应为from django.forms import ModelForm,然后您可以使用class StudentForm(ModelForm):from django import forms

答案 1 :(得分:0)

当我收到“ AttributeError:模块'django.forms.forms'没有属性'ModelForm'“时,我检查了django模块并更改了以下字段。 当我第一次检查模块时,就像这样:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div id="app">
  <product-counter v-bind:quantity="item.quan" v-on:input="item.quan=$event" v-on:increment-quantity="item.quan++" v-on:decrement-quantity="item.quan--"></product-counter>
</div>

=> form.ModelForm问题已解决

答案 2 :(得分:0)

from django.forms import ModelForm

class SampleForm(forms.ModelForm):

class Meta: 
    model = SampleModel
    fields = ['name']

from django.forms import forms 更改为 from django.forms import ModelForm,这应该可以工作。