Re: I can't get urls with explicit keyword arguments to reverse correctly.

2010-02-08 Thread rebus_
On 8 February 2010 17:33, Luke Sneeringer  wrote:
> Good morning, Django e-mail list! Happy Monday! I have a problem. :) I 
> checked the Django documentation and Stack Overflow with no success, so you 
> guys are my next line of defense. This is an issue I've encountered several 
> times; this is just the first time working around it has bothered me enough 
> to send an email.
>
> So I have a couple URLs. The idea here is to use the same view but get 
> slightly different results. In particular, I have a registration page. We 
> have our regular packages, and then special nonprofit pricing. So, my 
> database stuff to power this is all set up.
>
> I have my basic url:
> ('^register/$', 'mysite.myapp.views.register', name = 'register')
>
> I want another one for nonprofits...same thing, and almost identical 
> functionality, so I changed the above and added a line:
> (r'^register/$', 'mysite.myapp.views.register', { 'nonprofit': False }, name 
> = 'register')
> (r'^register/nonprofit/$', 'mysite.myapp.views.register', { 'nonprofit': True 
> }, name = 'register')
>
> In the view, I am passing the value of the "nonprofit" variable to the 
> template under the same name. Simple enough.
>
> Now, the problem: In the template, the reverse URL matching totally barfs. I 
> want...
> {% url register nonprofit=nonprofit %}
> ...to work.
>
> But I get a NoReverseMatch error:
> Reverse for 'register' with arguments '()' and keyword arguments 
> '{'nonprofit': False}' not found.
>
> The Django documentation (as well as an answer on Stack Overflow) suggest 
> that I really ought to be using named URL patterns to solve this 
> problem...so, instead of naming my non-profit registration page "register", I 
> name it "register-nonprofit".
>
> I really do not want to do this if I can avoid it. That would require me to 
> have something to the effect of...
> {% if nonprofit %}
>        {% url register-nonprofit %}
> {% else %}
>        {% url register %}
> {% endif %}
> ...on the relevant pages. That's substantially less clean.
>
> I also am hoping to avoid a /register/forprofit/ type of URL. My boss would 
> kill me. :)
>
> Any thoughts?
>
> Regards,
> Luke
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>


And those suggestions are quite good.

For example:

(r'^register/$', 'mysite.myapp.views.register', { 'nonprofit': False
}, name = 'register_nonprofit')
(r'^register/$', 'mysite.myapp.views.register', { 'nonprofit': True },
name = 'register_profit')

and in your form action field something along the line of:

{% if nonprofit %}
   {% url register_nonprofit %}
{% else %}
   {% url register_profit %}
{% endif %}

With correctly handled template inheritance [1] and include [2] you
should be able to have this if statement block only in one place in
your templates, and that is where the html for your form is located.

[1] http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance
[2] http://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



I can't get urls with explicit keyword arguments to reverse correctly.

2010-02-08 Thread Luke Sneeringer
Good morning, Django e-mail list! Happy Monday! I have a problem. :) I checked 
the Django documentation and Stack Overflow with no success, so you guys are my 
next line of defense. This is an issue I've encountered several times; this is 
just the first time working around it has bothered me enough to send an email.

So I have a couple URLs. The idea here is to use the same view but get slightly 
different results. In particular, I have a registration page. We have our 
regular packages, and then special nonprofit pricing. So, my database stuff to 
power this is all set up.

I have my basic url:
('^register/$', 'mysite.myapp.views.register', name = 'register')

I want another one for nonprofits...same thing, and almost identical 
functionality, so I changed the above and added a line:
(r'^register/$', 'mysite.myapp.views.register', { 'nonprofit': False }, name = 
'register')
(r'^register/nonprofit/$', 'mysite.myapp.views.register', { 'nonprofit': True 
}, name = 'register')

In the view, I am passing the value of the "nonprofit" variable to the template 
under the same name. Simple enough.

Now, the problem: In the template, the reverse URL matching totally barfs. I 
want...
{% url register nonprofit=nonprofit %}
...to work.

But I get a NoReverseMatch error:
Reverse for 'register' with arguments '()' and keyword arguments '{'nonprofit': 
False}' not found.

The Django documentation (as well as an answer on Stack Overflow) suggest that 
I really ought to be using named URL patterns to solve this problem...so, 
instead of naming my non-profit registration page "register", I name it 
"register-nonprofit".

I really do not want to do this if I can avoid it. That would require me to 
have something to the effect of...
{% if nonprofit %}
{% url register-nonprofit %}
{% else %}
{% url register %}
{% endif %}
...on the relevant pages. That's substantially less clean.

I also am hoping to avoid a /register/forprofit/ type of URL. My boss would 
kill me. :)

Any thoughts?

Regards,
Luke

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.