Newer
Older
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
Week 12: Setting up a simple Django application
Inside a centos VM I cloned from LDAP server (link cloned)
conda create --name django python=3.9
conda activate django
conda install django
django-admin --version
django-admin startproject helloworld
cd helloworld
python manage.py startapp greetings
In greetings/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, World")
In helloworld/urls.py:
from django.contrib import admin
from django.urls import path
from greetings import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.hello, name='hello'),
]
Open settings.py
Add the IP Address to ALLOWED_HOSTS
Locate the ALLOWED_HOSTS setting and update it to include your VM's IP address.
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '0.0.0.0', '192.168.56.101']
python manage.py runserver 0.0.0.0:8001
Open your web browser on your host machine and navigate to http://<VM_IP>:8001/
VM_IP = 192.168.56.101
a page opens up with message 'Hello, World'
Set a login page for your Django application
add 'greetings' in helloworld/settings.py:
INSTALLED_APPS = [
...
'greetings',
'django.contrib.auth', # Make sure auth is included
'django.contrib.contenttypes', # Make sure contenttypes is included
...
]
Create login template
mkdir -p greetings/templates/registration and create login.html here:
<!-- greetings/templates/registration/login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
in helloworld/urls.py write:
# helloworld/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')), # Include built-in auth URLs
path('', include('greetings.urls')), # Include your app URLs
]
create a file greetings/urls.py and write:
# greetings/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'), # Home view
]
in greetings/views.py wtite
# greetings/views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required
def home(request):
return render(request, 'home.html')
create home.html in greetings/templates as:
<!-- greetings/templates/home.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h2>Welcome, {{ user.username }}!</h2>
<p>You are logged in.</p>
<a href="{% url 'logout' %}">Logout</a>
</body>
</html>
make sure helloworld/settings.py has:
# helloworld/settings.py
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/accounts/login/'
LOGIN_URL = '/accounts/login/'
# Ensure you have a working database and run migrations
python manage.py migrate
python manage.py createsuperuser
(add username, password etc. cmurali, timeMACHINE)
python manage.py runserver 0.0.0.0:8001
go to http://192.168.56.101:8001/accounts/login/ and make sure login is working and see Hello World upon login.