[Django] TypeError object not iterable

Submitted 3 years, 8 months ago
Ticket #96
Views 2855
Language/Framework Python
Priority Urgent
Status Closed

Hi i have found an issue while developing my webapp

So i tried to render my template called marketingup.html and it showed me an issue i've never seen

My model:

class MarketingUp(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    description = models.TextField(max_length=200, null=True)
    def __str__(self):
        return f'{self.name}'

   My form:

class MarketingUp(forms.ModelForm):
    
    class Meta:
        model = MarketingUp
        fields = ['user','name','description']

My view:

@login_required
def add_upsell(request):
    context={}
    if request.method == 'POST':
        form = MarketingUp(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, f'Offer created successfully')
            return redirect('marketing')
    context['form'] = MarketingUp
    return render(request, 'users/marketingup.html', context)

My template (HTML):

{% extends "homepage/base.html" %} {% load crispy_forms_tags %}
<!---->
{% block body %}

<body>
    {% endblock %}
    <!---->
    {% block content %}
    <!---->

    <div class="content-section">
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend align="center" class="border-bottom mb-3"><strong>Add New UpSell Offer</strong></legend>
                {{form|crispy}}


            </fieldset>

            <div class="form_group">
                <button align="center" class="btn btn-outline-info" type="submit">Create</button>
            </div>

        </form>
    </div>
    <!---->
    {% endblock %}

And the error is on this link more detailed

https://prnt.sc/tuspas

TypeError at /add_upssell/
'MarketingUp' object is not iterable
Request Method:	GET
Request URL:	http://127.0.0.1:8000/add_upssell/
Django Version:	3.0.5
Exception Type:	TypeError
Exception Value:	
'MarketingUp' object is not iterable
Exception Location:	C:\Users\a39548\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\defaulttags.py in render, line 165
Python Executable:	C:\Users\a39548\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version:	3.8.2
Python Path:	
['C:\\Users\\a39548\\Documents\\GitHub\\ProjetoFinal\\24_07_2020\\UpCross',
 'C:\\Users\\a39548\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
 'C:\\Users\\a39548\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
 'C:\\Users\\a39548\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
 'C:\\Users\\a39548\\AppData\\Local\\Programs\\Python\\Python38-32',
 'C:\\Users\\a39548\\AppData\\Roaming\\Python\\Python38\\site-packages',
 'C:\\Users\\a39548\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time:	Thu, 6 Aug 2020 02:10:03 +0000
Error during template rendering
In template C:\Users\a39548\AppData\Local\Programs\Python\Python38-32\lib\site-packages\crispy_forms\templates\bootstrap4\uni_form.html, error at line 8

'MarketingUp' object is not iterable
1	{% load crispy_forms_utils %}
2	
3	{% specialspaceless %}
4	    {% if include_media %}{{ form.media }}{% endif %}
5	    {% if form_show_errors %}
6	        {% include "bootstrap4/errors.html" %}
7	    {% endif %}
8	    {% for field in form %}
9	        {% include field_template %}
10	    {% endfor %}
11	{% endspecialspaceless %}
12	

Submitted on Aug 07, 20
add a comment

1 Answer

Verified

@ Brunoseixas at high level looks like your FORM is not rendering properly on GET request. I think it because its not initialized properly. Can you try this out,

@login_required
def add_upsell(request):
    context={}
    if request.method == 'POST':
        form = MarketingUp(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, f'Offer created successfully')
            return redirect('marketing')
    #scott 
    form = MarketingUp()
    context['form'] = form
    return render(request, 'users/marketingup.html', context)

Submitted 3 years, 8 months ago

It shows the same error

- BrunoSeixas 3 years, 8 months ago

Resolved was a dummie error cuz it was confunding with the models.py...

same name as form and wasnt importing the form into the views.py

- BrunoSeixas 3 years, 8 months ago

ah gotcha. Hmm I didnt realize that . Nice, thanks for letting me know otherwise I thought to debug the issue.

- scott 3 years, 8 months ago


Latest Blogs