Django 3 : Install2021/09/22 |
Install Django which is Python Web Application Framework.
|
|
[1] | Install some packages. |
root@dlp:~# apt -y install python3-virtualenv
|
[2] | Install Django under an Virtualenv environment. It's possible to do for any common user. |
debian@dlp:~$ virtualenv venv debian@dlp:~$ cd ~/venv debian@dlp:~/venv$ source bin/activate (venv) debian@dlp:~/venv$ pip install django==3.2 Collecting django==3.2 Downloading Django-3.2-py3-none-any.whl (7.9 MB) ..... ..... Installing collected packages: sqlparse, pytz, asgiref, django Successfully installed asgiref-3.4.1 django-3.2 pytz-2021.1 sqlparse-0.4.2
(venv) debian@dlp:~/venv$
django-admin --version 3.2 # exit from virtualenv (venv) debian@dlp:~/venv$ deactivate debian@dlp:~/venv$ |
[3] | Create a test project. |
# create testproject (venv) debian@dlp:~/venv$ django-admin startproject testproject (venv) debian@dlp:~/venv$ cd testproject # configure database (default is SQLite) (venv) debian@dlp:~/venv/testproject$ python manage.py migrate
# create admin user (venv) debian@dlp:~/venv/testproject$ python manage.py createsuperuser Username (leave blank to use 'debian'): debian Email address: debian@dlp.srv.world Password: Password (again): Superuser created successfully.
(venv) debian@dlp:~/venv/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 (venv) debian@dlp:~/venv/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). September 22, 2021 - 00:31:25 Django version 3.2, 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. |
debian@dlp:~$
(venv) debian@dlp:~/venv/testproject$ cd ~/venv debian@dlp:~/venv$ source bin/activate (venv) debian@dlp:~/venv$ cd testproject python manage.py startapp test_app
(venv) debian@dlp:~/venv/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)
(venv) debian@dlp:~/venv/testproject$
vi testproject/urls.py # line 17 : 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')), ]
(venv) debian@dlp:~/venv/testproject$
vi test_app/urls.py # create new from django.urls import path from .views import main urlpatterns = [ path('', main, name='home') ]
(venv) debian@dlp:~/venv/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',
)
(venv) debian@dlp:~/venv/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 |
|