在前端启用Django管理功能,内置线条 [英] Enable Django admin functionality at frontend with inlines

查看:106
本文介绍了在前端启用Django管理功能,内置线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已决定从管理网站移动一些功能到前端。功能包括管理一个模型与一些外键内联。



为此,我已经安装了django-dynamic-formset JQuery插件(链接

解决方案

此任务需要对两者的支持前端和后端。



似乎最简单的方法是在前端使用django-admin表单本身,如果这是可以接受的。



任何django管理表单都可以识别URL中的?_ popup = 1 参数,以抑制管理页面的装饰,并只提供一个表单。将包括所有必需的JavaScript和CSS样式。因此,管理表单可以在 iframe 中的前端显示。仍然需要在JavaScript级别上进行小型黑客攻击以将 iframe 的大小调整到表单,然后通知页面,然后表单关闭。


I have decided to move some functionality from my admin website into the frontend. Functionality includes the administration of one model with some foreign key inlines.

For that I have installed django-dynamic-formset JQuery plugin (link git) and struggling with it already for couple of days. Here is one of the problems.

The same functionality is already implemented in Django admin. I can add, modify, remove inlines and modify model instance as I want. I am wondering why should I use this JQuery plugin and why there are not so many good tutorials on the topic in the Internet?

I need a good and recent example of how to use django formsets or inline formsets at the frontend side without third-party JS files. I whould be glad if it has links (not checkboxes) to remove inline items and add button which will correctly add new inline.

To be more specific because the question was considered to be too broad:

I have two models School and SchoolPlace:

class School(models.Model):
     name = models.CharField(_('School name'), max_length=100)

class SchoolPlace(models.Model):

    school = models.ForeignKey(School, verbose_name=_('school place'), related_name='school_places', blank=True, null=True)

    name = models.CharField(_('School place name'), max_length=200)

    city = models.ForeignKey(City, blank=True, null=True, verbose_name=_('city'),
                     help_text='city')

There are also corresponding forms:

class SchoolForm(forms.ModelForm):

      name = forms.CharField(
                             label=_('Name'),
                             widget=forms.TextInput(attrs={
                                'placeholder': _('school name')}),
                              max_length=100, required=True)

class SchoolPlaceForm(forms.ModelForm):

    name = forms.CharField(label=_('Name'),
                            widget=forms.TextInput(
                                        attrs={'placeholder': _('school place name')}),
                                        max_length=200,
                                        required=False)

    city = forms.ModelChoiceField(label=_('City'),
                                  widget=forms.Select(attrs={'class': 'ui search dropdown'}),
                                  queryset=City.objects.all(), required=False)

    class Meta:
        model = SchoolPlace
        fields = ['name','city']
        exclude = ['school']

I would like to edit these two models in the same way as it is done in Django admin, but only at my own frontend. As far as all the js files are already in django.contrib.admin I would like to do it without usage of side applications and plugins.

I need the same functionality as in Django admin: add, delete, modify SchoolPlace inlines. Here is the screenshot:

解决方案

This task requires the support on a both frontend and backend sides.

It seems the simplest way is the using of django-admin forms itself on frontend if this is acceptable for you.

Any django admin form recognizes the ?_popup=1 parameter in the URL to suppresses the admin page decorations and serve just a form. All necessary JavaScript and CSS styles will be included. So admin forms could be shown on frontend in the iframe. A small hacking around on JavaScript-level is still required to adjust a size of iframe to the form and notify the page then form is closed.

这篇关于在前端启用Django管理功能,内置线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