在Django中使用Custom DB进行登录

时间:2014-01-09 09:59:23

标签: python django authentication sqlite

我是django的新手,我有一个 sqlite3 数据库文件,我想在其中使用一个表来验证我的网站,这是一个非常基本的html登录页面,我该怎么办?去做?另外,如果您可以告诉我内置登录和身份验证工作如何以及他们使用哪个数据库?提前感谢。

1 个答案:

答案 0 :(得分:2)

请阅读此https://docs.djangoproject.com/en/dev/topics/auth/http://www.djangobook.com/en/2.0/chapter14/以便更好地理解。

要在Django中实现基本登录功能,您可以按照以下步骤操作。

先决条件: *您必须在settings.py *

中设置有效的数据库设置

<强> 1.Urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    # index page
    url(r'^$', 'app.views.index'),
    # after login user will be redirected to this url
    url(r'^logged_in/$', 'app.views.logged_in'),
    # using default django auth views with custom templates
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}),

) 

<强> 2。 Views.py

from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

# index view (just redirect to login page)
def index(request):
    return HttpResponseRedirect('/login')

# this view will run after successfull login
@login_required
def logged_in(request):
    return render_to_response('logged_in.html', context_instance=RequestContext(request))

第3。的login.html

{% extends 'base.html' %}
{% block title %}Log in{% endblock %}
{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}bootstrap/css/signin.css" />
{% endblock %}
{% block content %}
  <form action="" class="form-signin" method="post">{% csrf_token %}
      <h3 class="form-signin-heading text-center">Please log in</h3>
      {% if form.non_field_errors %}
          <div class="alert alert-danger">
            {{ form.non_field_errors|striptags }}
          </div>
      {% endif %}
      {% if not form.username.errors %}
        <input id="id_username" name="username" type="text" class="form-control" placeholder="Username (admin)" autofocus>
      {% else %}
        <div class="form-group has-error">
          {% for error in form.username.errors %} 
            <label class="control-label" for="id_username">{{ error }}</label>
          {% endfor %}
          <input id="id_username" name="username" type="text" class="form-control" placeholder="Username (admin)" autofocus>
        </div>
      {% endif %}
      {% if not form.password.errors %}
        <input id="id_password" name="password" type="text" class="form-control" placeholder="Password (admin)" autofocus>
      {% else %}
        <div class="form-group has-error">
          {% for error in form.password.errors %}
            <label class="control-label" for="id_password">{{ error }}</label>
          {% endfor %}
          <input id="id_password" name="password" type="text" class="form-control" placeholder="Password (admin)">
        </div>
      {% endif %}

      <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
    </form>
  </form>
{% endblock %}