{"id":6485,"date":"2023-06-10T12:46:00","date_gmt":"2023-06-10T12:46:00","guid":{"rendered":"https:\/\/www.smarthost.au\/blog\/?p=6485"},"modified":"2024-06-18T13:22:16","modified_gmt":"2024-06-18T13:22:16","slug":"how-to-run-and-install-django-framework-on-smarthost","status":"publish","type":"post","link":"https:\/\/www.smarthost.au\/blog\/how-to-run-and-install-django-framework-on-smarthost","title":{"rendered":"How to run and install django framework on smarthost"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-center\"><strong>Introduction to the installation<\/strong><\/h4>\n\n\n\n<p>What you need to have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account on smarthost (the best is nvme series of packages)<\/li>\n\n\n\n<li>Django &#8211; version 4.2.1<\/li>\n\n\n\n<li>Python &#8211; version 3.9.16<\/li>\n\n\n\n<li>database &#8211; sqlite3<\/li>\n\n\n\n<li>SSH Access (you can enable your access on customer panel)<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center\"><strong>For the purposes of installation, the data required for the account<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account name:<strong> janek<\/strong><\/li>\n\n\n\n<li>Domain name: <strong>djdomena.pl<\/strong> (added in cPanel with generated SSL certificate)<\/li>\n\n\n\n<li>The name of the directory where the django applications will be &#8211; <strong>djapp<\/strong><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center\"><strong>We start by logging into cpanel and looking for Software -&gt; Setup Python App<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"982\" height=\"318\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1.png\" alt=\"\" class=\"wp-image-6487\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1.png 982w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1-300x97.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1-768x249.png 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1-520x168.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a1-740x240.png 740w\" sizes=\"auto, (max-width: 982px) 100vw, 982px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center\"><strong>We select the version of python, specify the path to the root directory of the application ( djapp ) and select our domain or subdomain.<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2.webp\" alt=\"\" class=\"wp-image-6489\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2.webp 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2-300x127.webp 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2-768x325.webp 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2-520x220.webp 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a2-740x313.webp 740w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We have chosen python 3.9<\/p>\n\n\n\n<p><strong>\/home\/janek\/public_html\/djdomain.pl\/djapp<\/strong><\/p>\n\n\n\n<p>Domain : djdomain.pl<\/p>\n\n\n\n<p>We click on <strong>CREATE<\/strong><\/p>\n\n\n\n<p>A virtual python environment and our djapp directory will be created.<\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>Important!<\/strong> a message will be displayed on how to access virtualenv.<\/p>\n\n\n\n<p>It is best to copy and save in a safe place.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"586\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3.webp\" alt=\"\" class=\"wp-image-6490\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3.webp 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3-300x172.webp 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3-768x440.webp 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3-520x298.webp 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a3-740x423.webp 740w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We log in via ssh and use the source command:<br><strong>source \/home\/janek\/virtualenv\/public_html\/djdomena.pl\/djapp\/3.9\/bin\/activate &amp;&amp; cd \/home\/janek\/public_html\/djdomena.pl\/djapp<\/strong><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p>View after the command has been executed:<\/p>\n\n\n\n<p><code>((djapp:3.9)) {janek@serwer:djapp}$<\/code><\/p>\n\n\n\n<p>Logging out of virtualenv: <strong>deactivate<\/strong> command<\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p>Install django ( if the specific version is django==4.2.1 ) with the command <code>pip install django<\/code><\/p>\n\n\n\n<p>After installation, the result on the console:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>((djapp:3.9)) {janek@serwer:djapp}$ pip install django\nCollecting django \nUsing cached Django-4.2.1-py3-none-any.whl (8.0 MB)\nCollecting asgiref&lt;4,&gt;=3.6.0 (from django)\nUsing cached asgiref-3.6.0-py3-none-any.whl (23 kB)\nCollecting sqlparse&gt;=0.3.1 (from django)\nUsing cached sqlparse-0.4.4-py3-none-any.whl (41 kB)\nInstalling collected packages: sqlparse, asgiref, django\nSuccessfully installed asgiref-3.6.0 django-4.2.1 sqlparse-0.4.4<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p>We also install whitenoise<\/p>\n\n\n\n<p><code>pip install whitenoise<\/code><\/p>\n\n\n\n<p>Without this package, access to the django-admin panel will not use CSS styles.<\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p>Informative in our catalog<\/p>\n\n\n\n<p><code>\/home\/janek\/public_html\/djdomena.pl\/djapp<\/code><\/p>\n\n\n\n<p>there should be directories : <strong>public<\/strong>, <strong>tmp<\/strong> and the files <strong>manage.py<\/strong> and <strong>passenger_wsgi.py<\/strong><\/p>\n\n\n\n<p><br>We create our first app with the command:<\/p>\n\n\n\n<p><code>django-admin startproject mysite .<\/code><\/p>\n\n\n\n<p><br>Next, we will first look at the <strong>passenger_wsgi.py file<\/strong><\/p>\n\n\n\n<p>You will need to customise\/edit it with the following code<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import imp\nimport os\nimport sys\n\nsys.path.insert(0, os.path.dirname(__file__))\nwsgi = imp.load_source('wsgi', 'mysite\/wsgi.py')\napplication = wsgi.application<\/code><\/pre>\n\n\n\n<p><br>The most important is the line<\/p>\n\n\n\n<p><code>wsgi = imp.load_source('wsgi', 'mysite\/wsgi.py')<\/code><\/p>\n\n\n\n<p>mysite is our app that we added a step earlier<\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p>Then we edit <strong>mysite\/settings.py<\/strong><\/p>\n\n\n\n<p>We will be adding or editing<\/p>\n\n\n\n<p><code>ALLOWED_HOSTS = ['djdomena.pl','www.djdomena.pl']<\/code><\/p>\n\n\n\n<p><br>Configuration of whitenoise in the MIDDLEWARE section<\/p>\n\n\n\n<p><code>\"whitenoise.middleware.WhiteNoiseMiddleware\",<\/code><\/p>\n\n\n\n<p><br>This is what whitenoise should look like in the 2nd position:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>MIDDLEWARE = [ \n 'django.middleware.security.SecurityMiddleware',\n \"whitenoise.middleware.WhiteNoiseMiddleware\",\n 'django.contrib.sessions.middleware.SessionMiddleware',\n 'django.middleware.common.CommonMiddleware',\n 'django.middleware.csrf.CsrfViewMiddleware',\n 'django.contrib.auth.middleware.AuthenticationMiddleware',\n 'django.contrib.messages.middleware.MessageMiddleware',\n 'django.middleware.clickjacking.XFrameOptionsMiddleware', \n]<\/code><\/pre>\n\n\n\n<p><code>STATIC_ROOT = BASE_DIR \/ 'static'<\/code><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p>We are launching our app to work<\/p>\n\n\n\n<p>We need to be in the djapp directory<\/p>\n\n\n\n<p><code>((djapp:3.9)) {janek@serwer:djapp}$ python manage.py migrate<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Operations to perform:\nApply all migrations: admin, auth, contenttypes, sessions\nRunning migrations:\nApplying contenttypes.0001_initial\u2026 OK\nApplying auth.0001_initial\u2026 OK\nApplying admin.0001_initial\u2026 OK\nApplying admin.0002_logentry_remove_auto_add\u2026 OK\nApplying admin.0003_logentry_add_action_flag_choices\u2026 OK\nApplying contenttypes.0002_remove_content_type_name\u2026 OK\nApplying auth.0002_alter_permission_name_max_length\u2026 OK\nApplying auth.0003_alter_user_email_max_length\u2026 OK\nApplying auth.0004_alter_user_username_opts\u2026 OK\nApplying auth.0005_alter_user_last_login_null\u2026 OK\nApplying auth.0006_require_contenttypes_0002\u2026 OK\nApplying auth.0007_alter_validators_add_error_messages\u2026 OK\nApplying auth.0008_alter_user_username_max_length\u2026 OK\nApplying auth.0009_alter_user_last_name_max_length\u2026 OK\nApplying auth.0010_alter_group_name_max_length\u2026 OK\nApplying auth.0011_update_proxy_permissions\u2026 OK\nApplying auth.0012_alter_user_first_name_max_length\u2026 OK\nApplying sessions.0001_initial\u2026 OK<\/code><\/pre>\n\n\n\n<p><br>The next command is to add css styles to the django panel:<\/p>\n\n\n\n<p><code>python manage.py collectstatic<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>((djapp:3.9)) {janek@serwer:djapp}$ python manage.py collectstatic\n125 static files copied to '\/home\/janek\/public_html\/djdomena.pl\/djapp\/static'.<\/code><\/pre>\n\n\n\n<p><br>Add the user with the command:<\/p>\n\n\n\n<p><code>python manage.py createsuperuser<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>((djapp:3.9)) {janek@serwer:djapp}$ python manage.py createsuperuser\nNazwa u\u017cytkownika (leave blank to use 'janek'): janek\nAdres e-mail: janek@djdomena.pl\nPassword:\nPassword (again):\nSuperuser created successfully.\n((djapp:3.9)) {janek@serwer:djapp}$<\/code><\/pre>\n\n\n\n<p>when entering the password, nothing is shown on the console.<\/p>\n\n\n\n<p><br>We are almost at the very end!<\/p>\n\n\n\n<p><br>Restarting the application can be done in 2 ways: in cpanel by clicking on restart:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"353\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4.webp\" alt=\"\" class=\"wp-image-6492\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4.webp 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4-300x103.webp 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4-768x265.webp 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4-520x179.webp 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a4-740x255.webp 740w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>or create a restart.txt file and the server will reload itself<\/p>\n\n\n\n<p><code>touch tmp\/restart.txt<\/code><\/p>\n\n\n\n<p><br>We check our site should be a basic template like this one:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"556\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5.png\" alt=\"\" class=\"wp-image-6493\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5.png 965w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5-300x173.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5-768x442.png 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5-520x300.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a5-740x426.png 740w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/figure><\/div>\n\n\n<p class=\"has-text-align-center\">We can also log into the panel &#8211;  the address is the name of our domain + admin\/ ( Example https:\/\/djdomena\/admin\/ )<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"419\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a6.png\" alt=\"\" class=\"wp-image-6495\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a6.png 744w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a6-300x169.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a6-520x293.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/a6-740x417.png 740w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/figure><\/div>\n\n\n<p class=\"has-text-align-center\">We have reached the end there are still a few prompts left but that will be in the next article.<\/p>\n\n\n\n<p class=\"has-text-align-center\">Have fun with&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.djangoproject.com\/\" target=\"_blank\">django<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to the installation What you need to have: For the purposes of installation, the data required for the account We start by logging into cpanel and looking for Software -&gt; Setup Python App We<a class=\"read-more\" href=\"https:\/\/www.smarthost.au\/blog\/how-to-run-and-install-django-framework-on-smarthost\">Continue reading<\/a><\/p>\n","protected":false},"author":18,"featured_media":6500,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-6485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hosting"],"_links":{"self":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/comments?post=6485"}],"version-history":[{"count":4,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6485\/revisions"}],"predecessor-version":[{"id":6530,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6485\/revisions\/6530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media\/6500"}],"wp:attachment":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media?parent=6485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/categories?post=6485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/tags?post=6485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}