0

I'm having some trouble understanding the behaviour of inclusion tags.

I have the following relevant files

base.html (the base template)

<!DOCTYPE html>
<html lang="en">

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'tags/style.css' %}">

{% load my_tags %}

<head>
    <div class="back">
        <h1> Flux </h1>
        <hr>
    </div>
</head>

{% block sidebar %}
{% endblock %}

{% block eventlist %}   
{% endblock %}

{% sidebar %}

</body>
</html>

start.html (which at the moment does nothing)

{% extends "base.html" %}

views.py

def start(request):
    return render_to_response("tags/start.html",{},
                              context_instance=RequestContext(request))

my_tags.py (my inclusion tag, SeeTagForm is just a textfield form)

@register.inclusion_tag("tags/sidebar.html",takes_context=True)
def sidebar(context):
    seetagform = SeeTagForm()
    return {"seetagform":seetagform}

sidebar.html

<form action="" method="POST">
    {% for error in seetagform.name.errors %}
    <p> {{ error }} </p>
    {% endfor %}
    {% csrf_token %}
    <button class="btn" type="submit" name="seetag">See</button>
    {{ seetagform.name }}
</form>

Now I have a small list of doubts:

1 - Since I have takes_context=True in my tag, which members does the context argument have?

2 - More specificly, how could I handle from submission from/trough my tag, that is, could I do request = context["request"] inside sidebar(context) so that I could check, for example, if the input was correct?

3 - To do any of the prior, do I have to add anything to the setting.py?

4 - Is it possible (and good practise) to handle the form in the inclusion tag, or should I use yet another view to do that? If so, how?

Thanks in advance,

okm
  • 22,257
  • 4
  • 76
  • 87
user3264316
  • 322
  • 2
  • 15
  • 1
    1. Do a `print dir(context)`. 2. YES 3. NO 4. Nothing stops you from doing so. An alternate though: http://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax – karthikr Mar 05 '14 at 01:15

1 Answers1

0

The context will have all the elements the outer context has. If you have installed the "django.core.context_processors.request" context processor your context will contain the request. This processor is not installed by default in TEMPLATE_CONTEXT_PROCESSORS.

Generally I think its a bad idea to handle forms in an inclusion tag. Its not easy to imagine a situation where this has benefit

On a class based view you would have a seperate post method that handles the form submission. On a view function you could seperate the form handling in a seperate function or process it inline.

It is often desireable to redirect a user back to the same url after a valid form submission to get rid of POST data. This won't work out of an inclusion tag.

In some places like class base views django is using a TemplateResponse instead of HttpResponse as return from a view. A TemplateResponse is a not yet rendered Template. It gets rendered after the middlewares are processed. A middleware can ignore the original reponse by replacing it. That way the original tempalte would have never been rendered, which would mean a form in an inclusion tag would have never been validated.

kanu
  • 691
  • 5
  • 9