Not able to pass API urls to the REST API app - django

I have my Django project split into three apps:
frontend: You guessed it, the pure frontend.
shared_stuff: This is where I've put my models, because I feel they might be shared between apps later on.
rest_api: The, well, REST API.
All three apps are also registered in settings.py.
Now my problem is that the urls meant for rest_api app are also being serviced by the frontend app. Here's what my main urls.py looks like:
urlpatterns = [
url(r'^api/v1', include('rest_api.urls')),
url(r'^', include('frontend.urls')),
]
Am I doing something wrong? Please feel free to ask for more info!

Try this: url(r'^/api/v1', include('rest_api.urls')). Just add a front slash to the pattern.
When you visit http://host/api/v1/xxx, django will use /api/v1/xxx to match the patterns. But ^api/v1 fails the match because of the missing front slash.

Related

What is the ideal format/structure of urlconfs in django 2

I creating an application and one of its urlconf is as follows
urlpatterns = [
path('', DashboardView.as_view(),name='dashboard:index'),
]
I am coming from PHP background (say Laravel) where we name our routes like below
dashboard:index - for get
dashboard:store - for post
dashboard:update - for patch etc...
So I named my route as above, but while performing the system check, the following warning comes up.
System check identified some issues:
WARNINGS: ?: (urls.W003) Your URL pattern '' [name='dashboard:index']
has a name including a ':'. Remove the colon, to avoid ambiguous
namespace references.
System check identified 1 issue (0 silenced).
So my question is what is the ideal naming format of URLs in Django in general.
dashboard_index ?
dashboard.index ?
I guess the best place to find some kind of convention is in the django admin app, where I found this:
urlpatterns = [
url(r'^$',
views.BaseAdminDocsView.as_view(template_name='admin_doc/index.html'),
name='django-admindocs-docroot'),
url(r'^bookmarklets/$',
views.BookmarkletsView.as_view(),
name='django-admindocs-bookmarklets'),
# and so on...
]
So, a string representing the url with dashes between words. I think is also important the name to be very explicit, not acronyms or shortened names.
EDIT:
Example of general url/path naming from the docs (2.1):
path('archive/', views.archive, name='news-archive')
Also it's a good idea to have in mind python code style.

Integrate Django Oauth Toolkit urls correctly

