演示/ urls.py
from django.conf.urls import url, include, patterns
from django.contrib import admin
import views
from . import views
urlpatterns = patterns('demo.urls',
url(r'^admin', admin.site.urls),
url(r'^', views.login, name="login"),
url(r'^resetpwd', views.resetpwd, name='resetpwd'),
url(r'^mechanics/', include('mechanics.urls', namespace="mechanics")),
)
力学/ urls.py
import views
from . import views
urlpatterns = patterns('mechanics.urls',
url(r'^admin', include(admin.site.urls)),
url(r'^$', views.login, name="login"),
url(r'^resetpwd', views.resetpwd, name='resetpwd'),
url(r'^loandelinquency', views.loandelinquency, name='loandelinquency'),
)
力学/ views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
import cPickle
def loandelinquency(request):
return render(request, 'mechanics/LoanDelinquency.html')
def login(request):
try:
users = cPickle.load(open('users.p', 'rb'))
except:
users = {'user@email.com':'**********'}
cPickle.dump(users, open('users.p', 'wb'))
if request.method == 'POST':
try:
users = cPickle.load(open('users.p', 'rb'))
except:
users = {'user@email.com':'**********'}
cPickle.dump(users, open('users.p', 'wb'))
user = request.POST.get('user','')
pwd = request.POST.get('pwd', '')
request.session['user'] = user
request.session['pwd'] = pwd
try:
if user in users:
if users[user] == pwd:
return render(request, 'mechanics/inside.html')
else:
wronguser = ''
wrongpwd = 'Wrong password.'
return render(request, 'mechanics/login.html', {'wrongpwd': wrongpwd, 'wronguser':wronguser})
else:
wronguser = 'Wrong username'
wrongpwd = ''
return render(request, 'mechanics/login.html', {'wrongpwd': wrongpwd, 'wronguser':wronguser})
except:
return render(request, 'mechanics/login.html')
if request.method == "GET":
return render(request, 'mechanics/login.html')
def resetpwd(request):
if request.method == 'GET':
return render(request, "resetpwd.html")
力学/模板/力学/ login.html的
<html>
<body>
<title>Data Science Demo</title>
<h1 align="center">Ninja's</h1>
<br>
<div align="center"><font size=24>Login</font>
<br>
<form align="center" method="post">
{% csrf_token %}
E-mail<br>
<input type="text" align="center" name="user"><br>
Password<br>
<input type="password" align="center" name="pwd"><br>
<input type="submit" value="Submit">
{% if wrongpwd %}
<br>
<font color="red">{{ wrongpwd }}</font><br>
{% endif %}
{% if wronguser %}
<br>
<font color="red">{{ wronguser }}</font>
{% endif %}
</form>
<a href="{% url 'mechanics:resetpwd' %}">Reset Password</a>
</div>
</body>
</html>
<style>
div {
position: relative;
width: 300px;
padding: 25px;
border: 25px solid navy;
margin: auto;
background-color: #b0e0e6;
}
</style>
力学/模板/力学/ resetpwd.html
<html>
<title>Reset Password</title>
<body>
<p>You did it!</p>
</body>
</html>
<a href="{% url 'mechanics:resetpwd' %}">Reset Password</a>
只是让我回到登录页面。它与我为代码添加的每个其他链接都做了同样的事情。我该如何解决这个问题?
答案 0 :(得分:4)
您需要在正则表达式的末尾添加一个美元符号。
url(r'^$', views.login, name="login"),
如果没有美元符号,正则表达式r'^'
将匹配所有网址,因此将忽略此网址下方的任何网址格式。
您实际上可以从根网址配置中移除此网址和resetpwd
,因为您已在包含的mechanics.urls
中添加了这些视图。
请注意,Django附带an authentication system。你应该使用它而不是试着自己编写。
答案 1 :(得分:1)
您的代码存在以下问题:
首先,您完全正在使用Django的身份验证系统。如果你知道你在做什么这很好(虽然它真的让你想知道为什么你使用Django,然后)。
在任何其他情况下,你应该真的使用它,因为:
<association>
正确地重定向到登录页面,本地化,权限,通过电子邮件重置密码等等。作为旁注:
您有两个login_required()
和login
个网页,并且您包含两次管理网站(在基础urls.py和机制网站中)。