Django Helloworld

2021-01-05 10:36:40 浏览数 (1)

【极简入魂】道理有很多MVC/MTV还有ORM,先动手吧,这是个官方的小例子。

代码语言:shell复制
pip install django
django-admin startproject xxx
django-admin startapp xx
vi ./xxx/settings.py
# installed app   xx
# allow ip ['*']
代码语言:python代码运行次数:0复制
# models.py
from django.db import models

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)

    def __str__(self):
        return self.full_name

class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline
代码语言:shell复制
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8080

【历史故事】似乎Python从诞生起就是各种传奇,Django 从一个非常实际的需求成长而来:World Web 是一家新闻网站,负责在新闻截止期限内建立密集的 Web 应用程序。 在快节奏的新闻编辑室,World Online 往往需要几个小时内将一个复杂的 Web 应用程序从概念推向发布上线。

与此同时,World Online 的 Web开发者在 Web 开发的最佳实践方面一直是完美主义者。

在2003年秋季,世界在线开发者(Adrian Holovaty 和 Simon Willison)放弃 PHP 而开始使用 Python 开发网站。 当他们构建了密集的,强互动性的网站时(如 Lawrence.com),他们开始提取一个通用的 Web 开发框架,使他们可以更快地构建 Web 应用程序。 两年内他们经常调整和改进这个框架。

接下来放一个实践过的小例子

代码语言:python代码运行次数:0复制
# Initialization
pip install django
# -i https://pypi.tuna.tsinghua.edu.cn/simple
# --proxy http://USERNAME:PASSWORD@PROXYIP:PORT
Django-admin startproject PRJ_NAME
Python manage.py startapp APP_NAME
## settings.py -> installed app   allow ip
## development of models.py url.py views.py admin.py
 
# Build
Python manage.py migrate
Python manage.py createsuperuser
Python manage.py makemigrations APP_NAME
# python manage.py migrate –fake (MARK FIRST) 
Python manage.py runserver 0.0.0.0:8080

# Keep Online
# ps -aux | grep python| xargs kill -9
# nohup python3 manage.py runserver 0.0.0.0:8080 >djo.out 2>&1 &

# ===== PYTHON ===== #
# models.py
class T2_HU_INFO(models.Model):
    key_id = models.CharField(max_length=30,verbose_name="主键", primary_key=True)
    vin = models.CharField(max_length=20, verbose_name="车辆识别码")
    sw_version = models.CharField(max_length=200, verbose_name="软件版本号")
    hw_version = models.CharField(max_length=200, verbose_name="硬件版本号")
    hu_id = models.CharField(max_length=20, verbose_name="HU唯一标识")
    supplier_code = models.CharField(max_length=20, verbose_name="供应商代号")
    create_time = models.DateTimeField(verbose_name="创建时间", default = timezone.now)
    last_login_time = models.DateTimeField(verbose_name="创建时间", default = timezone.now)
    class Meta: verbose_name = "T2_HU_INFO-HU信息"; verbose_name_plural = verbose_name

class T2_HU_ACTION_DATA(models.Model):
    key_id = models.CharField(max_length=30,verbose_name="主键", primary_key=True)
    vin = models.CharField(max_length=20, verbose_name="车辆识别码")
    create_time = models.DateTimeField(verbose_name="上报时间", default = timezone.now)
    action_id = models.CharField(max_length=200, verbose_name="应用ID逻辑外键--流量管理-应用信息")
    count = models.IntegerField(verbose_name="触发次数")
    content = models.CharField(max_length=200, verbose_name="内容")
    action_id_fast = models.CharField(max_length=100, verbose_name="应用ID 逻辑外键--流量管理-应用信息_FAST")
    class Meta: verbose_name = "T2_HU_ACTION_DATA-HU操作信息收集"; verbose_name_plural = verbose_name

class T2_HIS_TRAJECTORY_LAST_TIME(models.Model):
    vin = models.CharField(max_length=20,verbose_name="车辆识别码", primary_key=True)
    last_time = models.CharField(max_length=30,verbose_name="最后统计时间")
    class Meta: verbose_name = "T2_HIS_TRAJECTORY_LAST_TIME-最后历史轨迹时间"; verbose_name_plural = verbose_name

# views.py
from django.http import JsonResponse
def pack_json(query_obj):
	ret={}; ret["data"]=list(query_obj.objects.all().values())
	return JsonResponse(ret, safe=False, json_dumps_params={'ensure_ascii':False})

def _init(request):
	print('STARTING...')
	for single in tests.DT_T2_HIS_TRAJECTORY_LAST_TIME:
		T2_HIS_TRAJECTORY_LAST_TIME.objects.create(vin=single[0], last_time=single[1])
		print(single[0])
	print('T2_HIS_TRAJECTORY_LAST_TIME LOADED !')
	for single in tests.DT_T2_HU_INFO:
		T2_HU_INFO.objects.create(key_id=single[0],
								 vin=single[1],
								 sw_version=single[2],
								 hw_version=single[3],
								 hu_id=single[4],
								 supplier_code=single[5],
								 create_time=single[6],
								 last_login_time=single[7])
		print(single[0])
	print('T2_HU_INFO LOADED !')
	for single in tests.DT_T2_HU_ACTION_DATA:
		T2_HU_ACTION_DATA.objects.create(key_id=single[0],
								 vin=single[1],
								 create_time=single[2],
								 action_id=single[3],
								 count=single[4],
								 content=single[5],
								 action_id_fast=single[6])
		print(single[0])
	print('T2_HU_ACTION_DATA LOADED !')
def demo1(request): return pack_json(T2_HIS_TRAJECTORY_LAST_TIME)
def demo2(request): return pack_json(T2_HU_INFO)
def demo3(request): return pack_json(T2_HU_ACTION_DATA)

# admin.py
from django.contrib import admin
from APP_NAME.models import *

admin.site.register(T2_HU_INFO)
admin.site.register(T2_HIS_TRAJECTORY_LAST_TIME)
admin.site.register(T2_HU_ACTION_DATA)

运行起来

代码语言:shell复制
python manage.py runserver 0.0.0.0:8080
open http://localhost:8080

看上去很简单,貌似也没什么用,

但我认为,这是个非常伟大的框架。

0 人点赞