为Jango管理颜色框弹出菜单
apidev-coop_colorbox的Python项目详细描述
coop colorbox,使jquery.colorbox在django应用程序中易于使用
安装
管道:
pip install apidev_coop-colorbox
设置
在settings.py中:
INSTALLED_APPS = ( '...', 'colorbox', )
视图
视图:
from django.utils.decorators import method_decorator from django.http import HttpResponseRedirect, Http404 from colorbox.decorators import popup_redirect class MyView(FormView): """edit the profile of the current user""" template_name = "form_popup_template.html" @method_decorator(popup_redirect) def dispatch(self, request, *args, **kwargs): """Manage close of the colorbox popup""" self.user = request.user return super(EditProfileView, self).dispatch(request, *args, **kwargs) def get_form_class(self): """returns the form class to use""" return MyForm def form_valid(self, form): form.save() return HttpResponseRedirect(reverse(’next_step'))
表单弹出模板
模板:
{% extends "colorbox/popup_form_base.html" %} {% load i18n %} {% block title %}{% trans "Edit" %}{% endblock %} {% block form_url %}{% url 'my_view' %}{% endblock %}
您还可以重写或扩展{%block form{u intro%}{%block form{u fields%}`{%block popup_buttons%} 或{%block extra_head%}
主模板
模板:
{% load static i18n %} <script type="text/javascript" charset="utf-8" src="{% static 'js/jquery.colorbox-min.js' %}"></script> <script type="text/javascript" charset="utf-8" src="{% static 'js/jquery.form.js' %}"></script> <script type="text/javascript" src="{% static 'js/colorbox.coop.js' %}"></script> <link rel="stylesheet" href="{% static 'css/colorbox.css' %}" type="text/css" /> <script> $(function () { // activate popups $("a.colorbox-form").colorboxify(); }); </script> <a class="colorbox-form" href="{% url 'my_view' %}">{% trans "Edit" %}</a>
在测试中
测试:
from colorbox.utils import assert_popup_redirects assert_popup_redirects(response, reverse('my_view'))
许可证
coop colorbox使用bsd许可证请参见license.txt