我是django的新手并且陷入了我认为简单的问题。我在我的应用程序中设置了用户身份验证,工作正常,我遇到的问题是提交表单时出现的网址。这是问题所在:
我的表单上接受用户登录凭据的操作设置为"{% url 'home' %}"
,这是对/dashboard/home
的引用。这正是我想要成功登录时的URL的原因。但是,当登录失败时,我想留在登录URL,即/dashboard/login
。正如您可以想象的那样,发生的事情是,如果登录失败,网址仍会更改为/dashboard/home
。我认为这对用户来说是误导性的,通常只是不好的做法,但我不知道如何根据登录尝试的结果动态更改URL。如何在尝试失败时将网址更改为/dashboard/login
,但在成功时将其保留为/dashboard/home
?
截断到表格。
<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})
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'),
]
答案 0 :(得分:0)
正如AMG在评论中提到的,RightThing(tm)要做的是在登录视图中处理表单提交(POST),然后重定向到dashbord home 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})
答案 1 :(得分:0)
据我所知,您的表单会在用户主页上重定向用户,无论是否有效。
因此您不需要使用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
是表单提交后将重定向页面的网址。因此,如果你使用与我上面写的相同的视图,你不需要动作网址,只需保留action="."