zoukankan      html  css  js  c++  java
  • Django学习笔记(一)

    之前没有接触过django,甚至python也是最近才刚刚着手学习,可以说是零基础。
    要学习一门新技术,官方文档自然是首选的入门教程。
    开发环境:python2.7+django1.7+win

    1.首先,新建一个网站
    django-admin.exe startproject mysite
    2.创建数据表(一些应用需要基本的数据表,在调用之前使用此命令创建数据表。)
    python manage.py migrate
    3.1时区设置
    TIME_ZONE = 'Asia/Shanghai'

    3.2本地启动
    python manage.py runserver
    更改端口号或IP地址
    $ python manage.py runserver 8080
    $ python manage.py runserver 0.0.0.0:8000
    4.创建应用(创建后在setting中注册)
    python manage.py startapp polls
    4.1创建两个模型Quetion和Choice,编写polls/models.py
    from django.db import models


    class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


    class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    5.1在setting中注册应用后,还需要运行以下命令使django知道添加了polls这个app
    $ python manage.py makemigrations polls
    通过此命令可告知django对你的模型做了哪些改动
    5.2
    通过以下命令查看生成的数据表的SQL语句样式。
    $ python manage.py sqlmigrate polls 0001
    通过此命令检查项目中没有创建数据表的改动地方
    python manage.py check
    5.3
    创建一个管理员
    $ python manage.py createsuperuser
    pip freeze查看已安装模块及其版本

    pip uninstall django删除安装的对应模块
    pip install -U setuptools升级对应的模块
    6.
    Run python manage.py makemigrations to create migrations for those changes
    Run python manage.py migrate to apply those changes to the database.
    7.polls/models.py

    from django.db import models
    
    class Question(models.Model):
    # ...
    def __str__(self): # __unicode__ on Python 2
    return self.question_text
    
    class Choice(models.Model):
    # ...
    def __str__(self): # __unicode__ on Python 2
    return self.choice_text
    For:
    It’s important to add __str__() methods to your models,
    not only for your own sanity when dealing with the interactive prompt, 
    but also because objects’ representations are used throughout Django’s
    automatically-generated admin.
    
     
  • 相关阅读:
    poj3693 Maximum repetition substring (后缀数组+rmq)
    spoj687 REPEATS
    bzoj3626: [LNOI2014]LCA (树链剖分+离线线段树)
    bzoj2243 [SDOI2011]染色 (树链剖分+线段树)
    SPOJ QTREE- Query on a tree (树链剖分)
    hdu5662 YJQQQAQ and the function (单调栈)
    hdu4348 To the moon (主席树 || 离线线段树)
    hdu3565 Bi-peak Number (有上界和下界的数位dp)
    修改文件上传大小限制
    强制不按行
  • 原文地址:https://www.cnblogs.com/code-charmer/p/4009784.html
Copyright © 2011-2022 走看看