· 9 years ago · Aug 03, 2016, 06:27 PM
1from django.views.generic import View
2from django.utils import timezone
3from django.shortcuts import render, get_object_or_404,render_to_response,redirect
4from django.http import HttpResponseRedirect
5from django.contrib import auth
6from django.contrib.auth import authenticate,login
7from django.core.context_processors import csrf
8from .forms import UserForm
9
10# Create your views here.
11
12def home(request):
13 return render(request, 'fosssite/home.html')
14
15def login(request):
16 c={}
17 c.update(csrf(request))
18 return render_to_response('fosssite/login.html',c)
19
20def UserFormView(request):
21 form_class=UserForm
22 template_name='fosssite/signup.html'
23
24 if request.method=='GET':
25 form=form_class(None)
26 return render(request,template_name,{'form':form})
27
28
29 #validate by forms of django
30 if request.method=='POST':
31 form=form_class(request.POST)
32
33 if form.is_valid():
34 # not saving to database only creating object
35 user=form.save(commit=False)
36 #normalized data
37 username=form.cleaned_data['username']
38 password=form.cleaned_data['password']
39 #not as plain data
40 user.set_password(password)
41 user.save() #saved to database
42
43 user=auth.authenticate(username=username,password=password)
44 if user is not None:
45 auth.login(request,user)
46 return HttpResponseRedirect('/')#url in brackets
47
48 return render(request,template_name,{'form':form})
49
50def auth_view(request):
51 username=request.POST.get('username', '')
52 password=request.POST.get('password', '')
53 user=auth.authenticate(username=username,password=password)
54
55 if user is not None:
56 auth.login(request,user)
57 return HttpResponseRedirect('/profileuser')#url in brackets
58 else:
59 return HttpResponseRedirect('/invalid')
60
61def profileuser(request):
62 return render_to_response('fosssite/profileuser.html',{'fullname':request.user.username})
63
64def logout(request):
65 auth.logout(request)
66 pass
67
68def edit_user_profile(request):
69 pass
70
71def invalid_login(request):
72 return render_to_response('fosssite/invalid_login.html')
73
74"""
75Django settings for foss project.
76
77Generated by 'django-admin startproject' using Django 1.9.7.
78
79For more information on this file, see
80https://docs.djangoproject.com/en/1.9/topics/settings/
81
82For the full list of settings and their values, see
83https://docs.djangoproject.com/en/1.9/ref/settings/
84"""
85
86import os
87
88# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
89BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
90
91
92# Quick-start development settings - unsuitable for production
93# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
94
95# SECURITY WARNING: keep the secret key used in production secret!
96SECRET_KEY = 'ezwgujuxhv9@3w$o^x8t#*gi-zu6c4&-h^41a-)!w--xqe1=c4'
97
98# SECURITY WARNING: don't run with debug turned on in production!
99DEBUG = True
100
101ALLOWED_HOSTS = []
102
103
104# Application definition
105
106INSTALLED_APPS = [
107 'django.contrib.admin',
108 'django.contrib.auth',
109 'django.contrib.contenttypes',
110 'django.contrib.sessions',
111 'django.contrib.messages',
112 'django.contrib.staticfiles',
113 'fosssite',
114]
115
116MIDDLEWARE_CLASSES = [
117 'django.middleware.security.SecurityMiddleware',
118 'django.contrib.sessions.middleware.SessionMiddleware',
119 'django.middleware.common.CommonMiddleware',
120 'django.middleware.csrf.CsrfViewMiddleware',
121 'django.contrib.auth.middleware.AuthenticationMiddleware',
122 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
123 'django.contrib.messages.middleware.MessageMiddleware',
124 'django.middleware.clickjacking.XFrameOptionsMiddleware',
125]
126
127ROOT_URLCONF = 'foss.urls'
128
129TEMPLATES = [
130 {
131 'BACKEND': 'django.template.backends.django.DjangoTemplates',
132 'DIRS': [],
133 'APP_DIRS': True,
134 'OPTIONS': {
135 'context_processors': [
136 'django.template.context_processors.debug',
137 'django.template.context_processors.request',
138 'django.contrib.auth.context_processors.auth',
139 'django.contrib.messages.context_processors.messages',
140 ],
141 },
142 },
143]
144
145WSGI_APPLICATION = 'foss.wsgi.application'
146
147
148# Database
149# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
150# read database.md file for configuring
151DATABASES = {
152 'default': {
153 'ENGINE': 'django.db.backends.postgresql_psycopg2',
154 'NAME': '',
155 'USER': '',
156 'PASSWORD': '',
157 'HOST': '',
158 'PORT': '',
159 }
160}
161
162
163# Password validation
164# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
165
166AUTH_PASSWORD_VALIDATORS = [
167 {
168 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
169 },
170 {
171 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
172 },
173 {
174 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
175 },
176 {
177 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
178 },
179]
180
181
182# Internationalization
183# https://docs.djangoproject.com/en/1.9/topics/i18n/
184
185LANGUAGE_CODE = 'en-us'
186
187TIME_ZONE = 'UTC'
188
189USE_I18N = True
190
191USE_L10N = True
192
193USE_TZ = True
194
195
196# Static files (CSS, JavaScript, Images)
197# https://docs.djangoproject.com/en/1.9/howto/static-files/
198
199STATIC_URL = '/static/'