0

How to pass the "render_to_response arguments" to an new view?

def show_list(request, id):
    try:
        profile = request.user.get_profile()
        list = Lista.objects.get(user_profile = profile)
    except Lista.DoesNotExist:
        c = {}
        c.update(csrf(request))
        titlepage = "ooops, you don't have a list YET! click on button to create one!"
        c = {'profile' : profile, 'request' : request, 'titlepage' : titlepage}
        return render_to_response('/profiles/list/create/',c,context_instance=RequestContext(request)) 

with this last line this doesn't work, the url /profiles/list/create/ redirects to a view create_list.

Well, I know that I could write something like redirect(/profiles/list/create/) but with this I cannot pass the dictionary c.

Thanks in advance

cleliodpaula
  • 819
  • 2
  • 11
  • 27

2 Answers2

3

You should use django.contrib.messages to display your message and redirect user to another view.

View code:

from django.contrib import messages
from django.shortcuts import redirect

def show_list(request, id):
    try:
        # normal flow
    except Lista.DoesNotExist:
        messages.warning("ooops, you don't have a list YET! click on button to create one!")
        return redirect("/profiles/list/create/")

Somewhere in create template:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

Also remember to properly enable messages.

Konrad Hałas
  • 4,844
  • 3
  • 18
  • 18
  • While this is a good tip, it doesn't really answer the question yet on how to pass the arguments to the next view. It can be done by adding adding a query part to the target `Location:`. I don't know if there's a shortcut function to create a GET query-part from dict `c`, but `urlparse.urlunsplit` looks promising... – Chris Wesseling Aug 29 '12 at 07:15
  • I do not know why is happening but, I couldn't make the messages appear. – cleliodpaula Aug 30 '12 at 17:19
  • Remember to satisfy additional settings as per https://docs.djangoproject.com/en/1.8/ref/contrib/messages/ – HeyWatchThis Apr 21 '15 at 00:21
0
return render_to_response('my_template.html',
                      my_data_dictionary,
                      context_instance=RequestContext(request))
juankysmith
  • 11,839
  • 5
  • 37
  • 62