Skip to content

HTTP Request

The most common use of django-components is to render HTML for a given request. As such, there are a few features that are dependent on the request object.

Passing and accessing HttpRequestยค

In regular Django templates, the request object is available only within the RequestContext.

In Components, you can either use RequestContext, or pass the request object explicitly to Component.render() and Component.render_to_response().

When a component is nested in another, the child component uses parent's request object.

You can access the request object under Component.request:

class MyComponent(Component):
    def get_context_data(self):
        return {
            'user_id': self.request.GET['user_id'],
        }

# โœ… With request
MyComponent.render(request=request)
MyComponent.render(context=RequestContext(request, {}))

# โŒ Without request
MyComponent.render()
MyComponent.render(context=Context({}))

Context Processorsยค

Components support Django's context processors.

In regular Django templates, the context processors are applied only when the template is rendered with RequestContext.

Components allow you to pass the request object explicitly. Thus, the context processors are applied to components either when:

  • The component is rendered with RequestContext (Regular Django behavior)
  • The component is rendered with a regular Context (or none), but you set the request kwarg of Component.render().
  • The component is nested in another component that matches one of the two conditions above.
# โŒ No context processors
rendered = MyComponent.render()
rendered = MyComponent.render(Context({}))

# โœ… With context processors
rendered = MyComponent.render(request=request)
rendered = MyComponent.render(Context({}), request=request)
rendered = MyComponent.render(RequestContext(request, {}))

When a component is rendered within a template with {% component %} tag, context processors are available depending on whether the template is rendered with RequestContext or not.

template = Template("""
<div>
  {% component "MyComponent" / %}
</div>
""")

# โŒ No context processors
rendered = template.render(Context({}))

# โœ… With context processors
rendered = template.render(RequestContext(request, {}))

Accessing context processors dataยค

The data from context processors is automatically available within the component's template.

class MyComponent(Component):
    template = """
        <div>
            {{ csrf_token }}
        </div>
    """

MyComponent.render(request=request)

You can also access the context processors data from within get_context_data() and other methods under Component.context_processors_data.

class MyComponent(Component):
    def get_context_data(self):
        csrf_token = self.context_processors_data['csrf_token']
        return {
            'csrf_token': csrf_token,
        }