0

我的意愿是使用以下选项制作一个选择菜单:

models.py
TITLE_CHOICES = (
    ('MR', 'Mr.'),
    ('MRS', 'Mrs.'),
    ('MS', 'Ms.'),
)

并将其显示在hello.html. 但我不断收到此错误:ImportError: No module named hello

对象:#continuation of models.py

class hello(models.Model):
    title = models.CharField(max_length=3, choices=TITLE_CHOICES)

    def __unicode__(self):
        return self.name

view.py 上的请求:

from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
from testApp.models import hello
from testApp.models.hello import title
from django.shortcuts import render_to_response
from django.template import RequestContext


def contato(request):
    form = hello()
    return render_to_response(
       'hello.html',
        locals(),
        context_instance=RequestContext(request),
    )

def hello_template(request):
    t = get_template('hello.html')
    html = t.render(Context({'name' : title}))
    return HttpResponse(html)

我在INSTALLED_APPS( setting.py) 中的应用程序:

INSTALLED_APPS = (
'testApp',
'hello',
)

任何帮助表示赞赏。

4

1 回答 1

0

为什么它hello在您安装的应用程序中?我认为这可能是问题所在。hello是你的models.py一个属于你的类testApptestApp是您唯一必须包含在INSTALLED_APPS.

此外,您也应该从代码中删除这一行:from testApp.models.hello import title这也会产生错误,因为您无法从类中导入字段。如果您需要访问title字段,则必须这样做:

def contato(request):
    form = hello()
    title = form.title
    return render_to_response(
       'hello.html',
        locals(),
        context_instance=RequestContext(request),
    )

def hello_template(request):
    t = get_template('hello.html')
    # see here the initialization and the access to the class field 'title'
    title = hello().title
    html = t.render(Context({'name' : title}))
    return HttpResponse(html)
于 2013-04-29T02:01:07.593 回答