标签 conda 下的文章

env configuration

on macos or linux , use conda to create a python env

conda create -n django python=3.12

conda activate django

# 查看python版本号是否OK

python --version

Python 3.12.12
# OK

how to select a python version

https://docs.djangoproject.com/zh-hans/5.2/faq/install/#faq-python-version-support

install django

pip install django

base python files

mkdir -p ~/django
cd ~/django

# create a new project named sample
django-admin startproject sample

cd sample

start to run base admin

python ./manage.py runserver
(django) ~/dev/django/sample $ python ./manage.py runserver
#Watching for file changes with StatReloader
#Performing system checks...

#System check identified no issues (0 silenced).

#You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
#Run 'python manage.py migrate' to apply them.
#November 02, 2025 - 10:08:12
#Django version 5.2.7, using settings 'sample.settings'
#Starting development server at http://127.0.0.1:8000/
#Quit the server with CONTROL-C.

#WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead.
#For more information on production servers see: https://docs.djangoproject.com/en/5.2/howto/deployment/

add a module such as category

python ./manage.py startapp category

# will create a folder named category and files lie views.py models.py etc.
cd category
ls

(django) ~/dev/django/sample/catalog $ ls
__init__.py admin.py    migrations  tests.py    views.py
__pycache__ apps.py     models.py   urls.py

set url map (route)

https://developer.mozilla.org/zh-CN/docs/Learn_web_development/Extensions/Server-side/Django/skeleton_website

create model (example from appsite)

from django.db import models

# Create your models here.
class CategoryModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="自增ID")
    uid = models.CharField(max_length=8, unique=True, null=False, verbose_name="分类ID")
    saasid = models.ForeignKey( to='saas.SaaSModel', to_field='uid', db_column='uid', on_delete=models.CASCADE, null=True, verbose_name="租户ID" )
    alias = models.CharField(max_length=24, unique=True, null=True, verbose_name="别称")

    title = models.CharField(max_length=64, null=False, verbose_name="分类名")
    authorid = models.CharField(max_length=8, null=True, db_index=True, verbose_name="创建人ID")
    parentid = models.CharField(max_length=8, null=True, db_index=True, verbose_name="上一级ID")
    type = models.CharField(max_length=32, null=True, verbose_name="类型")

    description = models.CharField(max_length=256, null=True, verbose_name="描述")
    cover = models.CharField(max_length=256, null=True, verbose_name="封面")

    status = models.CharField(max_length=12, null=False, default='enabled', verbose_name="状态 ")

    createtime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    lasttime = models.DateTimeField(auto_now=True, verbose_name="最后修改时间")
    featured = models.BooleanField(null=False, default=False, db_index=True, verbose_name="置顶")
    sort = models.IntegerField(null=False, default=0, db_index=True, verbose_name="优先排序")

    class Meta:
        db_table = "item_category"
        verbose_name = "通用分类"
        verbose_name_plural = "通用分类"

    def __str__(self):
        return self.title

    @classmethod
    def list_child(cls, uid, page=1, size=50, sort=None, more_filters=None):
        filters = more_filters if more_filters is not None else {}
        filters['parentid'] = uid
        query = cls.objects.filter(**filters)
        if sort:
            query = query.order_by(sort)
        start = (page - 1) * size
        end = start + size
        return query[start:end]

    @classmethod
    def count_child(cls, uid, more_filters=None):
        filters = more_filters if more_filters is not None else {}
        filters['parentid'] = uid
        return cls.objects.filter(**filters).count()

field types introduction

https://developer.mozilla.org/zh-CN/docs/Learn_web_development/Extensions/Server-side/Django/Models

references

MDN introduction / docs

https://developer.mozilla.org/zh-CN/docs/Learn_web_development/Extensions/Server-side/Django/Introduction

django official docs

https://docs.djangoproject.com/zh-hans/5.2/faq/install/#faq-python-version-support

Conda env

https://shezw.com/note/270/macos_conda