Writing your first Django app, part 1
Requriments:django 1.8 and Python 2.7
Create Project Django-admin Startproject MySite
.
├──manage.py command Line function
Python Package for └──mysite project
├──__init__.py tells Python that this directory is a python package
├──settings.py configuration file
URLs supported by ├──urls.py
└──wsgi.py compatible with WSGI Web service entry points
Change the database in the settings file to change the databases
Build table Python manage.py migrate
Create app Python manage.py startapp polls
Python manage.py sqlmigrate Polls 000
The sqlmigrate command does not actually run the migration on the database, it just prints to the screen and is used to check what Django is doing.
If you change the model, you need to update the table
Python manage.py makemigrations:create migrations for those changes
Python manage.py migrate:apply Those changes to the database.
Python manage.py Shell
manage.py Set the DJANGO_SETTINGS_MODULE environment variable, which gives DJANGO the Python import path to your mysite/settings.py file.
#mysite/polls/models.py fromDjango.dbImportModelsImportdatetime fromDjango.utilsImportTimeZone#Create your models here.classQuestion (models. Model): Question_text= Models. Charfield (max_length=200) Pub_date= Models. Datetimefield ('Date Published') def __str__(self):returnSelf.question_textdefwas_published_recently (self):returnSelf.pub_date >= Timezone.now ()-Datetime.timedelta (Days=1)classChoice (models. Model): Question=models. ForeignKey (Question) Choice_text= Models. Charfield (max_length=200) Votes= Models. Integerfield (default=0)def __str__(self):returnSelf.choice_text
Writing your first Django app, part 2
Writing your first Django app--2017 May 9