Writing your first Django app--2017年5月9日

來源:互聯網
上載者:User

標籤:ret   str   rate   服務   apply   char   one   tab   module   

Writing your first Django app, part 1

requriments : Django 1.8 and Python 2.7

建立項目django-admin startproject mysite
.
├── manage.py 命令列呢功能
└── mysite 項目的python包
├── __init__.py 告訴python這個目錄是個python包
├── settings.py 設定檔
├── urls.py 支援的url
└── wsgi.py 相容WSGI的web服務的進入點
更改資料庫在settings檔案中更改DATABASES
建表python manage.py migrate
建立app python manage.py startapp polls

python manage.py sqlmigrate polls 000
sqlmigrate命令實際上並不在資料庫上運行遷移,它只是列印到螢幕上,是用於檢查Django要做什麼。

如果更改了model,即需要更新表
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 設定了 DJANGO_SETTINGS_MODULE 環境變數,which gives Django the Python import path to your mysite/settings.py file.

# mysite/polls/models.pyfrom django.db import modelsimport datetimefrom django.utils import timezone# Create your models here.class Question(models.Model):    question_text = models.CharField(max_length=200)    pub_date = models.DateTimeField(‘date published‘)    def __str__(self):        return self.question_text    def was_published_recently(self):        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)class Choice(models.Model):    question = models.ForeignKey(Question)    choice_text = models.CharField(max_length=200)    votes = models.IntegerField(default=0)    def __str__(self):        return self.choice_text

Writing your first Django app, part 2

Writing your first Django app--2017年5月9日

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.