Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BioHPC training notes
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Chinthak Murali
BioHPC training notes
Commits
e00c8119
Commit
e00c8119
authored
7 months ago
by
Chinthak Murali
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parent
2b6fc226
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Week 12: Setting up a simple Django application
+196
-0
196 additions, 0 deletions
Week 12: Setting up a simple Django application
with
196 additions
and
0 deletions
Week 12: Setting up a simple Django application
0 → 100644
+
196
−
0
View file @
e00c8119
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.
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment