· 7 years ago · Nov 25, 2018, 01:58 PM
1settings.py
2"""
3Django settings for login project.
4
5Generated by 'django-admin startproject' using Django 1.11.
6
7For more information on this file, see
8https://docs.djangoproject.com/en/1.11/topics/settings/
9
10For the full list of settings and their values, see
11https://docs.djangoproject.com/en/1.11/ref/settings/
12"""
13
14import os
15
16# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
17BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18
19
20# Quick-start development settings - unsuitable for production
21# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
22
23# SECURITY WARNING: keep the secret key used in production secret!
24SECRET_KEY = 'm=0n9k#g6du7a2l5h-m6o7+g1++-(79i5ldgpy7-5mh3)c+-l!'
25
26# SECURITY WARNING: don't run with debug turned on in production!
27DEBUG = True
28
29ALLOWED_HOSTS = []
30
31
32# Application definition
33
34INSTALLED_APPS = [
35 'django.contrib.admin',
36 'django.contrib.auth',
37 'django.contrib.contenttypes',
38 'django.contrib.sessions',
39 'django.contrib.messages',
40 'django.contrib.staticfiles',
41 'home',
42 'social_django',
43]
44
45MIDDLEWARE = [
46 'django.middleware.security.SecurityMiddleware',
47 'django.contrib.sessions.middleware.SessionMiddleware',
48 'django.middleware.common.CommonMiddleware',
49 'django.middleware.csrf.CsrfViewMiddleware',
50 'django.contrib.auth.middleware.AuthenticationMiddleware',
51 'django.contrib.messages.middleware.MessageMiddleware',
52 'django.middleware.clickjacking.XFrameOptionsMiddleware',
53 'social_django.middleware.SocialAuthExceptionMiddleware', # <--
54]
55
56ROOT_URLCONF = 'login.urls'
57
58TEMPLATES = [
59 {
60 'BACKEND': 'django.template.backends.django.DjangoTemplates',
61 'DIRS': ['templates'],
62 'APP_DIRS': True,
63 'OPTIONS': {
64 'context_processors': [
65 'django.template.context_processors.debug',
66 'django.template.context_processors.request',
67 'django.contrib.auth.context_processors.auth',
68 'django.contrib.messages.context_processors.messages',
69
70 'social_django.context_processors.backends', # <--
71 'social_django.context_processors.login_redirect', # <--
72 ],
73 },
74 },
75]
76
77WSGI_APPLICATION = 'login.wsgi.application'
78
79
80# Database
81# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
82
83
84#DATABASES = {
85 # 'default': {
86 # 'ENGINE': 'django.db.backends.sqlite3',
87 # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
88 #}
89#}
90
91
92DATABASES = {
93 'default': {
94 'ENGINE': 'django.db.backends.oracle',
95 'NAME': 'xe',
96 'USER': 'django',
97 'PASSWORD': '12345',
98 'HOST':'127.0.0.1',
99 'PORT':'1521'
100 }
101}
102
103TEMPLATE_CONTEXT_PROCESSORS = (
104
105 'social.apps.django_app.context_processors.backends',
106 'social.apps.django_app.context_processors.login_redirect',
107
108)
109
110
111# Password validation
112# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
113
114AUTH_PASSWORD_VALIDATORS = [
115 {
116 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
117 },
118 {
119 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
120 },
121 {
122 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
123 },
124 {
125 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
126 },
127]
128
129SOCIAL_AUTH_URL_NAMESPACE = 'social'
130
131AUTHENTICATION_BACKENDS = (
132
133 'social_core.backends.open_id.OpenIdAuth',
134 'social_core.backends.google.GoogleOpenId',
135 'social_core.backends.google.GoogleOAuth2',
136 'social_core.backends.google.GoogleOAuth',
137 'social_core.backends.twitter.TwitterOAuth',
138 'social.backends.facebook.FacebookOAuth2',
139
140 'django.contrib.auth.backends.ModelBackend',
141)
142
143# Add the google credentials
144SOCIAL_AUTH_GOOGLE_OAUTH2_KEY ='1071989259170-es50h7ce4g271e2b54ng7bn89vvi963s.apps.googleusercontent.com' #Paste CLient Key
145SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'NSj8Ng_OuM-Eb5hOL9lBp_V4' #Paste Secret Key
146
147# Add the Facebook app credentials.
148SOCIAL_AUTH_FACEBOOK_KEY = 'app-id'
149SOCIAL_AUTH_FACEBOOK_SECRET = 'app-secret'
150
151LOGIN_URL = '/login/'
152LOGOUT_URL = 'logout'
153LOGIN_REDIRECT_URL = 'home'
154
155
156
157# Internationalization
158# https://docs.djangoproject.com/en/1.11/topics/i18n/
159
160LANGUAGE_CODE = 'en-us'
161
162TIME_ZONE = 'UTC'
163
164USE_I18N = True
165
166USE_L10N = True
167
168USE_TZ = True
169
170
171# Static files (CSS, JavaScript, Images)
172# https://docs.djangoproject.com/en/1.11/howto/static-files/
173
174STATIC_URL = '/static/'
175
176Url.py
177--------
178
179"""login URL Configuration
180
181The `urlpatterns` list routes URLs to views. For more information please see:
182 https://docs.djangoproject.com/en/1.11/topics/http/urls/
183Examples:
184Function views
185 1. Add an import: from my_app import views
186 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
187Class-based views
188 1. Add an import: from other_app.views import Home
189 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
190Including another URLconf
191 1. Import the include() function: from django.conf.urls import url, include
192 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
193"""
194from django.conf.urls import include, url
195from django.contrib import admin
196from django.conf import settings
197from django.contrib.auth import views as auth_views
198from django.conf.urls.static import static
199
200
201urlpatterns = [
202 url(r'^', include('home.urls')),
203 url(r'^admin/', admin.site.urls),
204 url(r'^login/$', auth_views.login, name='login'),
205 url(r'^oauth/', include('social_django.urls', namespace='social')),
206 url(r'^oauth/', include('django.contrib.auth.urls', namespace='auth')),
207]
208
209base.html
210----------------
211{% load static %}
212<html>
213<head>
214 <!-- Materialize CSS -->
215 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
216 <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>
217 <!-- Material icons -->
218 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
219</head>
220<title>Django</title>
221{% if user.is_authenticated %}
222<div class="navbar-fixed">
223 <nav class="blue">
224 <div class="nav-wrapper container">
225 <a href="/" class="brand-logo center">Google App</a>
226 <ul id="nav-mobile" class="right hide-on-med-and-down">
227 <li><a href="/logout">Log out</a></li>
228 </ul>
229 </div>
230 </nav>
231</div>
232{% endif %}
233<div class="container">
234 {% block content %}
235 {% endblock %}
236</div>
237</html>
238
239Login.html
240----------------------
241{% extends 'base.html' %}
242
243{% block content %}
244 <h2>Login</h2>
245 <form method="post">
246 {% csrf_token %}
247 {{ form.as_p }}
248 <button type="submit">Login</button>
249 </form>
250 <br>
251 <p><strong>-- OR --</strong></p>
252 <div class="form-group row">
253 <a class="btn btn-primary" href="{% url 'social:begin' 'facebook' %}">
254 Facebook Account
255 </a>
256
257 <a class="btn btn-primary" href="{% url 'social:begin' 'google-oauth2' %}">
258 Google
259 </a>
260
261
262</div>
263{% endblock %}