Question

I am using django 1.5

I want pass the request object to my inclusion tag, however get a error, Key Error.

My view is:

class IglesiaCreateView(CreateView):
     model = Iglesia
     template_name = 'iglesia/iglesia_form.html'
     success_url = reverse_lazy('iglesia_list')
     form_class = IglesiaForm

    def get_form_kwargs(self):
        kwargs = super(IglesiaCreateView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

My form is:

class IglesiaForm(forms.ModelForm):
    class Meta:
        model = Iglesia
        fields = ('nombre', 'principal', 'parroquia')

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(IglesiaForm, self).__init__(*args, **kwargs)
        parroquia = self.request.session.get('parroquia')
        self.fields['parroquia'].queryset = Parroquia.objects.filter(pk=parroquia.pk)
        self.fields['parroquia'].empty_label = None

My inclusion tag is:

from myapp.forms import IglesiaForm
@register.inclusion_tag('includes/iglesia_ajax_form.html', takes_context=True)
    def iglesia_ajax(context):
    request = context['request']
    form_iglesia = IglesiaForm()
    ctx = {'form_iglesia': form_iglesia}
    return ctx

I have a error:

Django Version: 1.5.1
Exception Type: KeyError
Exception Value: 'request'

EDIT:

Problem solved

I add in TEMPLATE_CONTEXT_PROCESSORS the follow: 'django.core.context_processors.request'.

And change:

form_iglesia = IglesiaForm()

to:

form_iglesia = IglesiaForm(request=request)

Thanks for your help

Was it helpful?

Solution

change this line

form_iglesia = IglesiaForm()

to:

form_iglesia = IglesiaForm(request=request)

See if that works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top