我可以使用 django admin 作为应用程序吗?

发布于 2024-10-15 15:14:18 字数 320 浏览 7 评论 0原文

众所周知,django 为您的项目提供了一个非常基本但灵活的管理面板。我在这里看到了有关(缺点)优点的讨论。编写自己的控制接口并使用默认的控制接口。为了重述,我会提到,imodjango.contrib.admin 用于快速而简单的项目,不需要任何复杂的内容管理;因此,为了对站点进行绝对控制和完全定制,人们可能应该实现一个适当的管理界面。

我想尝试为我正在开发的项目推出自己的管理员,但真的不想重新投入,所以也许我可以使用 django 的管理员作为项目中的插入应用程序,按照我想要的方式自定义它?您对如何做到这一点有什么想法吗?也许已经有一些类似的应用程序了?

As all of you know django provides a very basic though flexible admin panel for your project. I've seen here discussions refering the (dis)advantages. of writing your own control interface and using the default one. Just for recapitulation I'll mention that, imo, django.contrib.admin is for quick and simple projects that does not require any complex content managment; so for the absolute control and full customization of a site one probably should implement an adequate administration interface.

I want to try roll my own admin for the project I'm working on, but really don't want to reinvet the wheel, so maybe I could use the django's admin as a plugged app in my project customizing it the way I want to? Do you have any ideas of how can it be done? Maybe there already are some simmilar apps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-10-22 15:14:18

我对 django.contrib.admin 没有任何建议,因为我不使用它。我的客户更喜欢对他们的网站进行几乎类似 CMS 的内联更新(两个“版本”让他们感到困惑),而我更喜欢额外的控制。

那么,在这一点上,你曾经使用过 django.forms 吗? Django 表单将为您提供一种根据您的需求接受和验证输入的简单方法。您可以使用自己的自定义字段扩展表单,并自定义其验证。例如,您可以向表单添加一个字段,如果输入的字符串包含您想要省略的字符,则该字段会引发 ValidationError 。 Django 有很多有用的输入字段,包括 EmailField

下一个真正的大技巧是 django.forms.ModelForm 。这提供了一种非常简单的方法来快捷地编写自定义表单,然后将其插入到正确的对象中,然后将其保存到数据库中。它为你做这件事。所有这一切。从字面上看,您可以这样做:

fItem = ItemForm(request.POST) 
# or
# I = Item.objects.get(id=?)
# fItem = ItemForm(request.POST, instance=I) 

if f.is_valid():
    fItem.save()

从此并使用表单,您可以非常轻松地自己创建 CRUD 操作。

接下来要实现的是用户、组和权限。没有什么可以阻止您为此使用提供的身份验证内容并提供您自己的 UI。您也不必使用实际的管理站点;两者并没有绑在一起。

我个人倾向于使用自己的产品,因为在我的一项工作中,客户对身份验证和授权有特定的要求。但我不推荐它 - 只需使用那里的东西即可。特别是因为现在有身份验证后端,例如< a href="https://github.com/uswaretech/Django-Socialauth" rel="nofollow">Django-SocialAuth。

I don't have any advice for django.contrib.admin since I don't use it. My clients prefer an almost CMS-like inline updating of their site (two "versions" confuses them) and I prefer the extra control.

So, on that vein, have you ever used django.forms? Django forms will give you an easy way to accept and validate input as per your needs. You can extend a form with your own custom fields, and customise their validation. So for example you can add a field to a form that raises a ValidationError if the inputted string contains characters you want to omit, for example. Django has a lot of useful input fields, including EmailField.

The next really big trick is the django.forms.ModelForm. This provides a very easy way to shortcut writing a custom form and then inserting that into the right object then saving that into the database. It does it for you. All of it. Literally, you do this:

fItem = ItemForm(request.POST) 
# or
# I = Item.objects.get(id=?)
# fItem = ItemForm(request.POST, instance=I) 

if f.is_valid():
    fItem.save()

From that and using forms, you can create CRUD operations yourself very easily.

The next thing to achieve is users, groups and permissions. There's nothing stopping you using the provided authentication stuff for this and providing your own UI. You don't have to use the actual admin site as well; the two aren't tied together.

I personally rolled my own which I tend to use, because on one of my jobs the client had specific requirements for authentication and authorization. I wouldn't recommend it though - just use what's there. Especially since there are now authentication backends such as Django-SocialAuth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文