{"id":23032,"date":"2021-02-15T17:41:34","date_gmt":"2021-02-15T08:41:34","guid":{"rendered":"https:\/\/www.waca.associates\/en\/?p=23032"},"modified":"2021-02-26T00:27:36","modified_gmt":"2021-02-25T15:27:36","slug":"kit-how-to-build-an-application-django","status":"publish","type":"post","link":"https:\/\/www.waca.or.jp\/en\/web_marketing\/kit-how-to-build-an-application-django\/","title":{"rendered":"[KIT] How to build an application ( Django)"},"content":{"rendered":"<p>\n\nAll images are uploaded from Pixabay.com\n\n<\/p><p>Python is one of the most popular languages nowadays as its popularity keeps increasing days by days. Django is a python-based open source web framework which follows the MVT ( Model View Template) patterns. In this article, we will show you how to build a polls application using Django web framework in order to solidarize your understanding towards this particular framework. ( Everything in this article is written using my knowledge that I have regarding Django framework so if there is something which is inappropriate or not necessary, please let me know )<br><\/p><p>Firstly, go to your favorite integrated development environment ( IDE ) and create a new project. I am using conda virtual environment anyway and if you\u2019re not familiar with it, please have a look at it to ensure that u will understand everything throughout the walkthrough in this article.<br><\/p><p>Activate your conda virtual environment by running this command:&nbsp;<br><\/p><p><strong>conda activate myenv<\/strong><br><\/p><p>Then everything is ready. Run this command in your terminal in order to start our django project.<br><\/p><p><strong>django-admin startproject mysite<\/strong><br><\/p><p>You should see a mysite directory in your directory tree structure now so go into that directory and run a command to start our application.<br><\/p><p><strong>python manage.py startapp polls<\/strong><br><\/p><p>Now you will also see a polls directory in our tree structure. That directory will house our polls application.<br><\/p><p>Well let\u2019s try running our server and see whether it is working or not by using this command:<\/p><p><strong>python manage.py runserver<\/strong><br><\/p><p>Now, go into the polls directory and create a file called urls.py. urls.py is used to store our polls application urls. Put this code in urls.py:<br><\/p><p>polls\/urls.py<\/p><p><strong>from django.urls import path<\/strong><br><\/p><p><strong>from . import views<\/strong><br><\/p><p><strong>urlpatterns = [<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;path(&#8216;polls&#8217;, views.index, name=&#8217;index&#8217;),<\/strong><\/p><p><strong>]<\/strong><br><\/p><p>The next step is to point the root URLconf of our Django application at the polls.urls module<br><\/p><p><strong>from django.contrib import admin<\/strong><\/p><p><strong>from django.urls import include, path<\/strong><br><\/p><p><strong>urlpatterns = [<\/strong><\/p><p>&nbsp;&nbsp;<strong>&nbsp;&nbsp;path(&#8221;, include(&#8216;polls.urls&#8217;)),<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;path(&#8216;admin\/&#8217;, admin.site.urls),<\/strong><\/p><p><strong>]<\/strong><br><\/p><p>Now, let\u2019s create a model for our application, paste this code to our models.py in our polls, directory<br><\/p><p><strong>from django.db import models<\/strong><\/p><p><\/p><p><strong>class Question(models.Model):<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;question_text = models.CharField(max_length=200)<\/strong><\/p><p><\/p><p><strong>class Answer(models.Model):<\/strong><\/p><p>&nbsp;&nbsp;&nbsp;<strong>&nbsp;question = models.ForeignKey(Question, on_delete=models.CASCADE)<\/strong><\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>choice_text = models.CharField(max_length=200)<\/strong><\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>votes = models.IntegerField(default=0)<\/strong><br><\/p><p>After all of these now let\u2019s run these 2 commands in order to save our changes into the database.<\/p><p><strong>Python manage.py makemigrations<\/strong><\/p><p><strong>Python manage.py migrate<\/strong><br><\/p><p>Okay before moving further, let\u2019s have a quick recap of what we were doing. Firstly, we create a conda virtual environment for our Django application. Virtual environment is necessary for our project because package separation for different projects is necessary in order to avoid package chaos and virtual environment did just that. Then we run a command to create our project structure and our application structure. After that we create a urls.py file in our poll directory so that we can have a place to store our poll application url as there will be many applications in a single Django project so storing it at root url config is not advised at all. Next we create a model which is just another way of saying creating a table to store data in our database, it\u2019s just that Django got ORM which is very convenient.<br><\/p><p>This is what my current knowledge can offer but we&#8217;ll definitely continue where we&#8217;re leave off once I finished my courses!<\/p>","protected":false},"excerpt":{"rendered":"All images are uploaded from Pixabay.com Python is one of the most popular languages nowadays as its popularity keeps increasing days by days. Django is a python-based open source web framework which follows the MVT ( Model View Template) patterns. In this article, we will show you how to build a polls application using Django web framework in order to solidarize your understanding towards this particular framework. ( Everything in this article is written using my knowledge that I have regarding Django framework so if there is something which is inappropriate or not necessary, please let me know ) Firstly, go to your favorite integrated development environment ( IDE ) [&hellip;]","protected":false},"author":668,"featured_media":23035,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[175,169,19,74,18],"tags":[],"class_list":["post-23032","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-growth-hacking-course","category-growthhacking","category-web_analytics","category-web-analytics","category-web_marketing"],"jetpack_featured_media_url":"https:\/\/www.waca.or.jp\/en\/wp-content\/uploads\/2021\/02\/django-339744_1280.png","_links":{"self":[{"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/posts\/23032"}],"collection":[{"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/comments?post=23032"}],"version-history":[{"count":3,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/posts\/23032\/revisions"}],"predecessor-version":[{"id":24790,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/posts\/23032\/revisions\/24790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/media\/23035"}],"wp:attachment":[{"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/media?parent=23032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/categories?post=23032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.waca.or.jp\/en\/wp-json\/wp\/v2\/tags?post=23032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}