zoukankan      html  css  js  c++  java
  • Django 笔记 1

    一、在django中创建工程

    django-admin.py startproject mysite

    于是生成了一个mysite文件夹,里面有manage.py(是一个command line utility),和另一个mysite文件夹,这个才是这个工程真正的python包,import的时候就是用的这个名字。
    mysite/__init__.py就是告诉python这个文件夹是一个python package;
    mysite/settings.py是为这个project进行设置的地方;
    mysite/wsgi.py是使用WSGI的地方;
    启动的时候就是python manage.py runserver(或者指定其他端口号)。

    二、建立并同步数据库

    在settings.py里将DATABASES ENGINE更改为"django.db.backends.#" (#为sqlite3,mysql,oracle或者postgresql)
    如果使用mysql,需要下载 mysql-python: http://sourceforge.net/projects/mysql-python/
    然后设置Name,即数据库名,如果是sqlite3,就写数据库文件的路径。

    接下来设置USER,PASS,HOST和PORT,即数据库的用户名,密码,ip和端口号。

    设置完数据库之后需要同步数据库,就是建立相应的表格。

    python manage.py syncdb

    这条语句将查看INSTALLED_APPS然后创建需要的数据库表。

    三、创建一个application

    首先要注意application需要放在Python的path里面。需要注意的是每一个application都是可插拔的,就好像作为一个插件嵌入到project里面一样。

    python manage.py startapp polls

    就建成了polls这个application。django为我们生成了一个polls的文件夹,接下来修改里面的models.py:

    from django.db import models
    class Poll(models.Model):
      question = models.CharField(max_length=200)
      pub_date = models.DateTimeField(’date published’)
    class Choice(models.Model):
      poll = models.ForeignKey(Poll)
      choice_text = models.CharField(max_length=200)
      votes = models.IntegerField()

    这里建立了两个Model:Poll和Choice。Model中每一个Field都将会作为database表的一个column,使用外键时,用ForeignKey指明。

    将这个application添加到settings.py文件里的INSTALLED_APP选项里面,然后执行 python manage.py sql polls ,将会生成database schema(也就是为每个Model建立table),而且每一个表里面都会有一个id。

    其他非常有意思的命令:
    python manage.py validate 检查一下在建model的过程中有没有出错
    python manage.py sqlcunstom polls 输出sql 语句
    python manage.py sqlclear polls 输出 drop table语句
    python manage.py sqlindexes polls 输出 create index语句
    python manage.py sqlall polls 上面的集合

    四、使用Python shell 和 api

    python manage.py shell

    这样的启动shell的方式比之间输入python多了一个读取django环境变量的过程。
    在class下面添加__unicode__方法,以便在使用objects.all方法时显示有用的内容,而不是Object。
    使用database api:

    >>> from polls.models import Poll, Choice # Import the model classes we just wrote.
    # No polls are in the system yet.
    >>> Poll.objects.all()
    []
    # Create a new Poll.
    # Support for time zones is enabled in the default settings file, so
    # Django expects a datetime with tzinfo for pub_date. Use timezone.now()
    # instead of datetime.datetime.now() and it will do the right thing.
    >>> from django.utils import timezone
    >>> p = Poll(question="What’s new?", pub_date=timezone.now())
    # Save the object into the database. You have to call save() explicitly.
    >>> p.save()
    # Now it has an ID. Note that this might say "1L" instead of "1", depending
    # on which database you’re using. That’s no biggie; it just means your
    # database backend prefers to return integers as Python long integer
    # objects.
    >>> p.id
    1
    # Access database columns via Python attributes.
    >>> p.question
    "What’s new?"
    >>> p.pub_date
    datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
    # Change values by changing the attributes, then calling save().
    >>> p.question = "What’s up?"
    >>> p.save()
    # objects.all() displays all the polls in the database.
    >>> Poll.objects.all()
    [<Poll: "What's up">]

    然后添加一个自定义的方法:

    def was_published_recently(self):
      return self.pub_date >= timezone.now() -  datetime.timedelta(days=1)

    然后就可以在shell下各种调用这个方法了,这里就不再赘述了。

  • 相关阅读:
    MapKit 大头针基本使用
    iOS获取文件的大小
    iOS截取图片方法
    iOS通知3种使用方法
    iOS12适配及兼容问题解决,xcode10问题
    Mac不使用iTunes导出照片
    Xcode 10 项目迁移 Multiple commands produce...
    tableView在UITableViewStylePlain的状态下取消悬浮效果(取消粘性效果)
    OC 字符串和字典的相互转化
    破解 Mac OS管理员密码 亲测可用
  • 原文地址:https://www.cnblogs.com/cubika/p/2764861.html
Copyright © 2011-2022 走看看