{"id":3422,"date":"2020-06-10T10:36:27","date_gmt":"2020-06-10T10:36:27","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3422"},"modified":"2020-06-10T11:53:53","modified_gmt":"2020-06-10T11:53:53","slug":"setup-django-with-python-3-in-ubuntu-18-x","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2020\/06\/10\/setup-django-with-python-3-in-ubuntu-18-x\/","title":{"rendered":"Setup Django with Python 3 in Ubuntu 18.x"},"content":{"rendered":"\n<p><strong>1.Install Python3<\/strong> <strong>and PIP<\/strong><\/p>\n\n\n\n<p><code>sudo apt-get install python3 python3-pip<\/code><\/p>\n\n\n\n<p>Check Python version by typing <code>python3 -V<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Check PIP version by typing <code>pip3 -V<\/code><\/p>\n\n\n\n<p><strong>2.Install Django<\/strong><\/p>\n\n\n\n<p><code>sudo apt install python3-django<\/code><\/p>\n\n\n\n<p>Check the version of Django by typing <code>django-admin --version<\/code><\/p>\n\n\n\n<p><strong>3.Create Sample Django Project<\/strong><\/p>\n\n\n\n<p>To test the newly installed Django server , we will create a demo webapp and run it. Change the directory to where you want to create the project. eg <code>cd \/var\/www<\/code><\/p>\n\n\n\n<p>Type <code>django-admin startproject testsite<\/code><\/p>\n\n\n\n<p>This will create a new directory called testsite and install the default Django files in it. If you do a <code>cd testsite<\/code> and list the contents, you will find another folder called testsite and files within it. Change the directory to the inner testsite and edit the <code>settings.py<\/code> file.<\/p>\n\n\n\n<p>Change the ALLOWED_HOSTS  line to something like<code> ALLOWED_HOSTS = [\"localhost\"]<\/code> and save the file<\/p>\n\n\n\n<p>The value entered in ALLOWED_HOSTS is the domain url that you will call in the browser to load the site. So if you want to use the server IP address put <code>ALLOWED_HOSTS=[\"x.x.x.x\"]<\/code>. Instead of localhost you can put 127.0.0.1 as well. but then you cannot use http:\/\/localhost to load the site. &#8211; only http:\/\/127.0.0.1<\/p>\n\n\n\n<p><strong>4.Migrate Site Database<\/strong><\/p>\n\n\n\n<p>Django uses SQLite as its internal database to manage websites. Go to the root folder of your site eg. cd \/var\/www\/testsite and type<code> python3 manage.py migrate<\/code><\/p>\n\n\n\n<p><strong>5.Create Admin User<\/strong><\/p>\n\n\n\n<p>Type <code>python3 manage.py createsuperuser<\/code><\/p>\n\n\n\n<p>Enter your username, email address and password<\/p>\n\n\n\n<p><strong>6.Launch The Project<\/strong><\/p>\n\n\n\n<p><code>Type python3 manage.py runserver 0.0.0.0:8000<\/code><\/p>\n\n\n\n<p>The webapp is now running on port 8000. You can view it by opening <code>http:\/\/localhost:8000 <\/code>in your browser.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"514\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines.-940x514.png\" alt=\"\" class=\"wp-image-3426\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines.-940x514.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines.-620x339.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines.-300x164.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines.-768x420.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Django-the-Web-framework-for-perfectionists-with-deadlines..png 1252w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>To view the admin site load <code>http:\/\/localhost:8000\/admin<\/code> . Login using the admin credentials you created above.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"514\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin-940x514.png\" alt=\"\" class=\"wp-image-3427\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin-940x514.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin-620x339.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin-300x164.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin-768x420.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/06\/Log-in-Django-site-admin.png 1252w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>7.Stop the Server<\/strong><\/p>\n\n\n\n<p>Pressing Ctrl-C in the terminal window where django is running will kill the Django server and stop the project<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>1.Install Python3 and PIP sudo apt-get install python3 python3-pip Check Python version by typing python3 -V Check PIP version by typing pip3 -V 2.Install Django <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2020\/06\/10\/setup-django-with-python-3-in-ubuntu-18-x\/\" title=\"Setup Django with Python 3 in Ubuntu 18.x\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3429,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[298,297,11],"tags":[],"class_list":["post-3422","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-python","category-tutorials"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3422"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3422\/revisions"}],"predecessor-version":[{"id":3430,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3422\/revisions\/3430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3429"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}