multiple views function in a single URL specification

1,909 views
Skip to first unread message
Message has been deleted
Message has been deleted

John Yeukhon Wong

unread,
Oct 17, 2010, 1:16:01 PM10/17/10
to Django users
I asked this somewhere else but it seems like the responder hasn't
reply the latest comment I made.
http://stackoverflow.com/questions/3951758/how-do-you-iterate-over-a-...

Nevertheless, I think I should be welcome to make one here.

Let's keep thing short.

Say I have a very simple list to iterate

[--code--]
def link(request):
c = Context()
c['title'] = ['Home Page', 'Current Time', '10 hours later']
return render_to_response('time.html', c)
[--code--]

Now say I also have another view called current_time

In my html I had, for example
[--code--]
{% for item in title %}
{{item}}
{% if not forloop.last %} | {% endif %}
{% endfor %}

{% if hour <= 1 %}
do something...
{% endif %}

[--code--]

For the template, I tried loop through "in c.title, in c, in title"
and still doesn't work

As you can guess, I use two view functions in a single html file.

The problem is that I received 'function' object is not iterable

So the guy said I probably had a problem with the URL
my URL --> (r'^now/$', current_time, link),

So he recommended me doing this
(r'^articles/(?P<current_time>\(?P<link>)/$',
'project_name.views.link'), #the second tuple element is the view
function

Something similar to Django URL Dispatcher (from the dispatcher
chapter).. I think...
I think he meant to capture then. But what I want to do is really
just, say, localhost/now/ and load the page

I can definitely integrate two views functions into one single
function, which works fine.

The question is, how can I assign multiple views function in a single
URL???

Thank you for any input in advance!!!

Daniel Roseman

unread,
Oct 17, 2010, 2:34:05 PM10/17/10
to Django users

On Oct 17, 6:16 pm, John Yeukhon Wong <gokoproj...@gmail.com> wrote:
> I asked this somewhere else but it seems like the responder hasn't
> reply the latest comment I made.http://stackoverflow.com/questions/3951758/how-do-you-iterate-over-a-...
You can't do that, and there isn't any reason to want to do so. A view
function takes a request, and returns a response, which is usually in
the form of some rendered HTML. How would it work with two? What would
be the input to the second function? How would the outputs be merged?

As usual, the real question is what are you trying to do that you
think would be best solved by having two views for a single URL.
--
DR.

garagefan

unread,
Oct 19, 2010, 9:46:01 AM10/19/10
to Django users
seems like you're over thinking... you want a single url to handle
multiple pieces of information?

you can assign a single view function to a url. but you can define
multiple functions within that view and merely pass them through to
the template as part of the response.

Or take a look at template tags, as that appears to be more suitable

On Oct 17, 1:16 pm, John Yeukhon Wong <gokoproj...@gmail.com> wrote:
> I asked this somewhere else but it seems like the responder hasn't
> reply the latest comment I made.http://stackoverflow.com/questions/3951758/how-do-you-iterate-over-a-...
Reply all
Reply to author
Forward
0 new messages