向Django app添加一个面向用户的自定义表单(使用selectize和taggit)

时间:2016-02-11 02:07:52

标签: ajax django django-forms selectize.js django-taggit

我对django很新,我试图弄清楚如何使用taggit-selectize小部件(或django-taggit)创建表单。我在网上找到的所有内容都指的是它使用了管理页面,但我希望这些标签面向用户并且可以编辑 - 就像我在这篇文章下面创建的标签一样。到目前为止,我已经确定我需要使用小部件创建表单:

# models.py
from taggit_selectize.managers import TaggableManager
    tags = TaggableManager()

# forms.py
from taggit_selectize.widgets import TagSelectize
from .models import MyModel

class TagForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ('tags',)
        widgets = {'tags': TagSelectize(),}

但我无法弄清楚如何在我的模板中包含此表单,以便它显示在我的MyModel对象旁边。理想情况下,我想我会期望它的行为类似于django-fluent-comments,在那里我可以拨打{% render_comment_form for obj %}并将其称为一天。

更新

我已对views进行了修改(见下文),现在可以访问模板中的表单,但我似乎无法提交我的代码(理想情况下,这不会触发重定向,或者)。

# views.py
from .forms import TagForm
def show_tags(request):
    return render(request, 'tags.html', {'tagform' : TagForm})

# tags.html        
<div>
{{ tagform.media }}
{{ tagform.as_p }}
</div>

1 个答案:

答案 0 :(得分:1)

所以,我终于弄明白了。它涉及将标记包装在<form>标记中并捕获POST请求。为了记录,这是一个项目的一部分,涉及使用Haystack返回我想要标记的结果列表。我的views.pySearchView的子类,而不像我在这里定义一个函数(show_tags()),而不是每页有一个对象我有多个。

对于页面上的对象obj,您有以下

# views.py
from .forms import TagForm
from .models import MyModel
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt

def show_tags(request):

    # Perhaps the request specifies the object, but
    # for simplicity's sake we just pick a specific model instance
    object = MyModel.objects.filter(pk = 123) 
    return render(request, 'tags.html', {'tagform' : TagForm,
                                         'obj' : MyModel})

@require_POST
@csrf_exempt
def create_tag(request):
    # See javascript below for creation of POST request
    data = request.POST
    tag_text_raw = data.get('tag_data')

    # clean_tag() not shown, but it splits the serialized
    # tag_text_raw and returns a list of strings
    tag_text_clean = clean_tag(tag_text_raw)

    obj_pk = data.get('obj_pk')

    #save tags to document
    doc = DocInfo.objects.get(pk = obj_pk)
    doc.tags.add(*tag_text_clean)

    # not strictly necessary; mainly for logging
    response_data = {'tag_text': tag_text_clean,
                 'obj_pk': obj_pk
                 }

    return JsonResponse(response_data)

因此show_tags将信息发送到带有render的模板,然后模板可以访问这些对象。这是我最初没有意义的。

# tags.html (extends base.html)
{% block scripts %}{{ block.super }}
    <script type="text/javascript" src="{{ STATIC_URL }}js/ajaxtag.js"></script>
  {{ tagform.media }}
{% endblock %}    

{{ obj.text }}
<form method="post" action="create_tag/" id="tag-form-{{ obj.pk }}" name="tag-form-obj" data-object-id={{ obj.pk }}>
    {{ tagform.as_p }}
    <input type="submit" name ="tag-form-input" value="Add Tags" />
</form>

我们可以使用javascript来捕获POST请求:

    #ajaxtag.js
    (function($)
    {
    // A stripped-down version of ajaxcomments.js from fluent_comments
    // See that file for further expansions
    $.fn.ready(function () {
        var tagform = $('form[name="tag-form-obj"]');

        if (tagform.length > 0) {
            // Detect last active input.
            // Submit if return is hit
            tagform.find(':input').focus(setActiveInput).mousedown(setActiveInput);
            tagform.submit(onTagFormSubmit);
        }
    });

    function onTagFormSubmit(event)
    {   
        event.preventDefault();  // prevents redirect
        var form = event.target;

        create_tag(form);
        return false;
    }

    function create_tag(form) 
    {
        console.log("create_tag is working!") // sanity check
        var $form = $(form);
        var tag_text = $form.serialize();
        var url = $form.attr('action');
        var obj_id = $form.attr('data-object-id')

        $.ajax({
            url : url,
            type: "POST",
            data: { tag_data: tag_text, obj_pk: obj_id},

            success: function (data) {
                data;
                console.log(data);
                console.log('success');
            },

            error: function (xhr, errmsg, err) {
                // Return error to console
                console.log(xhr.status + ": " + xhr.responseText)
            }
        });

    }

    function setActiveInput() {
        active_input = this.name;
    }

    })(window.jQuery);

最后,urls.py将请求发回create_tag()

# urls.py
from .views import create_tag
...
url(r'^create_tag', create_tag, name = 'tag-form')
...