0

I am having problems with the example in the documentation.

When I try going to the default route "/", I keep getting a 404. The way the documentation example reads, I should be able to get a User list?

Here is the urls.py code:

from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'email', 'is_staff')

# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),

]

I have the rest_framework added to my application, too. I can see the various User related routes when I look at the router object via the manage.py shell, but I keep getting the 404.

I am implementing this in an existing new project, but I have yet to actually put anything in the project so I don't think the issue is there. My current set up is Nginx proxy -> Gunicorn -> Django. The \admin site works so it looks like other things are routed fine. Any advice would be great to help me along.

Edit: Image of the debug screen I am receiving. URL is masked out, but this is going to just the main site server/. Image

Branco
  • 191
  • 1
  • 18
  • There aren't any views that handle `/`. You should definitely change `r'^/'` to `r'^'` as Linovia suggested, then go to `server/users/`. If that doesn't work, update your question to show that code and the error you get for that URL. – Alasdair Jan 04 '18 at 14:58
  • **As stated in the comments to @Linovia's answer.. this has already been done.. I am not going to show every variant of code that I have tried in the major example as that would be counter productive ** – Branco Jan 04 '18 at 15:11
  • I'm not asking you to show every variant of code, just one that isn't known to be incorrect. – Alasdair Jan 04 '18 at 15:24
  • 1
    Does it work with the development server ? – Linovia Jan 04 '18 at 15:46
  • Yeah, that was what I just tried and noticed it wasn't updating via the gunicorn->nginx setup. It is a new stack and I was unaware of the need to reload services when there are changes. – Branco Jan 04 '18 at 15:50

2 Answers2

0

Why do you add a / to the router's url ? Your code:

url(r'^/', include(router.urls)),

The example's code:

url(r'^', include(router.urls)),
Linovia
  • 19,812
  • 4
  • 47
  • 48
  • I have tried both ways, tried `/users`, `/users.`, `/users/1`, etc. Whenever I look at the debug page, it only shows the `/admin` route and `/api-auth/` routes. I would _assume_ that the `router.urls` would add additional routes, but I don't know DRF well enough. – Branco Jan 04 '18 at 13:42
  • remove the / from the urls.py as shown and head to `http://yoursite/` and you should get links to the appropriate url. – Linovia Jan 04 '18 at 13:55
  • Yeah I have tried that before along with the other routes specified in the previous comment. – Branco Jan 04 '18 at 14:02
0

Finally got this solved. It relates to Restarting Gunicorn/Nginx when changes are made to files.

Starting with a project already up and running then adding DRF didn't update the gunicorn/nginx parts so even if the route was correct, it wouldn't update to the outside world.

So, this is one way the correct URLs can be in urls.py but not reflect to the outside world.

Branco
  • 191
  • 1
  • 18