Solution 1 :

test is a Template object. You need the get its content as a string

from django.template.loader import render_to_string
test = render_to_string('home/custom_options.html')

Problem :

I am trying to pass through an html file from my Django view via a django template. This is my view:

def sample_function(request, product_id, slug=None):
  from django.template import loader
  product_data = get_object_or_404(Product, id=product_id)
  country_code = request.session.get('country_code')
  form = CountryForm(initial={'country': country_code})
  form_estimate = CountryEstimateForm(initial={'country_estimate': country_code})

  test = loader.get_template('home/custom_options.html')

  return render(request, 'request_custom_order.html', {'product':product_data,'country': form, 'country_estimate': form_estimate,'test':test})

This is my html:


{{ test }}


However, if I just do {{ test }} I get:

<django.template.backends.django.Template object at 0x7fcd03203080>

If I do {{ test|safe }} I just get blank.

Could someone help me understand what I am doing wrong?


Comment posted by M-Chen-3

Why do you want to pass the file? Just type the contents of the template you want to pass into the other one.

Comment posted by iampre409

Hey there! Because I want to dynamically pass through fairly large html forms based on various criteria. I thought it would be easier to manage by keeping each form in its own file. Otherwise I think the view file would get out of control really quick.