[关闭]
@tenlee 2015-08-19T05:00:52.000000Z 字数 3782 阅读 1568

Django登陆注销

Django Python


文件配置

app的models.py

  1. class User(models.Model):
  2. username = models.CharField(max_length=30)
  3. password = models.CharField(max_length=100)
  4. def __str__(self):
  5. return self.username

app 的view.py

  1. #!/usr/bin/env python3
  2. #coding = utf-8
  3. from django.shortcuts import render
  4. from django.http import *
  5. from books.models import *
  6. from django.contrib.auth.forms import UserCreationForm
  7. from django.template import RequestContext
  8. from django import forms
  9. # Create your views here.
  10. class UserForm(forms.Form):
  11. username = forms.CharField()
  12. password = forms.CharField(widget = forms.PasswordInput)
  13. def register(request):
  14. if request.method == 'POST':
  15. uf = UserForm(request.POST)
  16. if(uf.is_valid()):
  17. uname = uf.cleaned_data['username']
  18. pword = uf.cleaned_data['password']
  19. User.objects.create(username = uname, password = pword)
  20. return HttpResponseRedirect("/login/")
  21. else:
  22. uf = UserForm()
  23. return render(request, 'books/register.html', {'uf': uf}, context_instance=RequestContext(request))
  24. def login(request):
  25. session = request.session.get('username', False)
  26. if(not session):
  27. if(request.method == 'POST'):
  28. uf = UserForm(request.POST)
  29. if(uf.is_valid()):
  30. uname = uf.cleaned_data['username']
  31. pword = uf.cleaned_data['password']
  32. user = User.objects.filter(username__exact = uname, password__exact = pword)
  33. if(user):
  34. request.session['username'] = uname
  35. return HttpResponse('login success')
  36. #return HttpResponseRedirect('/home/')
  37. else:
  38. return HttpResponseRedirect('/login/')
  39. else:
  40. return HttpResponseRedirect('/login/')
  41. else:
  42. uf = UserForm()
  43. return render(request, 'books/login.html', {'uf': uf}, context_instance=RequestContext(request))
  44. else:
  45. return HttpResponseRedirect('/home/')
  46. def home(request):
  47. username = request.session.get('username', 'anybody')
  48. return render(request, 'books/home.html', {'username': username})
  49. def logout(request):
  50. session = request.session.get('username', False)
  51. if session:
  52. del request.session['username']
  53. return render(request, 'books/logout.html', {'username':session})
  54. else:
  55. return HttpResponse('please login')
  56. def show_color(request):
  57. if "favorite_color" in request.session:
  58. return HttpResponse("Your favorite color is %s" %
  59. request.session["favorite_color"])
  60. else:
  61. return HttpResponse("You don't have a favorite color")
  62. def set_color(request):
  63. if "favorite_color" in request.GET:
  64. response = HttpResponse("Your favorite color is now %s"
  65. % request.GET["favorite_color"])
  66. request.session["favorite_color"] = request.GET["favorite_color"]
  67. return response
  68. else:
  69. return HttpResponse("You didn't give a favorite color")

settings.py添加响应的app,urls.py 添加相应的url

settings.py中包含如下

  1. INSTALLED_APPS = (
  2. 'bootstrap_admin', # 使bootstrap_admin
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. 'gouwuche',
  10. 'books',
  11. 'polls',
  12. )
  13. from django.conf import global_settings
  14. TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
  15. 'django.core.context_processors.request',
  16. )
  17. BOOTSTRAP_ADMIN_SIDEBAR_MENU = True
  18. MIDDLEWARE_CLASSES = (
  19. 'django.contrib.sessions.middleware.SessionMiddleware',
  20. 'django.middleware.common.CommonMiddleware',
  21. 'django.middleware.csrf.CsrfViewMiddleware',
  22. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  23. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  24. 'django.contrib.messages.middleware.MessageMiddleware',
  25. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  26. 'django.middleware.security.SecurityMiddleware',
  27. )

html模板

home.html

  1. <body>
  2. <h3>Welcome to this site !</h3>
  3. <p>hello <span>{{ username }}</span> </p>
  4. {% if username != 'anybody' %}
  5. <a href="/logout/">logout</a>
  6. {% else %}
  7. <a href="/login/">login</a>
  8. {% endif %}
  9. </body>

login.html

  1. <body>
  2. <form method = 'post' enctype='multipart/form-data'>
  3. {% csrf_token %}
  4. {{ uf.as_p }}
  5. <input type='submit', value='login'/>
  6. </form>
  7. </body>

logout.html

  1. <body>
  2. <h3> Good Bye </h3>
  3. {% if username %}
  4. <a href="/login/">login</a>
  5. {% endif %}
  6. </body>

register.html

  1. <body>
  2. <form method="post" enctype='multipart/form-data'> {% csrf_token %}
  3. {{ uf.as_p }}
  4. <input type="submit" value="register"/>
  5. </form>
  6. </body>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注