2017-10-19 73 views
-2

hello guys我有一个django错误我不知道错误在哪里请看看我的代码并帮我解决 这是我的看法从Notes应用程序的.pyTypeError at/notes/index/context必须是一个字典而不是RequestContext

from django.shortcuts import render 
from django.http import HttpResponse 
from django.template import loader,RequestContext 

# Create your views here. 
def index(request): 
    template=loader.get_template("index.html") 
    rc=RequestContext(request,{'username':'hello'}) 
    return HttpResponse(template.render(rc)) 

这是appsuite项目文件夹

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.11/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '&1hv71rr0e7b!m)7sv(p7of0p7q0%-mb9o*^r*amw$d1o3%=s_' 

# 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', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'appsuite.urls' 

WSGI_APPLICATION = 'appsuite.wsgi.application' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'notes/templates')], 

     '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', 

      ], 
     }, 
    }, 
] 



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

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


# Password validation 
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/ 

STATIC_URL = '/static/' 

我的settings.py配置和我的index.html是

<h3>{{username}}</h3> 

,这是错误的IAM得到当我试图加载页面

TypeError at /notes/index/ 
context must be a dict rather than RequestContext. 
Request Method: GET 
Request URL: http://127.0.0.1:8000/notes/index/ 
Django Version: 1.11.6 
Exception Type: TypeError 
Exception Value:  
context must be a dict rather than RequestContext. 
Exception Location: C:\Users\madhumani\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\context.py in make_context, line 287 
Python Executable: C:\Users\madhumani\AppData\Local\Programs\Python\Python36-32\python.exe 
Python Version: 3.6.2 
Python Path:  
['D:\\python programs\\appsuite', 
'C:\\Users\\madhumani\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip', 
'C:\\Users\\madhumani\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs', 
'C:\\Users\\madhumani\\AppData\\Local\\Programs\\Python\\Python36-32\\lib', 
'C:\\Users\\madhumani\\AppData\\Local\\Programs\\Python\\Python36-32', 
'C:\\Users\\madhumani\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages'] 
Server time: Thu, 19 Oct 2017 14:08:41 +0000 

什么,这是我的项目是如何:

appsuite(MAIN FOLDER) 
__db.sqlite3 
__manage.py 
__appsuite 
     __pycache__ 
     __init.py__ 
     __settings.py 
     __urls.py 
     __wsgi.py 
__notes 
     __pycache 
     __migrations 
     __templates 
      __index.html 
     __init.py 
     __admin.py 
     __apps.py 
     __models.py 
     __test.py 
     __urls.py 
     __views.py 

,请您帮我搞清楚什么是解决方法提前致谢

+0

关于该错误消息的含义尚不清楚?不要使用RequestContext,使用字典。 –

+0

@丹尼罗斯曼谢谢你回复我,但我不明白你到底在说什么,请你清楚,谢谢 –

+0

我简直不明白怎么可能不清楚。您正在使用RequestContext。别。 –

回答

0

正如错误消息所示,您应该使用常规字典而不是RequestContext。由于您的原始代码使用RequestContext,因此当您致电template.render()时,您可以传递request作为参数。

def index(request): 
    template=loader.get_template("index.html") 
    context = {'username':'hello'} 
    return HttpResponse(template.render(context, request=request)) 

你通常不会像这样渲染一个模板。使用render快捷方式要快得多。

from django.shortcuts import render 

def index(request): 
    context = {'username':'hello'} 
    return render(request, 'index.html', context) 

注意的是,在问题的代码是很老的 - 它已经过时了,因为Django的1.8于2015年被释放,我会建议采取不同的教程,否则你可能会碰到类似的问题。

+0

感谢很多兄弟,问题解决了 –

相关问题