0

Wagtail Form Builder で購読フォームを作成しました。フォームがそのテンプレート subscribe_form.html から送信されると、送信は正常に行われます。

<form action="{% pageurl page %}" method="POST">
     {% csrf_token %}
    <div class="shop-subscribe bg-color-green margin-bottom-40">
        <div class="container">
                <div class="row">
                    <div class="col-md-8 md-margin-bottom-20">
                            <h2>Subscribase para mantenerse<strong> informado</strong></h2>
                    </div>




                    <div class="col-md-4">
                            <div class="input-group">


                             <input type="text" class="form-control" placeholder="Correo Electronico..." {{ form.subscribase }}>
                                    <span class="input-group-btn">
                                            <button class="btn" type="submit"><i class="fa fa-envelope-o"></i></button>
                                    </span>

                            </div>

                                {{ form.subscribase.errors }}
                        </div>
                </div>
        </div><!--/end container-->
    </div>

 </form>

ただし、include タグを使用して他のページに含めると、送信されず、エラー メッセージも表示されません。

{% include "home/subscribe_form.html" %}

include タグを使用したときにフォームが送信されない原因について誰かアドバイスをいただけますか?

4

1 に答える 1

1

他の人が言ったことを詳しく説明すると、これにはカスタム テンプレート タグを使用するのが簡単な方法です。包含タグを使用すると、テンプレート タグを呼び出してそれらに args/kwargs を渡し、ロジックを実行してから、そのロジックを使用してテンプレートをレンダリングし、レンダリングされたページに含めることができます。

プロジェクトの app ディレクトリにフォルダーを作成し、 という名前のディレクトリをtemplatetags作成し、その名前の中に空のファイルを作成し__init__.py(Django はこれを使用して、起動時にファイルを実行する必要があることを認識します)、新しいディレクトリ名で別のファイルを作成しますmy_custom_tags.py(または使いたいものは何でも)。その中で――

from django.template import Library

register = Library()

@register.inclusion_tag("home/subscribe_form.html")
def subscription_form(form):
    return {'form':form}

次に、メイン テンプレートで次のようにします。

{% load my_custom_tags %}
{# (or whatever you named your file for custom tags #}

{# where you want to include your tag, pass the form from the main template; be sure to pass your form variable from your context data #}
{% subscription_form form %}

フォームをレンダリングしました。コンテキストからフォームを渡すため、テンプレート タグの外側で実行されるロジックはそのまま残ります。これは、複数の場所の要素に使用する汎用テンプレートがあるが、ビューの外部でロジックを実行する必要がある場合 (または、Wagtail の場合、モデルに埋め込まれたページ/スニペット ロジック) に特に役立ちます。

于 2016-05-24T16:36:27.633 に答える