2017-08-17 63 views
0

對不起。當我提交表單時,我得到以下錯誤。主題:Django問題:上下文必須是字典而不是RequestContext

TypeError at /foodlogin/ 
context must be a dict rather than RequestContext. 

我的views.py如下。我的登錄表單需要識別數據庫信息:Models.py-User類,並且需要在會話中保留所有網頁。 另外,登錄成功將重定向到「foodmenu.html」。

# -*- coding: utf-8 -*- 
from django.shortcuts import redirect 
from django.template.loader import get_template 
from django.http import HttpResponse 
from castecfood.models import User 
from castecfood import forms 
from django.template import RequestContext 

def index(request):  
    template=get_template('index.html') 
    html=template.render(locals()) 
    return HttpResponse(html) 

def foodlogin(request):  
    if request.method=='POST': 
     form=forms.LoginForm(request.POST) 
     if form.is_valid(): 
      login_id=request.POST['user_id'].strip() 
      login_password=request.POST['user_pass'] 
      try: 
      user=User.objects.get(jobnumber=login_id) 
      if user.jobnumber==login_id anduser.personpwd==login_password: 
      request.session['user_id']=user.jobnumber 
      request.session['user_pass']=user.personpwd 
      return redirect('/foodmenu/') 
      else: 
       message="ID或密碼有錯喔,請再檢查一次" 
      except: 
      message="目前無法登入" 
     else: 
      message="請檢查輸入的欄位內容" 
    else: 
     form=forms.LoginForm() 

    template=get_template('foodlogin.html') 
    request_context=RequestContext(request) 
    request_context.push(locals()) 
    html=template.render(request_context)  
    reponse=HttpResponse(html) 
    return reponse 

def foodmenu(request): 
    template=get_template('foodmenu.html') 
    html=template.render(locals()) 
    return HttpResponse(html) 

我foodlogin.html文件: 用它來登錄用戶輸入的 「user.jobnumber和user.personpwd」

<!--foodlogin.html (castecsystem project)>--> 
    <!DOCTYPE HTML> 
    <html> 
     <head> 
     <title>Login</title> 
     {% include "stylesheet.html" %} 
     </head> 
    <body> 
    <!-- Banner --> 
      {% include "banner.html" %} 
     <!--InnerHeader --> 
      {% block content %} 
        <section id="one" class="wrapper special"> 
        <div class="inner"> 
         <header class="major"> 
        <h2>FoodOrder System</h2> 
         </header> 
        </div> 
       <div class="container"> 
       <div class="foodlogin_image"><img src="/static/images/person_head1.png"></div> 
       <form name='login' action='.' method="POST"> 
       {% csrf_token %}     
       <fieldset>     
       {{form.as_table}} 
       </fieldset>       
       <br/> 
       <input type="submit" value="Login"> 
       <input type="reset" value="Reset"> 
       </form> 
       </div> 
       </section> 
      {% endblock %}   
    <!-- Footer --> 
     {% include "footer.html" %} 
    <!-- Scripts --> 
     {% include "script.html" %} 
    </body> 
    </html> 

我forms.py文件:

# -*- coding: utf-8 -*- 
from django import forms 

class LoginForm(forms.Form): 
    user_id=forms.CharField(label='Your ID:', max_length=10) 
    user_pass=forms.CharField(label='Your Password:', 
    widget=forms.PasswordInput()) 

我的urls.py文件:

urlpatterns = [ 
url(r'^admin/', include(admin.site.urls)), 
url(r'^$', index), 
url(r'^foodlogin/$', foodlogin), 
url(r'^foodmenu/$', foodmenu), 
url(r'^captcha/', include('captcha.urls')) 
] 

環境:

Request Method: GET 
Request URL: http://127.0.0.1:8000/foodlogin/ 

Django Version: 1.11.3 
Python Version: 3.6.1 
Installed Applications: 
['django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'castecfood', 
'captcha'] 
Installed 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'] 

回溯:

File "C:\Users\Sean\.spyder-py3\myproject\env\lib\site- 
packages\django\core\handlers\exception.py" in inner 
41.response = get_response(request) 
File "C:\Users\Sean\.spyder-py3\myproject\env\lib\site- 
packages\django\core\handlers\base.py" in _get_response 
187.response = self.process_exception_by_middleware(e, request) 
File "C:\Users\Sean\.spyder-py3\myproject\env\lib\site- 
packages\django\core\handlers\base.py" in _get_response 
185.response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "C:\Users\Sean\.spyder-py3\myproject\castecsystem\castecfood\views.py" 
in foodlogin 
39.html=template.render(request_context)  
File "C:\Users\Sean\.spyder-py3\myproject\env\lib\site- 
packages\django\template\backends\django.py" in render 
64.context = make_context(context, request, 
autoescape=self.backend.engine.autoescape) 
File "C:\Users\Sean\.spyder-py3\myproject\env\lib\site- 
packages\django\template\context.py" in make_context 
287.raise TypeError('context must be a dict rather than %s.' % 
context.__class__.__name__) 

Exception Type: TypeError at /foodlogin/ 
Exception Value: context must be a dict rather than RequestContext. 

我不知道如何解決這個問題。我嘗試了很久,但我仍然沒有解決 。請讓我知道如何解決它。我非常感謝。

+0

你使用最新的Django的軟件? –

+0

是的,我使用最新的Django:Django版本:1.11.3 – Sean

回答

0

你需要改變:

html=template.render(request_context) 

到:

html=template.render(context=locals(), request=request) 
相關問題