-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdjango.sh
289 lines (236 loc) · 6.84 KB
/
django.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ======================
# From Debian to Web App
# ======================
# This page contains a list of terminal commands that
# create a Django web application with routing, templates
# and user accounts.
#
# You can start from a fresh debian installation. Or with a
# fresh debian container:
#
# docker run -v $(pwd):/var/www --rm -it -p 80:80 debian:11-slim
#
# You can copy+paste each command to see the application take
# shape or copy the whole page and paste it in one go.
# ======================
# Let's configure Debian
# ======================
# Do not show dialogs during the upgrade
export DEBIAN_FRONTEND=noninteractive
# Update the packages
apt update -y && apt upgrade -y
# ====================
# Let's install Django
# ====================
cd /var/www
apt install -y python3-django
django-admin startproject mysite
cd mysite
python3 manage.py migrate
# ====================
# Let's install Apache
# ====================
apt install -y apache2
apt install -y libapache2-mod-wsgi-py3
cat << 'EOF' > /etc/apache2/sites-enabled/000-default.conf
ServerName mysite.local
WSGIPythonPath /var/www/mysite
<VirtualHost *:80>
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
EOF
service apache2 start
# Yay, we have a working Django instance!
read -p 'Django is live at 127.0.0.1! Hit enter to continue.'
# ===================
# Let's use templates
# ===================
# mysite/ in mysite/ is the code dir in Django
cd mysite
mkdir templates
cat << 'EOF' > templates/index.html
<h1>Hello World</h1>
EOF
cat << 'EOF' > views.py
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
EOF
cat << 'EOF' > urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
EOF
cat << 'EOF' >> settings.py
INSTALLED_APPS += ['mysite']
EOF
service apache2 restart
# Yay, we have static site!
read -p 'Serving a static site! Hit enter to continue.'
# =========================
# Let's use a base template
# =========================
# Let's create a base template
cat << 'EOF' > templates/base.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {background: #60a060}
</style>
</head>
<body>{% block content %}{% endblock %}</body>
</html>
EOF
# And use it for the index page:
cat << 'EOF' > templates/index.html
{% extends "base.html" %}
{% block content %}
<h1>Hello World</h1>
{% endblock %}
EOF
service apache2 restart
# Yay, we have a working base template!
read -p 'The base template is live! Hit enter to continue.'
# =======================
# Let's add user accounts
# =======================
cd ..
# Create a new app called User for Auth
django-admin startapp user
cat << 'EOF' >> mysite/settings.py
INSTALLED_APPS += ['user']
EOF
mkdir mysite/templates/user
# Apply Auth model migrations to db
python3 manage.py makemigrations
python3 manage.py migrate
# Create a User Registration Form
# Extends the UserCreationForm
cat << 'EOF' > user/forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model= User
fields = ['username', 'email', 'password1', 'password2']
EOF
# Create some basic views for registration
cat << 'EOF' > user/views.py
from django.shortcuts import render, redirect
from .forms import UserRegisterForm
def index(request):
return render(request, 'user/index.html')
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'user/register.html', {'form': form})
EOF
# Create the URL mappings of Auth
cat << 'EOF' > user/urls.py
from django.urls import path, include
from django.contrib.auth import views as auth_views
from user import views as user_views
urlpatterns = [
path('', user_views.index, name='auth'),
path('register/', user_views.register, name='register'),
path('login/', auth_views.LoginView.as_view(template_name='user/login.html'), name='login'),
path('logout/',auth_views.LogoutView.as_view(template_name='user/logout.html'), name='logout'),
]
EOF
cat << 'EOF' > mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('auth/', include('user.urls')),
]
EOF
# Create Templates for Authorization
cat << 'EOF' > mysite/templates/user/index.html
{% extends 'base.html' %}
{% block content %}
<a href="{% url 'register' %}"><button>Register</button></a>
<a href="{% url 'login' %}"><button>Login</button></a>
{% endblock %}
EOF
cat << 'EOF' > mysite/templates/user/register.html
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
<p>
Already have an Account? <a href="{% url 'login' %}"><button>Log In</button></a>
</p>
{% endblock %}
EOF
cat << 'EOF' > mysite/templates/user/login.html
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log In</button>
</form>
<p>
Don't have an Account? <a href="{% url 'register' %}"><button>Register</button></a>
</p>
{% endblock %}
EOF
cat << 'EOF' > mysite/templates/user/logout.html
{% extends 'base.html' %}
{% block content %}
<p>
Don't have an account?
<a href="{% url 'register' %}"><button>Register</button></a>
</p>
<p>
Already have an Account?<a href="{% url 'login' %}"><button>Login</button></a>
</p>
{% endblock %}
EOF
# Create a Login redirect URL route
cat << 'EOF' >> mysite/settings.py
LOGIN_REDIRECT_URL = 'index'
EOF
cat << 'EOF' > mysite/templates/index.html
{% extends "base.html" %}
{% block content %}
{% if user.is_authenticated %}
<h1>Hello, {{ user.username }}!</h1>
<h3>You are logged in as {{ user.username }} </h3>
<a href="{% url 'logout' %}"><button>Logout</button></a>
{% else %}
<h1>Hello, World!</h1>
<a href="{% url 'auth' %}"><button>Authorize</button></a>
{% endif %}
{% endblock %}
EOF
chown www-data:www-data .
chown www-data:www-data db.sqlite3
service apache2 restart
# Yay, we have user accounts!
read -p 'User accounts are live! Hit enter to continue.'