ajaxを実装する前に、javascriptを無効にしてフォームを動作させようとします。 2つのフォームは同じビューを指すことができます。 アクションをルーティングする場合は、<input type="submit">
の代わりに、name
value
という属性を持つ<button type="submit">
タグを使用できます。
2つのフォームテンプレート
<form action="{% url your-url %}" method="get">
<input type="text" name="q" value="{{ q }}">
<button type="submit" name="action" value="search">Search</button>
</form>
{% if entries %}
...
<form action="{% url your-url %}" method="post">
<input type="hidden" name="q" value="{{ q }}">
<button type="submit" name="action" value="save">Save entries</button>
</form>
{% endif %}
そう醜いない1つのフォームテンプレート
<form action="{% url your-url %}" method="post">
<input type="text" name="q" value="{{ q }}">
<button type="submit" name="action" value="search">Search</button>
{% if entries %}
...
<button type="submit" name="action" value="save">Save entries</button>
{% endif %}
</form>
その後、
(テストしていません)このコードのように、あなたのビューに "アクション" をキャッチ
def your_view(request, *args, **kwargs):
action = request.REQUEST.get('action', None)
if request.method == 'POST' and action == 'save':
# do the save stuff
elif action == 'search':
# no need to check if it's a GET
if request.REQUEST.get('q', None):
# do the display stuff
else:
# q required, maybe push a warning message here
else:
# default stuff
return # the response ...
次にいくつかのアヤックスがあります