Skip to content

Commit

Permalink
write article model
Browse files Browse the repository at this point in the history
  • Loading branch information
stacklens committed Aug 26, 2018
1 parent 5e04f34 commit d1ad598
Show file tree
Hide file tree
Showing 25 changed files with 281 additions and 0 deletions.
Empty file added article/__init__.py
Empty file.
Binary file added article/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added article/__pycache__/admin.cpython-37.pyc
Binary file not shown.
Binary file added article/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file added article/__pycache__/urls.cpython-37.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions article/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions article/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ArticleConfig(AppConfig):
name = 'article'
32 changes: 32 additions & 0 deletions article/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.1 on 2018-08-24 16:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('body', models.TextField()),
('created', models.DateTimeField(default=django.utils.timezone.now)),
('updated', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('-created',),
},
),
]
Empty file added article/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions article/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.db import models

# Django本身具有一个简单又完整的账号系统(User),足以满足一般网站的账号申请、建立、权限、群组等基本功能
# 因此这里导入内建的User模型,以便使用。
from django.contrib.auth.models import User
# timezone 用于处理时间相关事务。
from django.utils import timezone

# Django中所有的模型(Model)都必须继承django.db.models.Model模型,即顶部的导入
# 建立博客文章类 class Article,处理与文章有关的数据,它包含需要的字段和保存数据的行为
class Article(models.Model):

# 定义文章作者。 author 通过 models.ForeignKey 外键与内建的 User 模型关联在一起
# 参数 on_delete 用于指定数据删除的方式,避免两个关联表的数据不一致。通常设置为 CASCADE 级联删除就可以了
author = models.ForeignKey(User, on_delete=models.CASCADE)

# 文章标题。
# models.CharField 为字符串字段,用于保存较短的字符串,比如标题
# CharField 有一个必填参数 max_length,它规定字符的最大长度
title = models.CharField(max_length=100)

# 文章正文。
# 保存大量文本使用 TextField
body = models.TextField()

# 文章创建时间。
# DateTimeField 为一个日期字段
# 参数 default=timezone.now 指定其在创建数据时将默认写入当前的时间
created = models.DateTimeField(default=timezone.now)

# 文章更新时间。
# 参数 auto_now=True 指定每次数据更新时自动写入当前时间
updated = models.DateTimeField(auto_now=True)

# 内部类 class Meta 用于给 model 定义元数据
# 元数据:不是一个字段的任何数据
class Meta:
# ordering 指定模型返回的数据的排列顺序
# '-created' 表明数据应该以倒序排列
ordering = ('-created',)

# 函数 __str__ 定义当调用对象的 str() 方法时的返回值内容
# 它最常见的就是在Django管理后台中做为对象的显示值。因此应该总是为 __str__ 返回一个友好易读的字符串
def __str__(self):
# return self.title 将文章标题返回
return self.title
3 changes: 3 additions & 0 deletions article/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions article/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 引入path
from django.urls import path

# 正在部署的应用的名称
app_name = 'article'

urlpatterns = [
# 目前还没有urls
]

3 changes: 3 additions & 0 deletions article/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Binary file added db.sqlite3
Binary file not shown.
15 changes: 15 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_blog.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Empty file added my_blog/__init__.py
Empty file.
Binary file added my_blog/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added my_blog/__pycache__/settings.cpython-37.pyc
Binary file not shown.
Binary file added my_blog/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added my_blog/__pycache__/wsgi.cpython-37.pyc
Binary file not shown.
122 changes: 122 additions & 0 deletions my_blog/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""
Django settings for my_blog project.
Generated by 'django-admin startproject' using Django 2.1.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '__=s9@oixaun$x^g7-4#10wf_*7zvb8)kl1$j82fj&cyq%^o^3'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'article',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'my_blog.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'my_blog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'
26 changes: 26 additions & 0 deletions my_blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""my_blog URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
# 记得引入include
from django.urls import path, include

# 存放了映射关系的列表
urlpatterns = [
path('admin/', admin.site.urls),

# 新增代码,配置app的url
path('article/', include('article.urls', namespace='article')),
]
16 changes: 16 additions & 0 deletions my_blog/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for my_blog project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_blog.settings')

application = get_wsgi_application()

0 comments on commit d1ad598

Please sign in to comment.