I followed the instructions from the official django-oauth toolkit doc (https://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_02.html) and included all oauth-toolkit urls manually to prevent users from creating applications.
In my root urls.py i added the following line to the url patterns, as showed in the tutorial:
url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")),
I used the include from django.conf.url package.
Now when i start the server, it says
"Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead."
when i add a name attribute to the url-command like this:
url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider"), name="oauth2_provider"),
the server starts up, but when i try to visit "localhost/o/applications" it says "NoReverseMatch: 'oauth2_provider' is not a registered namespace"
What am i doing wrong?
Try
url(r'^o/', include(('oauth2_provider.urls', 'oauth2_provider_app', ), namespace='oauth2_provider'), ),
Refer oauth2_provider

Django URL reversing NoReverseMatch issue

I'm trying to use reverse to redirect a user to a login page from a third party App I'm using.
The URL config has:
urlpatterns = [
# authentication / association
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
name='begin'),
How can I accomplish this? I've tried
return redirect(reverse('social:login'), args=('facebook',))
and
return redirect(reverse('social:login'), kwargs={'backend':fb})
(to get to /login/facebook) but I'm getting a NoReverseMatch
The Django URL system and RegExes are confusing me a bit =(
EDIT: All right, it looks like I was making a mess with these URLs.
A simple solution that works (thank you #Alasdair in the comments):
return redirect('social:begin', backend='facebook')

How to document Django URLs with Sphinx?

I'm generating a new Django project, with several apps. The main goal for this project is to create a REST API. Right now, I've Sphinx working, creating documentation of all my project with
sphinx-quickstart
and
sphinx-apidoc -o doc/packages .
All works well, except for URLs. I wanted to have my URLs documented, as a nice API, fully integrated with the rest of Sphinx documentation.
Is it possible?
It is. This isn't directly documenting the URLs, but I connected the URLs to view documentation in this way:
Make a Sphinx extension that looks something like:
from django.core.urlresolvers import get_resolver
def setup(app):
app.connect('autodoc-process-docstring', process_django_view)
def process_django_view(app, what, name, obj, options, lines):
if what=='function':
res = get_resolver()
if res.reverse_dict.has_key(obj):
url_struct = res.reverse_dict[obj]
lines[:0] = [
"| URL structure: %s\n" % url_struct[0][0][0]
]
Then you need to add it to extensions in your conf.py and also load in the Django environment like explained here: How to build sphinx documentation for django project
There's some other stuff like the regex in url_struct and the parameters that you can include. My own also searches multiple URL resolvers that correspond to different domains.

Decoupling django apps 2 - how to get object information from a slug in the URL

I am trying to de-couple two apps:
Locations - app containing details about some location (town, country, place etc)
Directory - app containing details of places of interest (shop, railway station, pub, etc) - all categorised.
Both locations.Location and directory.Item contain lat/lng coords and I can find items within a certain distance of a specific lat/lng coord.
I'd like to use the following URL structure:
/locations/<location_slug>/directory/<category_slug>/
But I don't want to make my directory app reliant on my location app.
How can I translate this url to use a view like this in my directory app?
items_near(lat, lng, distance, category):
A work around would be to create a new view somewhere that translates this - but where should I put that? if it goes in the Directory app, then I've coupled that with my Location app, and vice versa.
Would a good idea be to place this workaround code inside my project URLs file? Thus keeping clear of both apps? Any issues with doing it like this?
For your urlpattern to work, the view function invoked has to be aware of both Locations and Directories. The short answer is you can put this view function anywhere you want - it's just a python function. But there might be a few logical places for it, outside of your Directory or Location app, that make sense.
First off, I would not put that view code in in your top-level urls.py, as that file is intended for URLconf related code.
A few options of where to put your view:
Create a new view function in a file that lives outside of any particular app. <project_root>/views.py is one possibility. There is nothing wrong with this view calling the item_near(..) view from the Directory app.
# in myproject/urls.py
urlpatterns = (
...
(r'/locations/(?P<location_slug>[\w\-]+)/directory/(?P<category_slug>[\w\-]+)/',
'myproject.views.items_near_from_slug')
)
# in myproject/views.py
from directory.views import items_near
def items_near_from_slug(request, location_slug, category_slug):
location = get_object_or_404(Location, slug=location_slug)
distance = 2 # not sure where this comes from
# And then just invoke the view from your Directory app
return items_near(request, location.lat, location.lng, distance, category_slug)
Create a new app and put the code there, in <my_new_app>/views.py. There is no requirement that a Django app need to have a models.py, urls.py, etc. Just make sure to include the __init__.py if you want Django to load the app properly (if, for instance, you want Django to automatically find templatetags or app specific templates).
Personally, I would go with option 1 only if the project is relatively simple, and <project_root>/views.py is not in danger of becoming cluttered with views for everything. I would go with option 2 otherwise, especially if you anticipate having other code that needs to be aware of both Locations and Directories. With option 2, you can collect the relevant urlpatterns in their own app-specific urls.py as well.
From the django docs here if you're using django >=1.1 then you can pass any captured info into the included application. So splitting across a few files:
# in locations.urls.py
urlpatterns = ('',
(r'location/(?P<location_slug>.*?)/', include('directory.urls')),
#other stuff
)
# in directory.urls.py
urlpatterns = ('',
(r'directory/(?P<directory_slug>.*?)/', 'directory.views.someview'),
#other stuff
)
# in directory.views.py
def someview(request, location_slug = None, directory_slug = None):
#do stuff
Hope that helps. If you're in django < 1.1 I have no idea.
Irrespective of how much ever "re-usable" you make your app, inevitably there is a need for site-specific code.
I think it is logical to create a "site-specific" application that uses the views of the reusable and decoupled apps.