2013-04-29 25 views
0

我的意愿是让一个选择菜单,如下因素选择:没有发现模块的Django模型要求

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

hello.html显示。不过,我不断收到此错误:ImportError: No module named hello

对象: #continuation 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', 
) 

A呃帮助表示赞赏。

回答

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) 
+0

它采空给出错误,当我验证,但是当我运行服务器,并尝试接取它返回的意见导入错误的hello.html的.py in:from testApp.models.hello import title' – ClaudioA 2013-04-29 02:23:42

+0

异常类型:\t导入错误 异常值:\t 没有名为hello的模块 – ClaudioA 2013-04-29 02:23:58

+0

也删除该行。 'hello'是一个类,不是一个模块,你不能从中导入任何东西。 'title'是hello类中的一个字段。您不会导入它,您可以从hello类中导入'hello'并访问'title'。 – 2013-04-29 02:40:10

相关问题