2015-08-12 4 views
0

получил следующие error-
TypeError в/'модуль' объект hello_template не отозваныTypeError в/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)[email protected])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'), 
] 

ответ

4

Проблема вы пытаетесь создать экземпляр объекта в модуляcontext вместо классContext (обратите внимание на столицу).

ли это так:

from django.template import Context 

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

Там же в документации example.

+3

Еще лучше используйте ярлык «render», который выполняет задание последних трех строк. –

3

В context Impo rted in django.template.context - это модуль, который нельзя назвать вызываемым.

Вы ищете Context класса

from django.template import Context 

А потом попробуйте так:

... 
html = t.render(Context({'name': name})) 
... 
Смежные вопросы