新手Django选择菜单'功能'对象不可迭代

时间:2013-04-30 11:22:26

标签: python django object menu pydev

当我访问我的网址时,我只能获得:<django.forms.fields.ChoiceField object at 0x026D1CD0> 我想要号码Choicefield

我的应用程序中有以下models.py

from django.db import models
from django.forms import ModelForm

class Zonas(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
    return self.name

class ZonasForm(ModelForm):
    class Meta:
       model = Zonas

我想问题出在我的views.py

from django    import forms
from testApp.models import Zonas
from django.shortcuts import render_to_response

def menuForm (request):
    z = list (Zonas.objects.all())
    numbers = forms.ChoiceField(z)
    return render_to_response('hello.html', {'numbers':numbers})

menuForm在我的urls.py上用作:

from django.conf.urls import patterns, include, url
from testApp.views import menuForm
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',

url(r'^hello/$', menuForm ),
)

我的html页面,尽可能简单:

<html>
<body>

{{numbers}}

</body>
</html>

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

您的网页网址不正确,请将其更改为

url(r'^hello/$', menuForm),

doc中所述,其形式为

  

(正则表达式,Python回调函数[,optional_dictionary [,optional_name]])

另外,将视图函数的定义更改为

def menuForm (request):
....