django 创建可重用的app
重要目的是打包自己的django app 为一个pip 包,实现应用的复用,以下是一个简单的demo
说明对于django project 集成admin 需要自己创建登陆账户的,以下没有说明
创建一个简单的django project
- 命令
django-admin startproject demoapp
- 配置static 以及templates
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = '/Users/dalong/mylearning/mystatic'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
创建一个简单的app
- 命令
python manage.py startapp userlogin
- 效果
- 编写简单的demo
views.py
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render
from . import models
def index(request):
record = models.Question(question_text="Instance #1", pub_date="2020-01-01")
record.save()
return render(request, "userlogin/login.html")
models.py
from django.db import models
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
admin.py
主要是注册model 方便admin 进行管理
from django.contrib import admin
# Register your models here.
from . import models
admin.site.register(models.Question)
login.html
{% load static %}
<html>
<head >
<link rel="stylesheet" type="text/css" href="{% static 'userlogin/app.css' %}">
</head>
<div>
this is userlogin demo app
</div>
</html>
- 配置install apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'userlogin'
]
- 运行
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
- 效果
打包app
打包app 实际上就是创建一个pip 包,官方的建议是使用独立的一个文件夹,我们可以copy 一份userlogin 的代码, 然后参考pip 包
的模式进行修改就可以了
- 需要的准备
README.rst,MANIFEST.in,setup.cfg,setup.py
README.rst 是关于说明的,MANIFEST.in 是关于静态资源以及模版的,setup.cfg 是关于pip 包定义的,setup.py 是pip
包的标准定义,LICENSE 关于许可的, 实际上setup.cfg可以不需要直接使用setup.py 就可以了,而且可以方便的管理依赖,配置即代码
README.rst
=====
userlogins
=====
Quick start
-----------
1. Add "polls" to your INSTALLED_APPS setting like this::
INSTALLED_APPS = [
'userlogin',
]
MANIFEST.in
include LICENSE
include README.rst
recursive-include userlogin/static *
recursive-include userlogin/templates *
setup.cfg
[metadata]
name = django-userlogin
version = 0.1
description = A Django app to conduct Web-based polls.
long_description = file: README.rst
url = https://github.com/rongfengliang
author = dalong
author_email = dalongrong@qq.com
license = BSD-3-Clause # Example license
classifiers =
Environment :: Web Environment
Framework :: Django
Framework :: Django :: X.Y # Replace "X.Y" as appropriate
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Internet :: WWW/HTTP
Topic :: Internet :: WWW/HTTP :: Dynamic Content
[options]
include_package_data = true
packages = find:
setup.py
from setuptools import setup
setup()
- 打包pip 包
python setup.py sdist
效果
使用pip 包
使用pip 包的方法很简单,就是创建一个新的项目,然后使用就可以了
- 创建新的django 项目&& 配置模版
django-admin startproject dalongrong
配置都是老样子
- 安装pip 包
pip install django-userlogin-0.1.tar.gz
- 添加app 到install_apps
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dalongrong.apps.DalongrongConfig',
"userlogin"
]
- 运行
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
- 效果
说明
以上是一个简单的开发可重用django app 的说明,核心还是关于pip 包的管理,django 的app 能力还是很强大的,我们基于此
可以开发灵活的软件应用
参考资料
https://docs.djangoproject.com/en/3.1/intro/reusable-apps/
https://www.cnblogs.com/rongfengliang/p/10219292.html