1

I have in my project in the main urls.py file the following:

# REST Framework packages
from rest_framework import routers
router = routers.DefaultRouter()

# ... My viewsets serialized
router.register(r'users', UserViewSet) 
# ... Another viewsets 
urlpatterns = [

    url(r'^$', HomeView.as_view(), name='home'),
    # Home url in my project        

    url(r'^', include('userprofiles.urls')),
    # Call the userprofiles/urls.py application

    url(r'^pacientes/',  include('userprofiles.urls', namespace='pacientes')),
    # Patients url

    url(r'^api/', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    # Rest frameworks urls
]

Until here, when I type in my browser calling to my local server http://localhost:8000/api/ I get this like response:

[08/Dec/2016 16:39:42] "GET /api/ HTTP/1.1" 200 7084

And my REST url's serialized models appear

After, I've created one url additional in the userprofiles/urls.py application with some regular expression of this way:

from .views import PatientDetail
urlpatterns = [
    url(r'^(?P<slug>[\w\-]+)/$', PatientDetail.as_view(), name='patient_detail'),

]

And, when I go to http://localhost:8000/api/ I get this response:

Not Found: /api/
[08/Dec/2016 16:42:26] "GET /api/ HTTP/1.1" 404 1753

My rest frameworks url is not found and in my browser, denote that the url which call to PatientDetailView is the origin of this problem:

enter image description here

My PatientDetailView have the following:

class PatientDetail(LoginRequiredMixin, DetailView):
    model = PatientProfile
    template_name = 'patient_detail.html'
    context_object_name = 'patientdetail'

    def get_context_data(self, **kwargs):
        context=super(PatientDetail, self).get_context_data(**kwargs)
    # And other actions and validations here in forward ...

In my regular expressions defined in userprofiles/urls.py I am doing:

url(r'^(?P<slug>[\w\-]+)/$', PatientDetail.as_view(), name='patient_detail')

The model PatientProfile have a slug field (username of patient). I pass this slug in the url.

In addition I want that allow me alphanumerics characters upper and lowercase with the [\w\-] parameter, and allow underscores and hyphen characters and many times.

Is possible that my regular expression may be the origin of the problem? What can be happened in relation to my /api django-restframework url be not found?

2 Answers 2

1

api is full match for regular expression [\w\-]+ using in 'userprofiles.urls'. So when you type http://localhost:8000/api/ Django returns first found urlpattern which is url(r'^', include('userprofiles.urls')). Try to swap lines:

url(r'^pacientes/',  include('userprofiles.urls', namespace='pacientes')),
# Patients url
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
url(r'^', include('userprofiles.urls')),
Sign up to request clarification or add additional context in comments.

Comments

1

The order in which you define your URLs matter. Django tries to match your URL against every pattern until one of them matches.

Note that you included this line:

url(r'^', include('userprofiles.urls')),

before:

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

which wasn't a problem because the first pattern to match was the latter.

However, when you added the PatientDetail view URL pattern:

url(r'^(?P<slug>[\w\-]+)/$', PatientDetail.as_view(), name='patient_detail')

/api/ was a match. Thus, the PatientDetail view was called and your 404 error happened because no patient was found with username api, not because the URL was not found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.