我正在用這個拉出我的頭髮一段時間。我正在使用mod_wsgi機器將我的django應用程序從測試服務器部署到apache。 一切似乎工作,除了Django的用戶認證系統似乎寫會話表django_session,不知何故,它做了某種驗證,因爲當我提供錯誤的憑據自定義錯誤消息被返回。這就是@login_required()在視圖中總是返回false。並且訪問應用管理頁面被拒絕。 在我所做的研究中可能會缺少參數 WSGIPassAuthorization在apache2.conf文件中,但它不起作用。Django無法在mod_wsgi下進行身份驗證
我的虛擬主機文件:
<VirtualHost *:80>
ServerName myapp.cl
ServerAlias *.myapp.cl myapp.cl
Alias /static /home/ubuntu/django_projects/myapp/static
<Directory /home/ubuntu/django_projects/myapp/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django_projects/myapp/myapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIProcessGroup myapp
WSGIDaemonProcess myapp python-path=/home/ubuntu/django_projects/myapp
WSGIScriptAlias//home/ubuntu/django_projects/myapp/myapp/wsgi.py process-group=myapp application-group=%{GLOBAL}
</VirtualHost>
和我url_file:
rom django.conf.urls import url, include
from django.contrib import admin
from . import views as views_ini
from django.contrib.auth.views import login, logout, password_change, password_change_done
from myapp.views import aviso_sistema, inicio, LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/$', login, {'template_name': 'loginrex.html',
'authentication_form': LoginForm,
}, name='django.contrib.auth.views.login'),
url(r'^logout/$', logout, {'next_page': '/login'}, name='logout'),
# ....Avisos del sistema
url(r'^aviso_sistema/(?P<titulo>[\w\ ]+)/(?P<detalle>[\w\ ]+)/(?P<tipo>[\w\ ]+)$', aviso_sistema, name='aviso_sistema'),
]
我/myapp/myapp/views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from django import forms
from myapp.definiciones.preferenciasDefi import unaPref
from myapp.deposito import procesoAnombre
from django.shortcuts import redirect
from django.conf import settings
@login_required()
def inicio(request):
#if not request.user.is_authenticated:
# raise ValueError('Usuario OK pero no logueado')
# return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
mdp = unaPref('mesActual')
dato = procesoAnombre(mdp)
datos = {
'mmp': dato,
'idv': ' ',
}
return render(request, 'inicio.html', datos)
class LoginForm(AuthenticationForm):
username = forms.CharField(label="Usuario", max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'username'}),
)
password = forms.CharField(label="Clave", max_length=30,
widget=forms.PasswordInput(attrs={'class': 'form-control', 'name': 'password'}),
)
# ........Genera los avisos del sistema
def aviso_sistema(request, titulo, detalle, tipo):
datos = {
'titulo': titulo,
'detalle': detalle,
'tipo': tipo,
}
return render(request, 'aviso_sistema.html', datos)
我真的很希望得到一些幫助,這。 謝謝
您是否定義了任何用戶?你是如何創建它們的? –
我創建了一個超級用戶: python manage.py創建超級用戶 它在表auth_user –
中設置了WSGIPassAuthorization。這是您在使用基於非會話的身份驗證時所需要的。 – Jayground