如何使用bootstrap登录表单来验证django用户

时间:2017-08-03 06:40:35

标签: django twitter-bootstrap django-authentication

我正在构建我的第一个Django Web应用程序,并且有一个问题似乎无法找到解决方案。

我的主页(index.html)是基于引导程序的,它会在表单中显示一些文本和引导程序日志。

从该引导程序登录表单允许Django身份验证的正确方法是什么?

我根据互联网上此处和那里发现的建议尝试了解决方案,但我不断收到错误。

这是我的代码:

内部index.html

<div class="inner cover">
  <h1 class="cover-heading">Admin Interface</h1>
  <p class="lead">Welcome to your dedicated Admin Interface.<br>Log in to access your dashboard and control panel.</p>
  <form class="form-signin">
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>
</div>

在myapp / urls.py内部

from django.conf.urls import include, url
from user import views
from django.contrib.auth import views as auth_views


urlpatterns = [
    url(r'^$', views.index, name='index'),
]

在我的主要urls.py

from django.conf.urls import include
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('user.urls')),

]

我尝试添加form method =“post”action =“{%url'django.contrib.auth.views.login'%}但它不起作用。

你对我该如何进行有任何想法吗?

非常感谢您提前。

2 个答案:

答案 0 :(得分:1)

您可以将网址放在urls.py。

from django.conf.urls import include
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_view

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('user.urls')),
    url(r'^login/$', auth_view.login, name="login"),

]

然后将其路由到行动

<form method='POST' action = '{% url 'login' %}' class="form-signin">

答案 1 :(得分:0)

或者,您也可以使用 LoginView ,如下所示:

from django.contrib.auth.views import LoginView

urlpatterns = [
    ...
    url(r'^login/$', LoginView.as_view(), name="login"),
]