Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python /hello_模板处的TypeError-“模块”对象不可调用_Python_Django_Pycharm - Fatal编程技术网

Python /hello_模板处的TypeError-“模块”对象不可调用

Python /hello_模板处的TypeError-“模块”对象不可调用,python,django,pycharm,Python,Django,Pycharm,出现以下错误- 无法调用/hello_模板“module”对象处的TypeError hello.html 设置.py url.py django.template.context中导入的上下文是一个模块,不可调用 你在找课吗 然后试着这样做: ... html = t.render(Context({'name': name})) ... 问题是您试图实例化模块上下文的对象,而不是类上下文 这样做: from django.template import Context html = t.

出现以下错误- 无法调用/hello_模板“module”对象处的TypeError

hello.html

设置.py

url.py

django.template.context中导入的上下文是一个模块,不可调用

你在找课吗

然后试着这样做:

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

问题是您试图实例化模块上下文的对象,而不是类上下文

这样做:

from django.template import Context

html = t.render(Context({'name': name}))
文档中还有一个属性。

更好的是,使用渲染快捷方式完成最后三行的工作。
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)
"""
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',
)
""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'),
]
from django.template import Context
...
html = t.render(Context({'name': name}))
...
from django.template import Context

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