milan
milan

Reputation: 2417

DRF does not work with wagtail

I have used DRF for rest api and puput for blog which is built on top of the wagtail. Before using puput, my rest api used to work but not now. For example when i try to open the url http://localhost:8000/rest-api/ui/tab/ i get following error

Page not Found 404 error Raised by: wagtail.wagtailcore.views.serve

settings.py

DJANGO_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
]

THIRD_PARTY_APPS = [
    # rest framework
    'rest_framework',

    # third party apps
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'ckeditor',
]

MACHINA_APPS = [
    'mptt',
    'haystack',
    'widget_tweaks',
    'django_markdown',
]

LOCAL_APPS = [
    'ui_mgmt',                # UI manager - tabs, cards etc
    'faq',                    # faq
]

PUPUT_APPS = [
    'wagtail.wagtailcore',
    'wagtail.wagtailadmin',
    'wagtail.wagtaildocs',
    'wagtail.wagtailsnippets',
    'wagtail.wagtailusers',
    'wagtail.wagtailimages',
    'wagtail.wagtailembeds',
    'wagtail.wagtailsearch',
    'wagtail.wagtailsites',
    'wagtail.wagtailredirects',
    'wagtail.wagtailforms',
    'wagtail.contrib.wagtailsitemaps',
    'wagtail.contrib.wagtailroutablepage',
    'compressor',
    'taggit',
    'modelcluster',
    'puput',
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + MACHINA_APPS + LOCAL_APPS + get_machina_apps() + PUPUT_APPS

urls.py

from ui_mgmt import urls as ui_mgmt_urls
urlpatterns = [
    url(r'^accounts/', include(allauth_urls)),
    url(r'^admin/', admin.site.urls),
    url(r'^forum/', include(board.urls)),
    url(r'', include('puput.urls')),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^rest-api/', include(ui_mgmt_urls)),
]

ui_mgmt/urls.py

url(r'^ui/tabs/$', tab_list, name='tab_list'),

ui_mgmt/views.py

@api_view(['GET','POST'])
def tab_list(request, format=None):
    if not request.user.is_authenticated():
        return Response(status=status.HTTP_401_UNAUTHORIZED)

    user = request.user
    if request.method == 'GET':
        tabs = Tab.objects.filter(belongsTo=user)
        serialized_data = ExtendedTabSerializer(tabs, many=True)
        return Response(serialized_data.data)

    elif request.method == 'POST':
        serialized_data = TabSerializer(data=request.data)
        if serialized_data.is_valid():
            serialized_data.save(belongsTo=user)
            newTab = ExtendedTabSerializer(Tab.objects.get(id=serialized_data.data['id']))
            return Response(newTab.data,status=status.HTTP_201_CREATED)

        return Response(serialized_data.errors,status=status.HTTP_400_BAD_REQUEST)

Why my DRF is not working? I get the same error for every rest api call.

Upvotes: 1

Views: 959

Answers (1)

pleasedontbelong
pleasedontbelong

Reputation: 20102

change the order of your url patterns.. puput should (probablly) the last element

urlpatterns = [
  url(r'^accounts/', include(allauth_urls)),
  url(r'^admin/', admin.site.urls),
  url(r'^forum/', include(board.urls)),
  url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
  url(r'^rest-api/', include(ui_mgmt_urls)),
  url(r'', include('puput.urls')),  # it will match the rest of patterns
]

Hope this helps

Upvotes: 3

Related Questions