Skip to content
Snippets Groups Projects
Commit e00c8119 authored by Chinthak Murali's avatar Chinthak Murali
Browse files

Add new file

parent 2b6fc226
No related merge requests found
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.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment