var FilterManager = window.FilterManager || {}; FilterManager.createElement = function(parent){ template = "
Select filter
SAVE NEW FILTER   
"; $(template).appendTo(parent); $("#select").load("/filter/options/ #options option"); } FilterManager.attachEvents = function(){ $("#button").bind("click", function(e){ var currentFilter = window.location.pathname + window.location.search; //var filterName = window.prompt("Filter name pleas...", "New filter name"); var options = {"modal":"true" , overlay: { opacity: 0.5, background: "black" }, buttons: { "Ok": function() { var filterName = $("#filtername").attr("value"); $.post("/filter/add/", { "filter": currentFilter, "name": filterName }, function(data){ $("#out").text(data).fadeOut(7000); $("select").load("/filter/options/ #options option"); dialog.dialog("close"); }); }, "Cancel": function() { $(this).dialog("close"); } } } var dialog = $("
" + "" + "
").dialog(options); $("#filtername").focus(function() { if ( $(this).val() == "New filter name") $(this).val(''); }); }); var u = document.location; $("#select").bind("change", function(e){ var url = $("#select").val(); var port = ""; if(u.port != ""){ port = ":" + u.port; } window.document.location = u.protocol + "//" + u.hostname + port + url; }); } // ********************* usage ****************************** $(document).ready(function() { //do stuff when DOM is ready jQury if(document.location.pathname == "/admin/org/registrationprofile/"){ FilterManager.createElement("#toolbar"); FilterManager.attachEvents(); } }); //******************* in view.py of django app //***************** @login_required def filter(request, action=None, template_name='www/filter.html', extra_context=None): data = {} if extra_context is None: extra_context = {} if action == 'options': data["filters"] = Filter.objects.all() if action == 'add': f = FilterForm(request.POST) f.save() data["response"] = "Ok, filter: " + request.POST.get("filter") + " with name " + request.POST.get("name") + "is added." #data["response"] = "Ok, filter: " + request.POST.get("filter") + " with name " + request.POST.get("name") + "is added." context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, data, context_instance=RequestContext(request)) //*********** in model.py ************************/ class Filter(models.Model): filter = models.CharField(_('Filter'), max_length=250, primary_key=True) name = models.CharField(_('Name'), max_length=250, blank=False) def __unicode__(self): return u" %(name)s %(filter)s " % {'filter':self.filter, 'name':self.name}