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 %}

 

转载于:https://www.cnblogs.com/bayueman/articles/6641709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值