我正在使用Django 1.6(和Python 2.7)制作一个简单的登录应用程序,但是我在一开始就遇到了错误,这导致我无法继续进行。
以下是该站点的url.py文件
from django.conf.urls import patterns, include, url
from django.contrib import admin
import login
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('login.urls', namespace='login')),
url(r'^admin/', include(admin.site.urls)),
)
这是login/urls.py文件:
from django.conf.urls import patterns, url
from login import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^auth/', views.auth, name='auth'),
)
这是login/views.py
from django.shortcuts import render
from django.contrib.auth import authenticate
def auth(request):
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
# the password verified for the user
if user.is_active:
msg = "User is valid, active and authenticated"
else:
msg = "The password is valid, but the account has been disabled!"
else:
# the authentication system was unable to verify the username and password
msg = "The username and password were incorrect."
return render(request, 'login/authenticate.html', {'MESSAGE': msg})
def index(request):
return render(request, 'login/login_form.html')
我有一个表单,其中action属性是这样的:
{% url 'login:auth' %}
这就是问题所在,当我试图加载页面时,我得到了:
Reverse for 'auth' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$auth/']
但是如果我将URL模式设置为:
url(r'', views.auth, name='auth')
它运行良好,只是将动作设置为'/'。
我一直在寻找答案,但不明白为什么它不起作用。
我尝试将登录url模式更改为url(r'^login/$', include('login.urls', namespace='login')),但什么也没变。