我对Django的身份验证和登录视图有疑问。我一直在关注创建自定义登录的官方文档:https://docs.djangoproject.com/en/1.9/topics/auth/default/#how-to-log-a-user-in
我已创建自定义登录视图:
def myLogin(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect(reverse('main'))
else:
return redirect(reverse('login'))
return render(request, "login.html", {'user': user})
这是我的模板,它是从官方文档中复制的:https://docs.djangoproject.com/en/1.9/topics/auth/default/#all-authentication-views
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
</form>
{% endblock %}
我的网址看起来像这样:
from authentication.views import myLogin
from users.views import baseView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^main/', baseView, name='main'),
url(r'^login/', myLogin, name='login'),
url(r'^logout/', auth_views.logout, name='logout'),
]
当我尝试访问登录页面时,它会生成页面,但只生成登录按钮,而不是写入用户名和密码所需的表单字段。为什么会这样?使用内置登录时,登录功能按预期工作,但我担心我无法使用它,因为我需要一些额外的功能。
答案 0 :(得分:0)
在您的mylogin视图中,您没有通过任何登录表单,因此在您的模板中,您传递的表单变量是正确的,但因为它的空django不显示任何表单
所以在基于函数的视图中,您必须创建一个登录表单
SELECT time_of_action FROM `trackvendoraction` where time_of_action between '2016-06-29 00:00:00' and '2016-06-30 23:59:59'