我正在学习django并开始只使用一个小型登录验证系统。我创建了登录的html页面:
<html>
<title>Login</title>
<body>
<br><br><br><br><br><br><br><br><br><br>
<form name="login" action = "/loginr/" method = "get">
USERNAME:<input type="text" name="uname"><br>
PASSWORD:<input type="password" name="passwd"><br>
<input type = "submit" value = "Login">
</form>
</body>
</html>
my views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request,'login_page.html')
x=''
def process(request):
global x
x=request.GET
return HttpResponseRedirect('/login/thanks/')
def thanks(request):
return render(request, 'thanks.html')
my urls.py:
from django.conf.urls import patterns,url
from login import views
urlpatterns=patterns('',
url(r'^$',views.index, name="index"),
url(r'^loginr/',views.process),
url(r'^login/thanks/',views.thanks)
)
最外层的mysite / urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^login/', include('login.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^loginr/', include('login.urls')),
url(r'^login/thanks/',include('login.urls')),
)
以上代码显示没有错误。但是当我填写登录表单并单击登录时,页面本身会重新加载。我希望加载“thanks.html”。我在做什么错误?
答案 0 :(得分:1)
在views.py
中,您没有导入HttpResponseRedirect
。
from django.http import HttpResponseRedirect
<强>更新强>
更改mysite/urls.py
,如下所示:
...
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('login.urls')),
)
将login/urls.py
更改为foolow:
...
urlpatterns=patterns('',
url(r'^loginr/?$',views.process),
url(r'^login/thanks/?$',views.thanks)
url(r'^login/?$',views.index, name="index"),
)