Reputation: 45285
I have two views:
def importContent(request):
d = get_some_data()
t = get_template('import.html')
c = Context({'entries' : d.entries })
return HttpResponse(t.render(c))
def doImport(request):
return HttpResponse("hey")
Here is import.html:
{% for entry in entries %}
{{ entry.title}} <br>
{% endfor %}
<a href="/do_import">soo</a>
User open importContent() view and press the link, which opens th doImport() view. How can I pass d-variable from importContent() view to doImport() view?
Upvotes: 0
Views: 288
Reputation: 7603
If you want to pass all entries back to doImport, it won't be easy. The way to pass parameter in a request is to put them in the url, use a post request or use session but this requires more work.
Using URL is not really convenient because there will be a lot of parameters on that url.
Using a post is sort of weird and not suited to a html link.
Using a session requires authentication and use of users.
Can't you just call:
d = get_some_data()
in doImport again ?
Upvotes: 1
Reputation: 74675
I can think of a couple of ways to approach this.
The first requires that you have sessions
enabled. In this mechanism the first view will store the variable in the user's session and the second will retrieve it. For e.g.
def importContent(request):
d = get_some_data()
t = get_template('import.html')
c = Context({'entries' : d.entries })
request.session['entries'] = d
return HttpResponse(t.render(c))
def doImport(request):
if 'entries' in request.session:
d = request.session['entries']
else:
d = # Perform a look up or show a message etc.
return HttpResponse("hey")
The session can be substituted with a custom cache too.
The second is to make the second explicitly look up the data. This is easier if the data is limited and doesn't require any extensive computation.
Upvotes: 2