2014-10-27 27 views

回答

0

可以使用http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html#bootstrap3-horizontal-forms

class SomeForm(forms.Form): 
    username = forms.CharField(
     label=u'Username', 
     required=True, 
    ) 
    password = forms.CharField(
     label=u'Login', 
     required=True, 
     widget=forms.PasswordInput 
    ) 
    remember_me = forms.BooleanField(
     label=u'Remember me', 
     required=True 
    ) 
    def __init__(self, *args, **kwargs): 
     super(SomeForm, self).__init__(*args, **kwargs) 

     self.helper = FormHelper() 
     self.helper.form_class = 'form-horizontal' 
     self.helper.label_class = 'col-lg-2' 
     self.helper.field_class = 'col-lg-8' 
     self.helper.layout = Layout(
      'username', 
      'password', 
      'remember_me', 
      StrictButton('Sign in', css_class='btn-default'), 

     ) 
0

我發現前兩個答案如此不足,我不得不放棄我的2美分。如果您知道自己在做什麼,可以在幾分鐘內部署該登錄。在你的項目中創建你的第一個應用假設該應用名爲「home」。創建一個模板目錄。在裏面,創建一個「註冊」目錄。創建一個文件「login.html」。 內容項目的/ home /模板/註冊/ login.html的的:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <link rel="icon" href="../../favicon.ico"> 
    <title>Signin/title> 
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> 
    </head> 
    <body class="text-center"> 
    <form class="form-signin"> 
     <img class="mb-4" src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72"> 
     <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1> 
     <label for="inputEmail" class="sr-only">Email address</label> 
     <input id="inputEmail" class="form-control" placeholder="Email address" required="" autofocus="" type="email"> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input id="inputPassword" class="form-control" placeholder="Password" required="" type="password"> 
     <div class="checkbox mb-3"> 
     <label> 
      <input value="remember-me" type="checkbox"> Remember me 
     </label> 
     </div> 
     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
     <p class="mt-5 mb-3 text-muted">© 2017-2018</p> 
    </form> 
    </body> 
</html> 
<style> 
html, 
body { 
    height: 100%; 
} 

body { 
    display: -ms-flexbox; 
    display: -webkit-box; 
    display: flex; 
    -ms-flex-align: center; 
    -ms-flex-pack: center; 
    -webkit-box-align: center; 
    align-items: center; 
    -webkit-box-pack: center; 
    justify-content: center; 
    padding-top: 40px; 
    padding-bottom: 40px; 
    background-color: #f5f5f5; 
} 

.form-signin { 
    width: 100%; 
    max-width: 330px; 
    padding: 15px; 
    margin: 0 auto; 
} 
.form-signin .checkbox { 
    font-weight: 400; 
} 
.form-signin .form-control { 
    position: relative; 
    box-sizing: border-box; 
    height: auto; 
    padding: 10px; 
    font-size: 16px; 
} 
.form-signin .form-control:focus { 
    z-index: 2; 
} 
.form-signin input[type="email"] { 
    margin-bottom: -1px; 
    border-bottom-right-radius: 0; 
    border-bottom-left-radius: 0; 
} 
.form-signin input[type="password"] { 
    margin-bottom: 10px; 
    border-top-left-radius: 0; 
    border-top-right-radius: 0; 
} 
</style> 

我加載jQuery和引導與CDN頭,複製模板到風格標籤下方(只是想一些代碼,可能是demo'd,我知道有更奇葩的方法來做到這一點)。基本上這是自舉網站上的剪貼工作。

下面是django可以揮動的叫做DRY的神奇魔杖;編輯位於項目/項目目錄中的主要urls.py:

from django.contrib import admin 
from django.urls import path, include 
from django.contrib.auth.views import LoginView 

urlpatterns = [ 
    path('', LoginView.as_view()), 
    path('home/', include('home.urls')), 
    path('admin/', admin.site.urls), 
] 

那麼爲什麼這個工作? Django已經爲你寫了視圖,所以你所要做的就是將視圖的模板放在正確的位置,然後在空中點上24k的django魔術。