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

Posted star

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Writing your first Django app--2017年5月9日相关的知识,希望对你有一定的参考价值。

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.py
from django.db import models
import datetime
from 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日的主要内容,如果未能解决你的问题,请参考以下文章

02 Writing Your First Program 写你的第一个C程序

Error writing temporary file. Make sure your temp folder is valid

NSIS Error:Error writing temporary file. Make sure your temp folder is valid的解决办法

37. Django 2.1.7 中间件

My First Blog - How to choose a handy Markdown writing tool

phpunit Drupal7