我是django的新手,我认为这是个简单的问题。我已经在我的应用程序中设置了用户身份验证,它运行良好,我遇到的问题是提交表单时出现的url地址。以下是问题所在:
在我的表单上接受用户登录凭据的操作设置为"{% url 'home' %}"
,这是对/dashboard/home
的引用。这正是我想要的网址,在成功登录的情况下。但是,当登录失败时,我希望停留在登录url上,即/dashboard/login
。正如您可以想象的那样,相反,如果登录失败,url仍将更改为/dashboard/home
。我认为这对用户来说是误导性的,通常只是错误的做法,但我不知道如何根据登录尝试的结果动态地更改url。当尝试失败时,如何将url更改为/dashboard/login
,但在成功时将其保持为/dashboard/home
?
我的档案
HTML模板
被截断为仅为窗体。
<form method="POST" action="{% url 'home' %}">
{% csrf_token %}
<div class="form">
<label for="email" class="credentials">Email:</label>
<input type="text" name="email" id="email" size="12" maxlength="30" />
</div>
<div class="form">
<label for="password" class="credentials">Password:</label>
<input type="password" name="password" id="password" size="12"
maxlength="30" />
</div>
<div class="submit">
<input type="submit" name="submit" value="GET STARTED" />
</div>
</form>
视图
请原谅所有的打印函数,它们都是初学者的帮助:)我还使用了一些通过上下文字典传递给模板的变量,这些变量还没有从视图中删除(例如,"login_successful")
def portal(request):
if request.method == 'POST':
print ("first if was executed")
form = AuthenticationForm(request.POST)
if form.is_valid():
print ("second if was executed")
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
print("user authenticated")
if user.is_active:
print("user active")
login_successful = 1
print('login_successful: ',login_successful)
login(request, user)
d = (UserFields.objects.filter(userid__exact=username).values())
d2 = d[0]
print(d2)
m = d2["manager"]
am = d2["apexmanager"]
print (m)
print (am)
if am==True:
print ("apex executed")
return render(request, 'dashboard/homeApexManager_new.html', {'login_error': login_successful})
elif m==True:
print ("m true executed")
return render(request, 'dashboard/homeManager_new.html', {'login_error': login_successful})
else:
print ("m false executed")
return render(request, 'dashboard/homeEmployee_new.html', {'login_error': login_successful})
else:
print("not active")
return render(request, 'dashboard/login.html')
else:
print ("not authenticated")
login_error = 1
print (login_error)
return render_to_response('dashboard/login.html', {'login_error': login_error})
else:
print ("form is not valid")
login_error=1
print (login_error)
return render_to_response('dashboard/login.html', {'login_error': login_error})
else:
print ("request is not POST")
return render(request, 'dashboard/login.html', {'form': form})
Urls
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login', auth_views.login,name = 'login'),
url(r'^home', views.portal,name = 'home'),
]
发布于 2016-12-22 15:57:39
正如AMG在他的评论中提到的,RightThing(tm)要做的是处理登录视图中的表单提交(POST),然后重定向到dashbord url (如果表单有效并且登录成功),或者在表单无效或登录失败时重新显示表单(没有重定向)。通常情况下,这里是处理表单提交的规范方法(除了搜索表单,它应该使用GET方法,永远不要重定向):
def someformview(request, ...):
if request.method == "POST":
# the user submitted the form, let's validate it
form = SomeFormClass(request.POST)
if form.is_valid():
# ok, process the data and redirect the user
do_something_with(form.cleaned_data)
return redirect("someapp:anotherview")
# if form is not valid just redisplay it again
# so the user gets the error messages
else:
# assume a GET request: create an unbound form
form = SomeFormClass()
# we either had a GET request or invalid form,
# let's display it
return render(request, "someapp/someviewtemplate.html", {form:form})
发布于 2016-12-22 22:03:12
据我从您的问题上了解,您的表单重定向用户在用户主页上,无论是否有效的形式。
因此,您不需要使用action="{% url 'home' %}"
重定向用户,这意味着当表单提交时,页面必须在主页上重定向用户,无论它成功登录与否。
你必须用你的视角重定向。下面我给大家留下了一些评论,我希望它能帮助你:
def portal(request):
form = AuthenticationForm(request.POST)
if request.method == 'POST': # checkout if method POST
print ("first if was executed")
if form.is_valid(): #checkout if form is valid
print ("second if was executed")
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
return HttpResponseRedirect('/dashboard/home/') #if form is valid, it redirect user on home page
else:
return render(request, 'dashboard/login.html', {'form': form}) # if not valid it render this form on the same page
else: # if method GET we just render the form
print ("request is not POST")
return render(request, 'dashboard/login.html', {'form': form})
希望它能帮到你。
更新
action
是在表单提交后将重定向页面的网址。因此,如果要使用与我前面所写的相同的视图,则不需要操作url,只需将其保留为action="."
即可。
https://stackoverflow.com/questions/41272120
复制相似问题