django admin使用

本文将指导您如何在Django项目中配置关键组件,包括安装应用、设置URL路由、激活管理界面等基本步骤,帮助您快速搭建起一个功能完善的Web应用。

1.在settings.py文件的INSTALLED_APPS部分中,加入

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
     'django.contrib.admindocs',

 

2.在urls.py文件中urlpatterns中,加入(r'^admin/', include(admin.site.urls)),  同时从django.contrib引入admin

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^FirstDjango/', include('FirstDjango.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

 


3. 同步数据库,运行即可。 

 

Django 的 `admin.StackedInline` 用于在 Django Admin 界面中实现内联编辑功能,允许在父模型的管理页面直接编辑关联的子模型。以下是使用指南: ### 定义模型 首先,需要定义父模型和子模型,并建立它们之间的关联。例如,假设存在一个 `Product` 模型和一个 `ProductImage` 模型,`ProductImage` 与 `Product` 为一对多的关系: ```python # models.py from django.db import models class Product(models.Model): name = models.CharField(max_length=100) # 其他字段... def __str__(self): return self.name class ProductImage(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) image = models.ImageField(upload_to='product_images/') # 其他字段... def __str__(self): return f"Image for {self.product.name}" ``` ### 创建内联类 在 `admin.py` 文件中,创建一个继承自 `admin.StackedInline` 的类,用于定义内联编辑的子模型。同时,指定子模型以及其他可选参数,如额外显示的空表单数量: ```python # admin.py from django.contrib import admin from .models import Product, ProductImage class ProductImageInline(admin.StackedInline): model = ProductImage extra = 0 # 额外显示的空表单数量 ``` ### 注册父模型并添加内联类 将父模型注册到 Django Admin 中,并在父模型的管理类中添加内联类,这样在父模型的管理页面就能直接编辑关联的子模型: ```python # admin.py class ProductAdmin(admin.ModelAdmin): inlines = [ProductImageInline] admin.site.register(Product, ProductAdmin) ``` ### 可选:自定义内联类 可根据需求进一步自定义内联类,例如设置只读字段、自定义表单等: ```python # admin.py class ProductImageInline(admin.StackedInline): model = ProductImage extra = 0 readonly_fields = ('created_at',) # 设置只读字段 # 其他自定义设置... ``` 通过上述步骤,就能在 Django Admin使用 `admin.StackedInline` 实现内联编辑功能。在 `Product` 模型的管理页面,能直接添加、编辑和删除关联的 `ProductImage` 实例。 ### 结合排序工具 若要为内联视图添加排序功能,可使用 `django-admin-sortable2` 这样的扩展包。假设使用 `SortableInline` 类,可参考如下示例: ```python # admin.py from django_admin_bootstrapped.admin.models import SortableInline from django.contrib import admin from .models import Product, ProductImage class ProductImageInline(admin.StackedInline, SortableInline): model = ProductImage extra = 0 class ProductAdmin(admin.ModelAdmin): inlines = [ProductImageInline] admin.site.register(Product, ProductAdmin) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值