/hello_模板的TypeError“module”对象不是callab

2024-03-29 09:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

出现以下错误-
TypeError at/hello\u template“module”对象不可调用

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 

你好.html

^{pr2}$

视图.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)

设置.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',
)

网址.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'),
]

Tags: djangofromhttpsimportdocshellolibcontext
2条回答

django.template.context中导入的context是一个不可调用的模块。在

您正在寻找Context

from django.template import Context

然后试着这样做:

^{pr2}$

问题是您试图实例化模块的一个对象context,而不是Context(注意大写字母)。在

这样做:

from django.template import Context

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

文档中还有一个example。在

相关问题 更多 >