FreeBSD 14
Sponsored Link

Django 5 : Install2024/02/01

 

Install Django which is the Python Web Application Framework.

[1]

Install Python 3.10, refer to here. (Django 5 requires Python 3.10 or higher)

[2] Install SQLite3 library.
root@dlp:~#
pkg install -y py310-sqlite3
[2] Install Django in a Python venv environment with any user you like to setup it.
freebsd@dlp:~ $
python3.10 -m venv --system-site-packages ~/django5

freebsd@dlp:~ $
. ~/django5/bin/activate

(django5) freebsd@dlp:~ $
pip install 'Django>=5'

Collecting Django>=5
  Downloading Django-5.0.1-py3-none-any.whl (8.1 MB)
      8.1/8.1 MB 27.2 MB/s eta 0:00:00
Collecting asgiref<4,>=3.7.0
  Using cached asgiref-3.7.2-py3-none-any.whl (24 kB)
Collecting sqlparse>=0.3.1
  Using cached sqlparse-0.4.4-py3-none-any.whl (41 kB)
Collecting typing-extensions>=4
  Using cached typing_extensions-4.9.0-py3-none-any.whl (32 kB)
Installing collected packages: typing-extensions, sqlparse, asgiref, Django
Successfully installed Django-5.0.1 asgiref-3.7.2 sqlparse-0.4.4 typing-extensions-4.9.0

(django5) freebsd@dlp:~ $
django-admin --version

5.0.1
# to exit from venv, run like follows

(django5) freebsd@dlp:~ $
deactivate

freebsd@dlp:~ $
[3] Create a test project.
freebsd@dlp:~ $
. ~/django5/bin/activate
# create testproject

(django5) freebsd@dlp:~ $
django-admin startproject testproject2

(django5) freebsd@dlp:~ $
cd testproject2

# configure database (default is SQLite)

(django5) freebsd@dlp:~/testproject2 $
python manage.py migrate
# create admin user

(django5) freebsd@dlp:~/testproject2 $
python manage.py createsuperuser

Username (leave blank to use 'freebsd'):
freebsd

Email address:
freebsd@dlp.srv.world

Password:
Password (again):
Superuser created successfully.
(django5) freebsd@dlp:~/testproject2 $
vi testproject2/settings.py
# line 28 : set if you allow to access to Django from other Hosts
# specify Hosts with comma separated
# if allow all, specify like follows

ALLOWED_HOSTS = [
'*'
]
# start server

(django5) freebsd@dlp:~/testproject2 $
python manage.py runserver 0.0.0.0:8000

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 01, 2024 - 01:59:55
Django version 5.0.1, using settings 'testproject2.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[4] Access to the [(server's hostname or IP address):8000/] from a client computer. It's OK if following site is displayed normally.
[5] It's possible to use admin site on [(server's hostname or IP address):8000/admin].
[6] Create a test application to try to use Django.
freebsd@dlp:~ $
. ~/django5/bin/activate
(django5) freebsd@dlp:~ $
cd testproject2

(django5) freebsd@dlp:~/testproject2 $
python manage.py startapp test_app

(django5) freebsd@dlp:~/testproject2 $
vi test_app/views.py
# add to last line

from django.http import HttpResponse
def main(request):
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'Django Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    return HttpResponse(html)

(django5) freebsd@dlp:~/testproject2 $
vi testproject2/urls.py
# line 18, 22 : add like follows

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test_app/', include('test_app.urls')),
]

(django5) freebsd@dlp:~/testproject2 $
vi test_app/urls.py
# create new

from django.urls import path
from .views import main

urlpatterns = [
    path('', main, name='home')
]

(django5) freebsd@dlp:~/testproject2 $
vi testproject2/settings.py
# line 33 : add test application in [INSTALLED_APPS] section

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'test_app',
)

(django5) freebsd@dlp:~/testproject2 $
python manage.py runserver 0.0.0.0:8000

[7] Access to the [(server's hostname or IP address):8000/test_app/] from a client computer. It's OK if testapp is displayed normally.
Matched Content