...
| Code Block |
|---|
$ cd my_project |
STEP 1. Create an application
In order to create an app, you will need "manage.py" created by Django. Once you verify it, you can create "online poll" app by running below command.
...
If you face an error like below, you should check if your python is properly installed and django is running on virtual environment like I mentioned at "1. How do I setup Django development environment on Mac?" or not.
STEP 2. Database setup
If everything goes fine, now is time for us to setup a database where can store poll results. Let us do it step by step.
Open up settings.py on your project, and you can see the default database setting like below
Code Block # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }If you want to use mysql instead of sqlite3, you can use it by replacing as "django.db.backends.mysql". For oracle, it will be "django.db.backends.oracle".
If you are not using SQLite as your database, additional settings such as USER, PASSWORD, and HOST must be added. For more details, see the reference documentation for DATABASES.
Example)Code Block DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'USER': 'mydatabaseuser', 'NAME': 'mydatabase', 'TEST': { 'NAME': 'mytestdatabase', }, }, }Once you set all your requirements, you should run following command.
Code Block python manage.py migrateAs a next step, what we need to do is creating a model for online poll.
In our simple poll app, we’ll create two models:
QuestionandChoice. AQuestionhas a question and a publication date. AChoicehas two fields: the text of the choice and a vote tally. EachChoiceis associated with aQuestion.These concepts are represented by simple Python classes. Edit the
polls/models.pyfile so it looks like this:Code Block title polls/models.py from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
