2017-12-11 2 views
-2

私は自分自身にPython \ Djangoを教えようとしています。これはビジュアルスタジオでDjangoのスターターサイトで構築されていますので、すべてのsettings.pyなどはあらかじめビルドされています。'WSGIRequest'オブジェクトに 'session'属性がありません。ログインしてください。

私はIISサーバーからサイトにログインしようとすると、私が得る:

はAttributeErrorを

/ログイン/ 'WSGIRequest' オブジェクトが何の属性 'セッション' を持たないでIものの私のローカルマシン上で実行しようとするとpython manage.py runserverからうまく動作します。

django.VERSION(2、0、0、 '最終'、0)

settings.py:

import os 
import posixpath 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


SECRET_KEY = 'c94808af-46cc-47da-b465-4e66803a8324' 

DEBUG = True 

ALLOWED_HOSTS = ['*'] 


# Application definition 

INSTALLED_APPS = [ 
    'app', 
    # Add your apps here to enable them 
    '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.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'AllFlexHub.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     '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 = 'AllFlexHub.wsgi.application' 




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



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




LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 



STATIC_URL = '/static/' 

STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static'])) 



views.py:

from django.contrib.auth import login, authenticate 
    from django.contrib.auth.forms import UserCreationForm 
    from django.shortcuts import render, redirect, render_to_response, get_object_or_404 
    from django.http import HttpRequest, HttpResponseRedirect 
    from django.template import RequestContext 
    from datetime import datetime 
    from app.models import * 
    from app.forms import * 

    def home(request): 
     """Renders the home page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/index.html', 
      { 
       'title':'Home Page', 
       'year':datetime.now().year, 
      } 
     ) 

    def Register(request): 
     if request.method == "POST": 
      form = RegisterUser(request.POST) 
      if form.is_valid(): 
       new_user = User.objects.create_user(**form.cleaned_data) 
       return HttpResponseRedirect('') 
     else: 
      form = RegisterUser() 

     return render(request, 'app\Register.html', {'form': form}) 


    def contact(request): 
     """Renders the contact page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/contact.html', 
      { 
       'title':'Contacts', 
       'year':datetime.now().year, 
      } 
     ) 

    def about(request): 
     """Renders the about page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/about.html', 
      { 
       'title':'About', 
       'message':'All Flex IT Tools.', 
       'year':datetime.now().year, 
      } 
     ) 

    def problemreports(request): 
     """Renders the home page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/problemreport.html', 
      { 
       'reports': PRTickets.objects.filter(Status__startswith='Act'), 
       'title':'Problem Reports', 
       'year':datetime.now().year, 
      } 
     ) 

    def myPRs(request): 
     """Renders the home page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/problemreport.html', 
      { 
       'reports': PRTickets.objects.filter(User=User.objects.get(username=request.user.username)), 
       'title':'Problem Reports', 
       'year':datetime.now().year, 
      } 
     ) 

    def Closed(request): 
     """Renders the home page.""" 
     assert isinstance(request, HttpRequest) 
     return render(
      request, 
      'app/problemreport.html', 
      { 
       'reports': PRTickets.objects.filter(Status__startswith='Close'), 
       'title':'Problem Reports', 
       'year':datetime.now().year, 
      } 
     ) 

    def SearchPRs(request): 
     if request.method == "POST": 
      Sval = request.POST['SearchField'] 
      frm = request.POST['SearchOption'] 
      form = Search() 
      if frm == "Ticket_Number": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(Ticket_Number__icontains = Sval), 'form': form}) 
      elif frm == "User": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(User__icontains = Sval), 'form': form}) 
      elif frm == "Title": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(Title__icontains = Sval), 'form': form}) 
      elif frm == "Bldg": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(Bldg__icontains = Sval), 'form': form}) 
      elif frm == "Description": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(Description__icontains = Sval), 'form': form}) 
      elif frm == "Computer": 
       return render(request, 'app\SearchPRs.html', {'reports': PRTickets.objects.filter(Computer__icontains = Sval), 'form': form}) 
      elif frm == "Status": 
       return render(request, 'app\SearchPRs.html', { 'reports': PRTickets.objects.filter(Status__icontains = Sval), 'form': form}) 
      form = Search() 
     else: 
      form = Search() 
      return render(request, 'app\SearchPRs.html', {'form': form}) 

    def PRNewItem(request): 
     if request.method == "GET": 
      form = PRticektform(initial={'User': User.objects.get(username=request.user.username)}) 
      return render(
       request, 
       'app/NewItem.html', 
       { 
       'form':form , 
       'now':datetime.now(), 
       'title': 'SEARCH', 
       } 
      ) 
      return render(request, 'app/NewItem.html',) 
     elif request.method == "POST": 
      form = PRticektform(request.POST) 
      form.save() 
      return HttpResponseRedirect('/problemreports') 

    def EditPR(request, name): 
     if request.method == 'POST': 
      val = PRTickets.objects.get(Ticket_Number=name) 
      val.User = request.POST['User'] 
      val.Title = request.POST['Title'] 
      val.Bldg = request.POST['Bldg'] 
      val.Computer = request.POST['Computer'] 
      val.Description = request.POST['Description'] 
      val.Status = request.POST['Status'] 
      val.save() 
      cmmts = Comments(Comment=request.POST['Comment'], User=User.objects.get(username=request.user.username), Related_PR=val) 
      cmmts.save() 
      return HttpResponseRedirect('/problemreports') 
     else: 
      PRinfo = PRTickets.objects.get(Ticket_Number=name) 
      form = EditPRs(
       initial={ 
       'User': PRinfo.User, 
       'Title': PRinfo.Title, 
       'Bldg': PRinfo.Bldg, 
       'Computer': PRinfo.Computer, 
       'Description': PRinfo.Description, 
       'Status': PRinfo.Status, 
       } 
       ) 

     return render(request, 'app/EditPR.html', 
      {'form': form, 
      'PRNumber': name, 
      'title': name, 
      'Fields': PRTickets.objects.get(Ticket_Number=name), 
      'Comms': Comments.objects.filter(Related_PR=name), 
      } 
     ) 



urls.py:
""" Definition of urls for ITWebsite. """

from datetime import datetime 
from django.conf.urls import url 
import django.contrib.auth.views 

import app.forms 
import app.views 

# Uncomment the next lines to enable the admin: 
# from django.conf.urls import include 
# from django.contrib import admin 
# admin.autodiscover() 

urlpatterns = [ 
    # Examples: 
    url(r'^$', app.views.home, name='home'), 
    url(r'^contact$', app.views.contact, name='contact'), 
    url(r'^about', app.views.about, name='about'), 
    url(r'^Register', app.views.Register, name='Register'), 
    url(r'^problemreports$', app.views.problemreports, name='problemreports'), 
    url(r'^problemreports/NewItem', app.views.PRNewItem, name='PRNewItem'), 
    url(r'^problemreports/MyProblemReports', app.views.myPRs, name='myPRs'), 
    url(r'^problemreports/Closed', app.views.Closed, name='Closed'), 
    url(r'^problemreports/SearchPRs', app.views.SearchPRs, name='SearchPRs'), 
    url(r'^problemreports/(\d{1,10})', app.views.EditPR, name='EditPR'), 
    url(r'^login/$', 
     django.contrib.auth.views.login, 
     { 
      'template_name': 'app/login.html', 
      'authentication_form': app.forms.BootstrapAuthenticationForm, 
      'extra_context': 
      { 
       'title': 'Log in', 
       'year': datetime.now().year, 
      } 
     }, 
     name='login'), 
    url(r'^logout$', 
     django.contrib.auth.views.logout, 
     { 
      'next_page': '/', 
     }, 
     name='logout'), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    # url(r'^admin/', include(admin.site.urls)), 
] 

+3

コードをスクリーンショットとして投稿しないでください。それはただ私たちがあなたを助けることをより困難にします。 –

+0

あなたはどのバージョンのDjangoを使用していますか? – Alasdair

+0

あなたの 'urls.py'と' views.py'モジュールを投稿してください。 –

答えて

3

MIDDLEWARE_CLASSES設定がdeprecated in Django 1.10、およびremoved in Django 2.0です。

代わりにMIDDLEWAREを定義する必要があります。

SessionAuthenticationMiddlewareがDjango 2.0で削除されました。リストから削除する必要があります。

+0

"_CLASSES"保存された/再開されたWebサイトはiisで同じエラーが発生します。 –

+0

あなたの質問に完全なトレースバックを含めて、使用しているDjangoのバージョンを教えてください。 – Alasdair

+0

「MIDDLEWARE_CLASSES」の設定を単に「MIDDLEWARE」に変更するだけでは不十分かもしれません - 異なるフォーマット、異なる注文などがあるかもしれません。 –

関連する問題