2017-10-19 103 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

感謝很多兄弟,問題解決了 –

相關問題