List Info

Thread: Using reverse() in your urlpatterns




Using reverse() in your urlpatterns
country flaguser name
United States
2007-06-26 13:12:43
I've just been screwing around with my urlpatterns, trying
to clean
them up. Mostly, I've been trying to remove all hard coded
urls
wherever they are, in templates and whatnot, by adding a
name="foo" to
my urlpatterns and then accessing them with {% url %} and
reverse().

I'm wondering though, is it possible to use reverse() and
pass it some
args, to use it as the post_save_redirect in generic views?
I've been
trying to get it to work, but no luck so far... maybe I'm
not passing
the args properly? Or maybe it's not even possible?

from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_detail
from django.views.generic.create_update import
create_object
from django.core.urlresolvers import reverse
from apps.myapp.models import MyThing
urlpatterns = patterns('',
	(r'^view/(?P<object_id>d+)/$', object_detail,
		{'queryset' : MyThing.objects.all() },
		'mything-view'),
	(r'^create/$', create_object,
		{'model' : MyThing,
		'post_save_redirect' : reverse('mything-view', args=(id,))
},
		'mything-create'),
)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Using reverse() in your urlpatterns
country flaguser name
United States
2007-06-26 14:58:01
On Jun 26, 12:12 pm, Justin <justin.m...gmail.com> wrote:
> I'm wondering though, is it possible to use reverse()
and pass it some
> args, to use it as the post_save_redirect in generic
views?

On second thought, this is kind of a stupid thing to do in
the first
place... why would I want to avoid hard coding urls in the
very same
file where they are defined? That's a silly thing to do. I
think I was
getting a little too anal there. 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Using reverse() in your urlpatterns
country flaguser name
United States
2007-06-26 16:51:15
On Tue, 2007-06-26 at 19:58 +0000, Justin wrote:
> On Jun 26, 12:12 pm, Justin <justin.m...gmail.com> wrote:
> > I'm wondering though, is it possible to use
reverse() and pass it some
> > args, to use it as the post_save_redirect in
generic views?
> 
> On second thought, this is kind of a stupid thing to do
in the first
> place... why would I want to avoid hard coding urls in
the very same
> file where they are defined? That's a silly thing to
do. I think I was
> getting a little too anal there. 

It's actually not that stupid. If you have urls hard-coded
in the
template, you have to make sure that you change them if you
change the
url patterns of your app. If you use the {% url %} tag, you
can change
the url patterns willy-nilly and only have to make changes
if you rename
the patterns or the view functions.

Following this pattern would seem to make it easier to make
apps
portable.

Todd


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Using reverse() in your urlpatterns
country flaguser name
Australia
2007-06-26 22:48:10
On Tue, 2007-06-26 at 18:12 +0000, Justin wrote:
> I've just been screwing around with my urlpatterns,
trying to clean
> them up. Mostly, I've been trying to remove all hard
coded urls
> wherever they are, in templates and whatnot, by adding
a name="foo" to
> my urlpatterns and then accessing them with {% url %}
and reverse().
> 
> I'm wondering though, is it possible to use reverse()
and pass it some
> args, to use it as the post_save_redirect in generic
views? I've been
> trying to get it to work, but no luck so far... maybe
I'm not passing
> the args properly? Or maybe it's not even possible?

I don't really understand the question you are asking.
However, here's
the skinny: you give reverse any parameters -- named or
positional --
that can appear in the URL and it returns a string. The only
arguments
you can give are ones that appear in the URL pattern (the
reg-exp).

If that's all you are trying to do, then perhaps give an
example of how
you're trying to use reverse() and we can help you. If
you're somehow
trying to influence the extra_dict (third) parameter in the
url() line,
then you are trying to do something that reverse() is not
designed for,
and which doesn't really make sense for URL creation.

Hope that might clear up some of your confusion, although,
as I said,
I'm not quite sure what you were asking.

Regards,
Malcolm

-- 
The cost of feathers has risen; even down is up! 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )