myrunner -- ModelAdmin 和View的思考

前端是jquery:

    本例子定义了jquery.init.js: 

 

/*global django:true, jQuery:false*/
/* Puts the included jQuery into our own namespace using noConflict and passing
 * it 'true'. This ensures that the included jQuery doesn't pollute the global
 * namespace (i.e. this preserves pre-existing values for both window.$ and
 * window.jQuery).
 */
var django = django || {};
django.jQuery = jQuery.noConflict(true);

 

某模块如何调用: 

 HostIPExecJob.js

(function ($) {
    $(document).ready(function ($) {
        $(".save-box").hide()
    });
})(django.jQuery);

后台admin如何调用:

 

@admin.register(ToolsExecJob)
class HostIPExecJobAdmin(admin.ModelAdmin):
    list_display = ['tools', 'param', 'create_time', 'update_time']
    search_fields = ['tools']
    list_filter = ['tools']
    readonly_fields = ['tools', 'hosts', 'param']
    inlines = [ToolsExecDetailHistoryInline]

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    class Media:
        js = ('/static/js/HostIPExecJob.js',)

摘要: 可定义css和js

    class Media:
        css = {
            "all": ("my_styles.css",)
        }
        js = ("my_code.js",)

 

使用以上方法:  

优点:对于“一次性”项目简单。
缺点:只对Change Form有效。

 

详情请看:  这里 

如何进行架构优化: 

使用Custom View URL 、 Custom View  和  Custom View Template

如:  

****admin.py:

class PostAdmin(admin.ModelAdmin):
    def my_view(self, request):
        return admin_my_view(request, self)
    def get_urls(self):
        urls = super(PostAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^my_view/$', self.my_view)
        )
        return my_urls + urls

 

 

view.py: 

@permission_required('blog.add_post')
def admin_my_view(request, model_admin):
    opts = model_admin.model._meta
    admin_site = model_admin.admin_site
    has_perm = request.user.has_perm(opts.app_label /
                  + '.' + opts.get_change_permission())
    context = {'admin_site': admin_site.name,
        'title': "My Custom View",
        'opts': opts,
        'root_path': '/%s' % admin_site.root_path,
        'app_label': opts.app_label,
        'has_change_permission': has_perm}
    template = 'admin/demo_app/admin_my_view.html'
    return render_to_response(template, context,
              context_instance=RequestContext(request))

  view_template.py:

{% extends "admin/base_site.html" %}
{% load i18n %}
{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="../../../">{% trans "Home" %}</a> &rsaquo;
    <a href="../../">{{ app_label|capfirst|
escape }}</a> &rsaquo;
    {% if has_change_permission %}<a
href="../">{{ opts.verbose_name_plural|
capfirst }}</a>{% else %}{{ opts.verbose_name_plural|
capfirst }}{% endif %} &rsaquo; My Custom View
</div>
{% endblock %}
{% block content %}
    <!-- do stuff here -->
{% endblock %}

 

posted @ 2017-03-29 20:53  八月的男人  阅读(...)  评论(...编辑  收藏