$ django-admin.py startproject mysite
lyndon@lyndon-ubuntu:~/Study/django$ tree mysite/ mysite/ ├── manage.py └── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py 1 directory, 5 files
from django.conf.urls import patterns, include, url urlpatterns = patterns(‘‘, url(r‘^home/$‘, ‘mysite.views.home‘, name=‘home‘), )
from django.http import HttpResponse def home(request): return HttpResponse(‘Welcome, logout‘)执行
$ python manager.py runserver
from django.http import HttpResponse from django.contrib.auth.decorators import login_required @login_required(login_url="/login/") def home(request): return HttpResponse(‘Welcome, logout‘)
from django.conf.urls import patterns, include, url urlpatterns = patterns(‘‘, url(r‘^home/$‘, ‘mysite.views.home‘, name=‘home‘), url(r‘^login/$‘, ‘django.contrib.auth.views.login‘, {‘template_name‘: ‘login.html‘}), )
<!DOCTYPE html> <html> <head> <title>login</title> </head> <body> {% if form.errors %} <p>Your username and password didn‘t match. Please try again.</p> {% endif %} <form method="post" action="{% url ‘django.contrib.auth.views.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" /> <input type="hidden" name="next" value="{{ next }}" /> </form> </body> </html>
TEMPLATE_DIRS = ( os.path.join(BASE_DIR, ‘templates‘), )
$ python manage.py syncdb
<input type="hidden" name="next" value="{{ next }}" />这样,当用户登陆后,可以直接跳转到登陆之前的页面.
url(r‘^logout/$‘, ‘django.contrib.auth.views.logout‘, {‘template_name‘: ‘logout.html‘}),
<!DOCTYPE html> <html> <head> <title>login</title> </head> <body> <p>Bye</p> </body> </html>
lyndon@lyndon-ubuntu:~/Study/django$ tree mysite/ mysite/ ├── db.sqlite3 ├── manage.py ├── mysite │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── views.py │ └── wsgi.py └── templates ├── login.html └── logout.html 2 directories, 9 files
Django用户认证系统 authentication system,布布扣,bubuko.com
Django用户认证系统 authentication system
原文:http://blog.csdn.net/feelang/article/details/24992693