Re: Django REST Framework: How to add prefix in URL for versioning

2014-11-05 Thread Lee Hinde
Swap the urls:

 url(r'^v1/', include(v1_urls, namespace="v1"))

url(r'^', include(default_urls, namespace="default")),


On Wed, Nov 5, 2014 at 5:01 AM, Shoaib Ijaz  wrote:

> I am trying to create version for REST application. Here is my URL Examle
>
> www.myapi.com/foo [default version]
> www.myapi.com/v1/foo [version one]
>
> This is the project structure
>
> ├── __init__.py├── settings.py├── urls.py├── default_app│ ├── __init__.py│ 
> ├── serializer.py│ ├── models.py│ ├── views.py│ ├── urls.py│ └── v1_app├── 
> __init__.py├── serializer.py├── models.py├── views.py├── urls.py
>
> *default_app urls.py*
>
> from django.conf.urls import *from default_app import views as df_viewsfrom 
> rest_framework import routers
>
> router = routers.DefaultRouter()
> router.register(r'foo', df_views.viewname, "foo")
> urlpatterns = router.urls
>
> *v1_app urls.py*
>
> from django.conf.urls import *from v1_app import views as ver_viewsfrom 
> rest_framework import routers
>
> router = routers.DefaultRouter()
> router.register(r'foo', ver_views.viewname, "foo")
> urlpatterns = router.urls
>
> *main file for urls.py*
>
> from django.conf.urls import patterns, include, urlfrom defualt_app import 
> urls as default_urlsfrom v1_app import urls as v1_urlsfrom 
> django.contrib.staticfiles.urls import staticfiles_urlpatterns
>
>
>
> urlpatterns += patterns('',
> url(r'^', include(default_urls, namespace="default")),
> url(r'^v1/', include(v1_urls, namespace="v1")))
>
> urlpatterns += staticfiles_urlpatterns()
>
> My issue is, when i using simple url without any prefix then it is working
>
> www.myapi.com/foo
>
> and when i used version prefix v1 or v2 then it throws error [Page not
> found (404)]
>
> www.myapi.com/v1/foo
>
> I got this idea from this link http://stackoverflow.com/a/21839842/1558544
>
> If I don't use middleware class then is this possible to get same result?
>
> Thank you
>
>
>

-- 
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/CA%2BePoMyV_O4zbJpdhbT409KkrP3%2BbM4UsoQgyfKMn8eV_sP2Gw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Django REST Framework: How to add prefix in URL for versioning

2014-11-05 Thread Shoaib Ijaz


I am trying to create version for REST application. Here is my URL Examle

www.myapi.com/foo [default version]
www.myapi.com/v1/foo [version one]

This is the project structure

├── __init__.py├── settings.py├── urls.py├── default_app│ ├── __init__.py│ ├── 
serializer.py│ ├── models.py│ ├── views.py│ ├── urls.py│ └── v1_app├── 
__init__.py├── serializer.py├── models.py├── views.py├── urls.py

*default_app urls.py*

from django.conf.urls import *from default_app import views as df_viewsfrom 
rest_framework import routers

router = routers.DefaultRouter()
router.register(r'foo', df_views.viewname, "foo")
urlpatterns = router.urls

*v1_app urls.py*

from django.conf.urls import *from v1_app import views as ver_viewsfrom 
rest_framework import routers

router = routers.DefaultRouter()
router.register(r'foo', ver_views.viewname, "foo")
urlpatterns = router.urls

*main file for urls.py*

from django.conf.urls import patterns, include, urlfrom defualt_app import urls 
as default_urlsfrom v1_app import urls as v1_urlsfrom 
django.contrib.staticfiles.urls import staticfiles_urlpatterns



urlpatterns += patterns('',
url(r'^', include(default_urls, namespace="default")),
url(r'^v1/', include(v1_urls, namespace="v1")))

urlpatterns += staticfiles_urlpatterns()

My issue is, when i using simple url without any prefix then it is working

www.myapi.com/foo

and when i used version prefix v1 or v2 then it throws error [Page not 
found (404)]

www.myapi.com/v1/foo

I got this idea from this link http://stackoverflow.com/a/21839842/1558544

If I don't use middleware class then is this possible to get same result?

Thank you

-- 
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/42d759fb-19f2-483f-a979-1b61cce51d57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.