Writing your first Django app--2017 May 9

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.