简体   繁体   English

仅显示Django管理表单

[英]Display django admin form only

I want to let the user (they're signed in) edit items using the admin form without them having access to the whole admin interface. 我想让用户(他们已登录)使用管理表单来编辑项目,而无需他们访问整个管理界面。

This would be easier and follow the DRY principle betters since the forms only need to be defined once in the admin setup. 这将更容易并且更好地遵循DRY原理,因为仅需在管理员设置中定义一次表单即可。 It should also avoids having to manually check if the user has permissions to edit that model since that will be taken care of by the admin system. 它还应避免必须手动检查用户是否具有编辑该模型的权限,因为这将由管理系统处理。

If i set the users as is_staff i know i can point them to the edit/add page for the particular model/item, but by manually editing the url they can find their way to the full admin interface (at least the bit they have access to). 如果我将用户设置为is_staff,我知道我可以将他们指向特定模型/项目的编辑/添加页面,但是通过手动编辑url,他们可以找到进入完整管理界面的方式(至少可以访问该位)至)。 Also the admin interface doesn't have the correct theme unless I change the whole admin theme. 除非我更改整个管理主题,否则管理界面也没有正确的主题。 (I think?) (我认为?)

Perhaps I'm missing something obvious and there's a super simple way to test if the user is able to add to/edit the model and generate a whole form (including formsets) for a model based on the relevent class in app/admin.py, but I haven't found it yet. 也许我缺少明显的东西,并且有一种超级简单的方法来测试用户是否能够基于app / admin.py中的relevent类添加/编辑模型并为模型生成整个表单(包括表单集)。 ,但尚未找到。

I look forward to being told I'm an idiot and there's an obvious and simple solution. 我期待着被告知我是个白痴,并且有一个显而易见的简单解决方案。

(edit: i've looked at django-frontendadmin, but it looks out of date and the urls situation looked a hack.) (编辑:我看过django-frontendadmin,但是它看起来已经过时,URL的情况看起来很糟。)

The admin is intended for trusted users only. 管理员仅适用于受信任的用户。 So opening the admin up to all users of your site is a bad idea. 因此,向网站的所有用户开放管理员是一个坏主意。

You need a form from your model... These are known as modelforms . 您需要模型中的表单...这些被称为modelforms

>>> from django.forms import ModelForm
>>> from myapp.models import Article

# Create the form class.
>>> class ArticleForm(ModelForm):
...     class Meta:
...         model = Article
...
>>> form = ArticleForm()

How to work with forms is in the forms documentation . 表单文档中提供了如何使用表单的信息

UPDATE: 更新:

The automatic admin interface reuses django.forms. 自动管理界面重复使用django.forms。 So ModelAdmin.form and a ModelForm are both forms. 所以ModelAdmin.formModelForm都是形式。 They are capable of the same thing. 他们有能力做同样的事情。 So if you want to 'Display django admin form only ... Easy and out of the box' . 因此,如果您想“仅显示django管理表单...简单易用” Than a modelform is the answer. 答案比modelform Proof: 证明:

from django.db import models
from django.forms import ModelForm
from django.contrib import admin

class Author(models.Model):
   pass

class AuthorForm(ModelForm):
    class Meta:
        model = Author        

class AuthorAdmin(admin.ModelAdmin):
    pass
admin.site.register(Author, AuthorAdmin)

print AuthorForm.__class__ == AuthorAdmin.form.__class__ # True

The only differences I can think of are: Some fields in ModelAdmin.form use admin widgets (widgets are easy to define) and the admin has some images, js and css (media is easy to include). 我能想到的唯一区别是: ModelAdmin.form某些字段使用管理窗口小部件(易于定义的小部件),并且管理员具有一些图像,js和css(易于包含媒体)。

Finaly a tip: Use an app that helps with styling, ordering, widgets etc like django-crispy-forms . 最后的提示:使用可帮助样式,订购,小部件等的应用程序,例如django-crispy-forms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM