Re: URL namespaces

2016-02-25 Thread James Schneider
On Thu, Feb 25, 2016 at 12:45 PM, Malik Rumi  wrote:

>
> Assuming it can be done, what is the proper way to namespace two models in
> the same app, where both use slugs in the url but have different views and
> templates?
>
> In the docs,
> https://docs.djangoproject.com/en/1.9/topics/http/urls/#url-namespaces,
> it talks about how to do this, but it always refers to 'applications'
> rather than 'models'.
>

That's because models are only a portion of the functionality of the
framework, and usually each app has multiple models associated with it.
URL's can and often do point to views that don't reference models at all.
Part of the philosophy of Django is loose-coupling of URL's and models (and
every other function of Django, as much as possible).

Normally the URL layout would match the intended application:

fruit/apple/
fruit/bananna/

Where you want to use something like:

apple/
bananna/

This isn't a technical restriction that you are dealing with, it is a
design issue. What if you wanted to list all of the fruit available?

#App based
fruit/ #list all fruit

#Model based
fruit/

However, now you've hit an inconsistency with your URL's and models, since
you probably don't have a concrete model named Fruit.

You can design your URL's however you like, just some food for thought.


> So can I do:
>
> *urls.py*
>
> url(r'^model1/', include('app.urls', appview4model1,
> namespace='app.model1')
>
> url(r'^model2/', include('app.urls', appview4model2,
> namespace='app.model2')
>
>
Sure, although I'm not sure if those namespace strings are valid. I use
dashes, and in this case i would just use 'model1' and 'model2'.


>
>
> *app.urls.py *
>
> app_name = "app.model1"
>
> urlpatterns [ ]
>
> app_name = "app.model2"
>
> urlpatterns [ ]
>
>
> Frankly, I want the shortest urls possible, so if there was a way to do
> this without putting 'model1' or 'model2' in the url, that would be great,
> but I'm not seeing a way around it - assuming the app_name = 'app.model1'
> thing even works.
>

If that's the case, use something like a letter and the PK of the object:
url(r'a/(?P\d+)/')

Short URL's are a great thing to have, but don't twist yourself into a
pretzel to attain them. Ultimately there are only marginal benefits. It's
means you need to simplify your URL structure, and you can already see what
kind of complication that simplification brings...


>
> Another option I thought of but don't know if possible:
>
> try url(r'^(?P) )
> except url(r'^modelname(?P) )
>
> I've never seen anything like that, though. If it did work, I doubt it
> would fit inside urlpatterns[ ], but maybe I could put it after?
>
> Or I could hack the code that handles url name collisions (once I find it)
>
>
There's no practical limit to what a url() tag can contain. URL's can be
thousands of characters long. If your models have a slug attached to them,
you can just use the slug at the top level of the URL structure. It will
likely incur a lookup for each type of model though, and you would need
some extra checking when creating/updating models to ensure that the slugs
are unique between the two. There are various tricks on how to do that
(usually in the save() method of your models) or a related table that keeps
a list of slugs used for both models with uniqueness enforced (maybe even a
GenericForeignKey). Your URL/view can also accept a slug and perform a
lookup against ModelA, and if it doesn't find anything, look again via
ModelB. This is slightly more challenging with a CBV, but is entirely
possible.

Using the bare slug at the top level also hampers your ability to generate
URL's elsewhere due to the overlap with other keywords (although still
possible if you are careful). Technically, keywords like 'contact' are
considered valid slugs, and may incur a lookup against ModelA/B when in
fact you wanted the user to see the contact page.

This method is exactly how URL shorteners work. The system provides the
slug/hash correlated to the real URL in the database, and the shortener
system redirects based on the matching real URL found for the hash.

Unless there is a business requirement for it, I would recommend for your
own sanity each model would live in its own namespace so that wouldn't have
to worry about overlapping slugs, etc.

You should also have a look at app namepaces vs. instance namespaces:
https://docs.djangoproject.com/en/1.9/topics/http/urls/#url-namespaces-and-included-urlconfs

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVo739FURrsFMWyTc9o_Vd%2BYb%2BFZEjJZiVMJP4qWGawWQ%40mail.g

Re: URL namespaces

2016-02-25 Thread Alex Heyden
Models don't have views. Applications have views. URLs route to views, not
models. Include is basically an import for a urls.py file as a whole, so
there's no reason to import the same file multiple times unless you wanted
it to match multiple URLs.

What are you actually trying to do?

On Thu, Feb 25, 2016 at 2:45 PM, Malik Rumi  wrote:

>
> Assuming it can be done, what is the proper way to namespace two models in
> the same app, where both use slugs in the url but have different views and
> templates?
>
> In the docs,
> https://docs.djangoproject.com/en/1.9/topics/http/urls/#url-namespaces,
> it talks about how to do this, but it always refers to 'applications'
> rather than 'models'.
>
>
> So can I do:
>
> *urls.py*
>
> url(r'^model1/', include('app.urls', appview4model1,
> namespace='app.model1')
>
> url(r'^model2/', include('app.urls', appview4model2,
> namespace='app.model2')
>
>
>
> *app.urls.py *
>
> app_name = "app.model1"
>
> urlpatterns [ ]
>
> app_name = "app.model2"
>
> urlpatterns [ ]
>
>
> Frankly, I want the shortest urls possible, so if there was a way to do
> this without putting 'model1' or 'model2' in the url, that would be great,
> but I'm not seeing a way around it - assuming the app_name = 'app.model1'
> thing even works.
>
> Another option I thought of but don't know if possible:
>
> try url(r'^(?P) )
> except url(r'^modelname(?P) )
>
> I've never seen anything like that, though. If it did work, I doubt it
> would fit inside urlpatterns[ ], but maybe I could put it after?
>
> Or I could hack the code that handles url name collisions (once I find it)
>
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fdb1ce25-0840-49d3-ab79-3011128a7207%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYVRQvVCF5NUVwhcUX38SCxo_oxoxkS0MVn%3DGHf8y8GaXw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: URL Namespaces

2014-03-07 Thread anubhav joshi
Thanks.
That was quite helpful.

Regards
Anubhav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1df3efc7-f9c4-49d6-a350-ab5a19dc5379%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: URL Namespaces

2014-03-05 Thread Jonathan Baker
Much like module namespacing, they allow you to group subitems in more
logical and descriptive ways while preventing naming collisions. So, if a
Django project has a blog, with a list of Posts at /blog/posts/, a sensible
url name for that pattern might be "post_list" (and using url names in your
application is a good thing). Now, say the same project also exposes a
public API, which includes includes Posts at the endpoint
/api/v1/blog/posts/. Since using url names is a good thing, what would you
name the API endpoint while not clobbering the already-defined 'post_list'
url name? Sure, something like 'api_post_list' might be acceptable for the
endpoint I just described, but I prefer the namespaced version
'api:post_list' for a few reasons:

1) You can define a namespace one time when you include a collection of URL
patterns into the top-level urls.py
2) Because you only have to do this once, changing namespaces is a matter
of changing one thing in one place
3) each url pattern in the namespace url pattern collection has no idea
it's even been namespaced, which means you can go on defining the url names
without having to remember some convention for preventing naming collisions
(since django takes care of the for you by following the namespace:url_name
pattern across the board).

Admittedly, this isn't very useful on small projects, but if you encounter
a situation like the one above (or your project grows very large), then the
potential for naming collisions very quickly becomes a reality.

Hope this helps,
Jonathan


On Wed, Mar 5, 2014 at 4:25 AM, anubhav joshi  wrote:

> Can anyone explain how URL Namespaces work in django.
> Some examples would be great.
> (Apart from those in the docs.)
>
> Regards,
> Anubhav Joshi
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1d59cdb2-d375-493d-88ae-da2ece757eee%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPMFOb68DHThkFMW2mPtNPLW2cs4pA4XOVcYi-PygPpWCvgkAw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Url Namespaces

2009-07-27 Thread Vitaly Babiy
Thanks, I knew it had to be somewhere in the docs.
Vitaly Babiy


On Sun, Jul 26, 2009 at 7:16 PM, Vasil Vangelovski
wrote:

>
> See this:
>
>
> http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces
>
> On Mon, Jul 27, 2009 at 12:13 AM, Vitaly Babiy wrote:
> > How does one go about registering a namespace?
> > I can't seem to find it in the docs.
> > Thanks,
> > Vitaly Babiy
> >
> > >
> >
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---



Re: Url Namespaces

2009-07-26 Thread Joshua Russo
On Sun, Jul 26, 2009 at 9:13 PM, Vitaly Babiy  wrote:

> How does one go about registering a namespace?


Not quite sure what you mean by that, but my guess is that you are assuming
you need to register your namespace (modules?) before being able to import
them. If this is what you are asking, you don't. The import dot notation is
simply the directory structure, optionally followed by the file, optionally
followed by the object within the file.

It will use the PythonPath list variable to try to find the namespaces you
are referring to. This includes directories in python and your project.

For example, in my urls.py I have:
from django.contrib import admin

urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root)

This could also be written as:
from django.contrib.admin.site import root

urlpatterns = patterns('',
(r'^admin/(.*)', root)

This (roughly) represents:
C:\python25\Lib\site-packages\django\contrib\admin\site.py (function root())

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---



Re: Url Namespaces

2009-07-26 Thread Vasil Vangelovski

See this:

http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces

On Mon, Jul 27, 2009 at 12:13 AM, Vitaly Babiy wrote:
> How does one go about registering a namespace?
> I can't seem to find it in the docs.
> Thanks,
> Vitaly Babiy
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---