/ hello_template中的TypeError - ' module'对象不可调用

时间:2015-08-12 16:02:01

标签: python django pycharm

得到以下错误 -
/ hello_template'模块'对象不可调用

Request Method: GET        
Request URL:    http://localhost:8000/hello_template    
Django Version: 1.8.3     
Exception Type: TypeError    
Exception Value: 'module' object is not callable   
Exception Location: C:\Users\Saket\PycharmProjects\newTut\articleapp\views.py in hello_template, line 11     
Python Executable:  C:\Python27\python.exe     
Python Version: 2.7.3      
Python Path:
['C:\\Users\\Saket\\PycharmProjects\\newTut',     
 'C:\\Python27\\lib\\site-packages\\setuptools-5.7-py2.7.egg',     
 'C:\\Python27\\lib\\site-packages\\pip-1.5.6-py2.7.egg',       
 'C:\\Users\\Saket\\PycharmProjects\\newTut',         
 'C:\\Python27\\python27.zip',          
 'C:\\Python27\\DLLs',                
 'C:\\Python27\\lib',                
 'C:\\Python27\\lib\\plat-win',                
 'C:\\Python27\\lib\\lib-tk',                
 'C:\\Python27',                
 'C:\\Python27\\lib\\site-packages']                
Server time:    Wed, 12 Aug 2015 21:22:03 +0530 

hello.html的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>
    Hello! {{ name }}
    this template seems to have worked!
</body>
</html>

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import context


# Create your views here.
def hello_template(request):
    name = 'Saket'
    t = get_template('hello.html')
    html = t.render(context({'name': name}))
    return HttpResponse(html)

settings.py

"""
Django settings for newTut project.

Generated by 'django-admin startproject' using Django 1.8.3.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3g)2$98_6@)m9_s__)t2b1v&r2ul4709taay%$f$d0ez%e*tjb'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'articleapp',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'newTut.urls'


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates/pages",],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'newTut.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
    #'C:\Users\Saket\PycharmProjects\newTut\templates\pages',
)

urls.py

""newTut URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello', 'articleapp.views.hello_template'),
]

2 个答案:

答案 0 :(得分:4)

问题是您尝试实例化模块 context的对象而不是 Context(请注意资本)。

这样做:

from django.template import Context

html = t.render(Context({'name': name}))

文档中还有example

答案 1 :(得分:3)

context中导入的django.template.context是一个无法调用的模块。

您正在寻找Context课程

from django.template import Context

然后尝试这样:

...
html = t.render(Context({'name': name}))
...