0

I am saving a list of forms as a variable in the view that I am sending to the template.

When I iterate the list of forms in the template, it gives me the error

AttributeError: 'long' object has no attribute 'get'

I've tried originally to save the forms in a dictionary, but got the same error. I can iterate through a queryset that I'm passing to the template, but the list, or dictionary, of forms cannot seem to be iterated.

Is there any solution for this?

Here's my relevant code:

forms.py

from django.contrib.auth.models import User
from django import forms
from apps.account.models import UserProfile

class StaffUserTypeForm(forms.Form):
    user_type = forms.ChoiceField(choices=UserProfile.STAFF_CHOICES)

account/models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    DEFAULT = 0
    ADMIN = 1
    MANAGER = 2
    COORDINATOR = 3
    REALTOR = 4
    TEAM_CAPTAIN = 5
    PROPERTY_OWNER = 6
    PRELOAD = 7
    BILLING = 8
    COORDINATOR_PRELOAD = 9

    STAFF_CHOICES = (
        (ADMIN, 'Admin'),
        (MANAGER, 'Manager'),
        (COORDINATOR, 'Coordinator'),
        (COORDINATOR_PRELOAD, 'Coordinator +Preload'),
        (PRELOAD, 'Preloader'),
        (BILLING, 'Billing'),
    )
    ...

views.py

from forms import *
    ...
    staff = Staff.objects.all()
    roles = []
    for member in staff:
        form = StaffUserTypeForm(initial=member.user.userprofile.user_type)
        roles.append(form)
    context.update({'staff':staff,'roles':roles})
    ...

template (This is where it breaks)

{% for role in roles %}
{{role}}
{% endfor %}

But this works fine:

{% for member in staff %}
{{member.user.first_name}}
{% endfor %}

Update

That particular error is because I didn't specify user_type in initial for the form.

Should be:

form = StaffUserTypeForm(initial={'user_type':member.user.userprofile.user_type})

However, I still have the problem of spitting out the form for each staffmember, because I can't reference a variable as a dictionary key within the template:

if:

roles = {}
for member in staff:
  roles[member] = StaffUserTypeForm(initial={'user_type':member.user.userprofile.user_type})

I can't get the form for the specific staffmember:

{% for member in staff %}
{{roles.member}}
{% endfor %}

Does not work, and does not throw an error. It just looks, I think, for roles['member'] which doesn't exist.

3
  • You should use a model formset. Also, you should post the full stacktraces when you have an exception. Commented Feb 14, 2013 at 20:44
  • Are you sure that this for with role in roles making error ? Commented Feb 14, 2013 at 20:45
  • Error was that I didn't send a dictionary as the initial value in the form. However, still can't reference the form in the for loop. I believe it's a template problem. Commented Feb 14, 2013 at 20:51

1 Answer 1

1

Since the roles dictionary contains both the members (as keys) and forms (as values), why do you need to iterate through staff at all? Just iterate through roles.

{% for member, form in roles.items %}
    {{ member }} : {{ form }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. I was thinking I had to make a template filter to pass the variable as a key: code.djangoproject.com/ticket/3371. You've saved me some time! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.