title: Sharing data between two views through template in Django?
body: <p>I may have overcomplicated things. </p>
  
  <p>I have two views. The first view generates a bunch of temporary data based on the user's input from the form. Each of the generated data contains a name and misc data. I want to pass only the names to the template to be rendered as a list of hyperlinks. If the user clicks on one of them, the second view should be given the specific name the user clicked on so that the view can manipulate it. The only problem is, I don't know how to get the misc data associated with the name.</p>
  
  <p>The misc data generated could contain random characters that's not a standard character in URLs, so I can't turn misc into a hyperlink like I can with just the name. </p>
  
  <p>I have something like this:<br/>
  <strong>views:</strong></p>
  
  <pre><code># Displays the temp data names
  def display(request):
      return render_to_response('display.html',{},context_instance=RequestContext(request))
  
  # User provides input, generate temp data to be displayed as hyperlinks
  def search(request):
      form = SearchForm(request.POST)
      if form.is_valid():
          usr_input = form.cleaned_data['input']
          data = generate_data(usr_input)   # generates a list of (name, misc) data.
          request.session['hyperlinks'] = get_list_names(data)   # returns only names in data
          return HttpResponseRedirect('views.display')
  
      else:
          ....
  
  # User has clicked on a hyperlink, we must process specific data given its name.
  def process_data(request, name):
      # How to get associated misc data created from search()?
  </code></pre>
  
  <p>I haven't written the template yet, but the idea is:<br/>
  <strong>template:</strong></p>
  
  <pre><code>{% for name_link in request.session.hyperlinks %}
      &lt;a href={% url process name_link %}&gt; 
  {% endfor %}
  </code></pre>
  
  <p>One solution could be creating a bunch of session variables:</p>
  
  <pre><code>for name in get_list_names(data):
      request.session[name] = // associated misc data
  </code></pre>
  
  <p>But this seems like a waste. Plus I'd have to manage deleting the session variable later on since this is only temporary data generated based on user input. A new input from the user would create another huge horde of session variables!</p>
  
  <p>Another solution could be to store it temporarily in the database, but that also seems like a bad idea.</p>
  
  <p><br/>
  <br/>
  <strong>EDIT - Trying out suggestion by christophe31:</strong>
  I'm not quite sure if I understand your suggestion, but is it something like this?</p>
  
  <pre><code>data_dict = {name1:misc1, name2:misc2, etc...}
  encoded = urllib.urlencode(data_dict)   # encoded = 'name1=misc1&amp;name2:misc2...etc'
  request.session['hyperlinks'] = encoded
  </code></pre>
  
  <p>A few questions on this though:<br/>
  1) Wouldn't encoding it using urllib defeat the purpose of having a dictionary? It returns a string rather than a dictionary</p>
  
  <p>2) To expand on (1), what if the misc data had '&amp;' and '=' in it? It would screw up parsing which is the key and value by the second view. Also, misc data may have unusual characters, so allowing that to be part of the url to be displayed may be bad.</p>
  
  <p>3) Does Django protect from allowing the user to maliciously modify the session misc data so that the misc data generated from the first view may be different than the one passed to the second view? That would be a problem! </p>