0

I have a Django site and want it so that when the user presses a button on the site that a python file makes a word document and shows it to the user in the browser. I am getting an error saying that context must be a dict rather than set. my document seems to be made in my pycharm project folder but it doesn't want to show itself for some reason.

my process is as follows: user clicks a html button. urls.py then points the browser to a function in my view.py called making_the_doc. This making_the_doc function runs a function in my the plot.py file which will generate the document and returns it to view.py for presentation to the user.

Firstly i created the code that will generate the document. This file is known as theplot.py.

THEPLOT.PY

def making_a_doc_function(request):

doc = docx.Document()
doc.add_heading('hello')
doc.save('this is doc.docx')
generated_doc = open('this is doc.docx', 'rb')
response = FileResponse(generated_doc)

return render(request, 'doc.html', {response})

Then I linked this function to my views.py

VIEWS.PY

def making_the_doc(request):

return making_a_doc_function(request)

Then i created my url paths to point to the views.py

URLS.PY

path('making_a_doc', views.making_the_doc, name='doc'),

finally I generate my html button so the whole process can start off when the button is clicked:

INDEX.HTML

<input type="button" value="generating" onclick="window.open('making_a_doc')">

enter image description here

kitchen800
  • 197
  • 1
  • 12
  • 36
  • Does this answer your question? [Django 1.11 TypeError context must be a dict rather than Context](https://stackoverflow.com/questions/43787700/django-1-11-typeerror-context-must-be-a-dict-rather-than-context) – shimo Jun 27 '20 at 22:44

1 Answers1

1

You are not passing a dict to your render function. A change like this should make it work

def making_a_doc_function(request):

  doc = docx.Document()
  doc.add_heading('hello')
  doc.save('this is doc.docx')
  generated_doc = open('this is doc.docx', 'rb')
  response = {"generated_doc": FileResponse(generated_doc)}

  return render(request, 'doc.html', response)
hendrikschneider
  • 1,696
  • 1
  • 14
  • 26
  • this gives me an error saying `unhashable type: 'dict'` – kitchen800 Jun 28 '20 at 08:44
  • I edited the answer. The {} in the last line have been removed – hendrikschneider Jun 28 '20 at 11:45
  • Great thank you it opens up a window with no errors so im presuming that this is correct now. however nothing is shown in the window. Is this because it is opening i have nothing in my `doc.html` file. i presumed it would open the file itself? – kitchen800 Jun 28 '20 at 14:00