Django 4 : Install2023/03/03 |
Install Django which is Python Web Application Framework.
|
|
[1] | Install Django under the Python venv. It's possible to do for any common user. |
[alma@dlp ~]$ python3 -m venv --system-site-packages ~/django [alma@dlp ~]$ source ~/django/bin/activate (django) [alma@dlp ~]$ pip3 install django==4 Collecting django==4 Downloading Django-4.0-py3-none-any.whl (8.0 MB) |################################| 8.0 MB 3.3 MB/s Collecting sqlparse>=0.2.2 Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB) |################################| 42 kB 275 kB/s Collecting asgiref<4,>=3.4.1 Downloading asgiref-3.5.0-py3-none-any.whl (22 kB) Installing collected packages: sqlparse, asgiref, django Successfully installed asgiref-3.5.0 django-4.0 sqlparse-0.4.2 WARNING: You are using pip version 21.2.3; however, version 22.0.4 is available. You should consider upgrading via the '/home/alma/django/bin/python3 -m pip install --upgrade pip' command.
(django) [alma@dlp ~]$
django-admin --version 4.0 # to exit from venv, run like follows (django) [alma@dlp ~]$ deactivate [alma@dlp ~]$ |
[2] | Create a test project. If Firewalld is running and also access to Django from other Hosts, Allow ports you plan to use with root privilege before it. (example below uses [8000/tcp]) |
[alma@dlp ~]$
source ~/django/bin/activate
# create testproject (django) [alma@dlp ~]$ django-admin startproject testproject (django) [alma@dlp ~]$ cd testproject # configure database (default is SQLite) (django) [alma@dlp testproject]$ python manage.py migrate
# create admin user (django) [alma@dlp testproject]$ python manage.py createsuperuser Username (leave blank to use 'alma'): alma Email address: alma@dlp.srv.world Password: Password (again): Superuser created successfully.
(django) [alma@dlp testproject]$
vi testproject/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 (django) [alma@dlp testproject]$ 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). March 16, 2022 - 01:00:59 Django version 4.0, using settings 'testproject.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. |
[alma@dlp ~]$
source ~/django/bin/activate
(django) [alma@dlp ~]$
(django) [alma@dlp testproject]$ cd testproject python manage.py startapp test_app
(django) [alma@dlp testproject]$
vi test_app/views.py # add to the end 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)
(django) [alma@dlp testproject]$
vi testproject/urls.py # line 17, 21 : 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')), ]
(django) [alma@dlp testproject]$
vi test_app/urls.py # create new from django.urls import path from .views import main urlpatterns = [ path('', main, name='home') ]
(django) [alma@dlp testproject]$
vi testproject/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',
)
(django) [alma@dlp testproject]$ python manage.py runserver 0.0.0.0:8000 |
[7] | Access to the [(server's hostname or IP address):8000/testapp/] from a client computer. It's OK if testapp is displayed normally. |
Sponsored Link |