set_cookie() missing 1 required positional argument: 'self'
<p>In Django, Im trying to render a template and send a cookie at the same time with this code:</p>

<pre><code>template = loader.get_template('list.html')
context = {'documents': documents, 'form': form}

if ('user') not in request.COOKIES:
    id_user =  ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(30))
    HttpResponse.set_cookie(key='user', value=id_user, max_age=63072000)

return HttpResponse(template.render(context, request))
</code></pre>

<p>But I get the Error:</p>

<blockquote>
  <p>TypeError at /myapp/list/</p>
  
  <p>set_cookie() missing 1 required positional argument: 'self'</p>
</blockquote>

<p>I have checked the <a href="https://docs.djangoproject.com/en/1.10/ref/request-response/" rel="nofollow">documentation</a>, but I dont find the solution.
Help me please :)</p>

<p>Close - HttpResponse is the class, not the instance of the class. The last line is creating one and returning it - so your earlier line needs to act on that instance...</p>

<p>try (untested code):</p>

<pre><code>myResponse = HttpResponse(template.render(context, request))
myResponse.set_cookie(...)
return myResponse
</code></pre>

python django httpcookie