• 用django创建一个简单的sns


    用django创建一个简单的sns

    1.首先创建一个工程newsns

    django-admin.py startproject newsns

    在工程目录下新建一个文件夹templates,在该文件夹下创建一个模版index.html:

    <title>newsns -by django</title>
    <h1><font color=#{{color}}>Django, my first django program.</font></h1>

    参照上篇文章中创建模版的方法,将app目录下的settings.py文件、views.py文件和urls.py文件进行相应设置。

    2.创建数据库,配置数据

    在app目录下的settings.py中进行如下设置

    复制代码
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'newsns',                      # Or path to database file if using sqlite3.
            'USER': 'root',                      # Not used with sqlite3.
            'PASSWORD': '***',                  #密码.
            'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
    }
    复制代码

    增加app newsns:

    复制代码
     1 INSTALLED_APPS = (
     2     'django.contrib.auth',
     3     'django.contrib.contenttypes',
     4     'django.contrib.sessions',
     5     'django.contrib.sites',
     6     'django.contrib.messages',
     7     'django.contrib.staticfiles',
     8     # Uncomment the next line to enable the admin:
     9     # 'django.contrib.admin',
    10     # Uncomment the next line to enable admin documentation:
    11     # 'django.contrib.admindocs',
    12     'newsns',
    13 )
    复制代码

    在控制台中使用mysql命令创建数据库newsns

    mysql> create database newsns default character set utf8 collate utf8_general_ci;
    Query OK, 1 row affected (0.03 sec)

    然后在app目录下创建一个models.py文件,该文件创建了3个类,实际上每个类代表一个表格,类的字段为表的字段。

    复制代码
     1 from django.db import models
     2 
     3 class Publisher(models.Model):
     4     name = models.CharField(max_length=30)
     5     address = models.CharField(max_length=50)
     6     city = models.CharField(max_length=60)
     7     state_province = models.CharField(max_length=30)
     8     country = models.CharField(max_length=50)
     9     website = models.URLField()
    10 
    11 class Author(models.Model):
    12     first_name = models.CharField(max_length=30)
    13     last_name = models.CharField(max_length=40)
    14     email = models.EmailField()
    15 
    16 class Book(models.Model):
    17     title = models.CharField(max_length=100)
    18     authors = models.ManyToManyField(Author)
    19     publisher = models.ForeignKey(Publisher)
    20     publication_date = models.DateField()
    复制代码

    在控制台输入以下命令,按照要求输入帐号、密码,建立上述表格:

    ***@Android:~/newsns# python manage.py validate
    0 errors found
    ***@Android:~/newsns# python manage.py syncdb

    创建好表格后我们在manage.py shell中进行测试:

    复制代码
     1 ***@Android:~/newsns# python manage.py shell
     2 Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
     3 Type "copyright", "credits" or "license" for more information.
     4 
     5 IPython 0.13.1 -- An enhanced Interactive Python.
     6 ?         -> Introduction and overview of IPython's features.
     7 %quickref -> Quick reference.
     8 help      -> Python's own help system.
     9 object?   -> Details about 'object', use 'object??' for extra details.
    10 
    11 In [1]: from newsns.models import Publisher
    12 
    13 In [2]: p = Publisher(name = 'Jim', city = 'beijing')
    14 
    15 In [3]: p.save()
    16 
    17 In [4]: l = Publisher.objects.all()
    18 
    19 In [5]: for n in l:
    20    ...:     print n.name
    21    ...:     
    22 Jim
    复制代码

    好了,测试无误。

     
     
    分类: python
    标签: django
  • 相关阅读:
    mac配置git和github
    更新hosts后,如何清理edge的DNS缓存
    初次运行 Git 前的配置
    mac安装homebrew
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    AJAX
    HTTP协议
    HTTP状态码大全
    SQL Servers数据库_删除
    Hello Word
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3229349.html
Copyright © 2020-2023  润新知