type object has no attribute error in django after form submission

Héléna

I got the error "type object 'Input' has no attribute 'company', but in the model.py, it is indeed there, so there must be some other errors that I didn't see. Your any help is really appreciated.

input- models.py

class Input(models.Model):

    company=models.CharField(max_length=100,default='Empty')
    region=models.CharField(max_length=100)
    start_date=models.DateField(auto_now=False, auto_now_add=False)

    def __unicode__(self):
        return self.company

forms.py

class InputForm(forms.ModelForm):
    company=forms.CharField(required=True)
    regionlist = forms.ModelChoiceField(queryset=Dupont.objects.values('region').distinct())
    start_date=forms.DateField(widget=DateInput(),required=True)

    class Meta:
        model = Input
        fields = ('company', 'region','start_date')
        widgets = {
            'start_date': forms.DateInput(attrs={'class':'datepicker'}),
            'end_date': forms.DateInput(attrs={'class':'datepicker'}),
        }

views.py

def input(request):

    if request.method == 'POST':
        form = InputForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            print 'is valid'
            company = form.cleaned_data['company']
            region = form.cleaned_data['region']
            start_date= form.cleaned_data['start_date']
            form.save()

            return redirect('result')

        else:
            return render_to_response('input.html',{'form': form},context_instance=RequestContext(request))

    else:
        form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
        return render_to_response('input.html',{'form': form})

html

        <form method="post" action="{% url 'result' %}">
        {% csrf_token %}

        <!--enter company name--> 
        <div class="field">
            <p>Company:<input type="text" name="company" value="{{company}}"/>
        </div>

        <!--select region from drop down list-->
        <div class="field" >
            <label> Select the Region:
            {{ form.regionlist }}
                {% for region in form.regionlist.choices %}
                     <option value="{{ val }}" name= "region" {% ifequal data.val val %}selected {% endifequal %}></option>
                {% endfor %}
            </label>
        </div>

        <!--select start date from drop down list-->
        <label for="startDate">Start Month:</label>
        <input name="start_date" id="start_date" class="date-picker"/>

        <!--submit-->
        <div class="fieldWrapper">
        <p><input type="submit" value="Submit" /></p></div>                   
 </form>    

trackback

Traceback:
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "C:\Users\user\Desktop\SCOR\inputform\views.py" in input
  34.         form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\forms\models.py" in __init__
  320.             object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\forms\models.py" in model_to_dict
  153.             data[f.name] = f.value_from_object(instance)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fields\__init__.py" in value_from_object
  918.         return getattr(obj, self.attname)

Exception Type: AttributeError at /input
Exception Value: type object 'Input' has no attribute 'company'
bruno desthuilliers

In this line:

form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)

you're passing the Input class as instance, instead of an Input instance. To pass an instance you'd need to call the class, ie:

form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input())

but that's totally useless here since your ModelForm already knows which model class to instanciate if you don't provide an instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django: "Form object has no attribute..." error

From Dev

Django form: object has no attribute

From Dev

Django form wizard 'NoneType' object has no attribute 'rsplit' error

From Dev

Django RESTful API error "type object 'User' has no attribute '_meta'"

From Dev

Python Attribute Error: type object has no attribute

From Dev

Django Attribute Error: object has not attribute 'forms'

From Dev

Missing Variable Error in Django Form After Form Submission

From Dev

Django AttributeError: form object has no attribute x

From Dev

AttributeError: type object has no attribute for a django model

From Dev

Django Test: type object has no attribute 'objects'

From Dev

None type object has no attribute django tenants

From Dev

Error: type object 'Keys' has no attribute 'chord'

From Dev

Error: Type object has no attribute 'Query'

From Dev

'module' object has no attribute 'views' django error

From Dev

django 'User' object has no attribute 'get' error

From Dev

'function' object has no attribute 'get' error in django

From Dev

'function' object has no attribute 'get' error in django

From Dev

Django error - 'function' object has no attribute 'MyForm'

From Dev

Kivy error: Attribute error:'None type' object has no attribute 'text'

From Dev

Get following error when creating user in Django: type object 'User' has no attribute 'objects'

From Dev

Django Attribute Error: 'function' object has no attribute 'is_usable'

From Dev

Python/DJango Attribute error : Model object has not attribute objects

From Dev

Django form saving issue AttributeError 'tuple' object has no attribute 'get'

From Dev

Django 'NoneType' object has no attribute '__getitem__' with Form Save

From Dev

Django form saving issue AttributeError 'tuple' object has no attribute 'get'

From Dev

Django 'File' object has no attribute '_size' on Change Form in Admin

From Dev

Django bootstrap modal after form submission

From Dev

Not able to redirect a Django form after submission

From Dev

Modify data after Django form submission

Related Related

  1. 1

    Django: "Form object has no attribute..." error

  2. 2

    Django form: object has no attribute

  3. 3

    Django form wizard 'NoneType' object has no attribute 'rsplit' error

  4. 4

    Django RESTful API error "type object 'User' has no attribute '_meta'"

  5. 5

    Python Attribute Error: type object has no attribute

  6. 6

    Django Attribute Error: object has not attribute 'forms'

  7. 7

    Missing Variable Error in Django Form After Form Submission

  8. 8

    Django AttributeError: form object has no attribute x

  9. 9

    AttributeError: type object has no attribute for a django model

  10. 10

    Django Test: type object has no attribute 'objects'

  11. 11

    None type object has no attribute django tenants

  12. 12

    Error: type object 'Keys' has no attribute 'chord'

  13. 13

    Error: Type object has no attribute 'Query'

  14. 14

    'module' object has no attribute 'views' django error

  15. 15

    django 'User' object has no attribute 'get' error

  16. 16

    'function' object has no attribute 'get' error in django

  17. 17

    'function' object has no attribute 'get' error in django

  18. 18

    Django error - 'function' object has no attribute 'MyForm'

  19. 19

    Kivy error: Attribute error:'None type' object has no attribute 'text'

  20. 20

    Get following error when creating user in Django: type object 'User' has no attribute 'objects'

  21. 21

    Django Attribute Error: 'function' object has no attribute 'is_usable'

  22. 22

    Python/DJango Attribute error : Model object has not attribute objects

  23. 23

    Django form saving issue AttributeError 'tuple' object has no attribute 'get'

  24. 24

    Django 'NoneType' object has no attribute '__getitem__' with Form Save

  25. 25

    Django form saving issue AttributeError 'tuple' object has no attribute 'get'

  26. 26

    Django 'File' object has no attribute '_size' on Change Form in Admin

  27. 27

    Django bootstrap modal after form submission

  28. 28

    Not able to redirect a Django form after submission

  29. 29

    Modify data after Django form submission

HotTag

Archive