Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. from django.shortcuts import render
  2. from django.contrib.auth import authenticate, login, logout
  3. from django.contrib.auth.decorators import login_required
  4. from django.http import HttpResponseRedirect, HttpResponse
  5. from django.contrib import auth
  6. from django.conf import settings
  7. from django.core.urlresolvers import reverse
  8. def login(request):
  9. next = request.POST.get('next', 'home/')
  10. if request.method == "POST":
  11. username = request.POST['username']
  12. password = request.POST['password']
  13. user = authenticate(username=username, password=password)
  14.  
  15.  
  16. if user is not None:
  17. if user.is_active:
  18. auth.login(request, user)
  19.  
  20. return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
  21. else:
  22. return HttpResponse("Inactive user.")
  23. else:
  24. return HttpResponseRedirect(settings.LOGIN_URL)
  25. return render(request, "login.html")
  26.  
  27. def logout(request):
  28. auth.logout(request)
  29. return HttpResponseRedirect(settings.LOGIN_URL)
  30.  
  31. @login_required(redirect_field_name='next')
  32. def home(request):
  33. return render (request, "home.html")
  34.  
  35. from django.conf.urls import url
  36.  
  37. from . import views
  38.  
  39. urlpatterns = [
  40. url(r'^$', views.home, name='home'),
  41. ]
  42.  
  43. from django.conf.urls import url, include
  44. from django.contrib import admin
  45. from django.contrib.auth import views
  46.  
  47.  
  48. urlpatterns = [
  49. url(r'^admin/', admin.site.urls),
  50. url(r'^home/', include('login.urls', namespace="login")),
  51. url(r'^$', views.login),
  52. url(r'^logout/$', views.logout),
  53. ]
  54.  
  55. import os
  56.  
  57. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  58. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  59.  
  60.  
  61. # Quick-start development settings - unsuitable for production
  62. # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
  63.  
  64. # SECURITY WARNING: keep the secret key used in production secret!
  65. SECRET_KEY = '(i34g@645+vc8$@y9qd)_fo1l#k%78up_cheab#!(b24xv$!uj'
  66.  
  67. # SECURITY WARNING: don't run with debug turned on in production!
  68. DEBUG = True
  69.  
  70. ALLOWED_HOSTS = []
  71.  
  72.  
  73. # Application definition
  74.  
  75. INSTALLED_APPS = [
  76. 'login',
  77. 'django.contrib.admin',
  78. 'django.contrib.auth',
  79. 'django.contrib.contenttypes',
  80. 'django.contrib.sessions',
  81. 'django.contrib.messages',
  82. 'django.contrib.staticfiles',
  83. ]
  84.  
  85. MIDDLEWARE_CLASSES = [
  86. 'django.middleware.security.SecurityMiddleware',
  87. 'django.contrib.sessions.middleware.SessionMiddleware',
  88. 'django.middleware.common.CommonMiddleware',
  89. 'django.middleware.csrf.CsrfViewMiddleware',
  90. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  91. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  92. 'django.contrib.messages.middleware.MessageMiddleware',
  93. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  94. ]
  95.  
  96. ROOT_URLCONF = 'myproject.urls'
  97.  
  98. TEMPLATES = [
  99. {
  100. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  101. 'DIRS': [],
  102. 'APP_DIRS': True,
  103. 'OPTIONS': {
  104. 'context_processors': [
  105. 'django.template.context_processors.debug',
  106. 'django.template.context_processors.request',
  107. 'django.contrib.auth.context_processors.auth',
  108. 'django.contrib.messages.context_processors.messages',
  109. ],
  110. },
  111. },
  112. ]
  113.  
  114. WSGI_APPLICATION = 'myproject.wsgi.application'
  115.  
  116.  
  117. # Database
  118. # https://docs.djangoproject.com/en/1.9/ref/settings/#databases
  119.  
  120. DATABASES = {
  121. 'default': {
  122. 'ENGINE': 'django.db.backends.sqlite3',
  123. 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  124. }
  125. }
  126.  
  127.  
  128. # Password validation
  129. # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password- validators
  130.  
  131. AUTH_PASSWORD_VALIDATORS = [
  132. {
  133. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  134. },
  135. {
  136. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  137. },
  138. {
  139. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  140. },
  141. {
  142. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  143. },
  144. ]
  145.  
  146.  
  147. # Internationalization
  148. # https://docs.djangoproject.com/en/1.9/topics/i18n/
  149.  
  150. LANGUAGE_CODE = 'en-us'
  151.  
  152. TIME_ZONE = 'UTC'
  153.  
  154. USE_I18N = True
  155.  
  156. USE_L10N = True
  157.  
  158. USE_TZ = True
  159.  
  160.  
  161. # Static files (CSS, JavaScript, Images)
  162. # https://docs.djangoproject.com/en/1.9/howto/static-files/
  163.  
  164. STATIC_URL = '/static/'
  165.  
  166.  
  167. LOGIN_URL = 'login/'
  168. APPEND_SLASH = False
  169.  
  170. <!DOCTYPE html>
  171. <center>
  172.  
  173. <section class="loginform cf">
  174. <h1 style="color:blue"> User Login </h1>
  175. <form name="login" method="post" accept-charset="utf-8" action="{% url 'login:home' %}">
  176. {% csrf_token %}
  177. <label for="usermail" align="center">User-Id </label>
  178. <input type="alphanumeric" name="userid" >
  179. <br >
  180. <label for="password" align="center">Password </label>
  181. <input type="alphanumeric" name="password" >
  182. <br >
  183. <input type="submit" value="Login" style="color:blue">
  184. <input type="hidden" name="next" value="{{ next }}"/>
  185. </center>
  186. </form>
  187. </section>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement