Reality Check for (problematic?) DRF Installation Instructions

31 views
Skip to first unread message

Daniel Cunningham

unread,
Aug 7, 2020, 10:43:41 PM8/7/20
to Django REST framework
Hi All:

I need a reality check on a misunderstanding I'm having with the installation instructions for  DRF.  I've used DRF successfully before (about a year ago) so I'm surprised I'm having this difficulty.

The instructions in the first "Installation" section call for a modification to urlpatterns (whereby we append a new pattern for DRF).  I noticed, when testing, that this results in an immediate exception:

NameError: name 'url' is not defined

I was curious to see if others are running into this error?  And if so, perhaps a quick correction would be useful to new DRF users, because an immediate error from the "here's how to install this" page seems like it would cause a bad first impression. 

As a secondary issue, I was also quite puzzled by the syntax itself,  as it does not match the urlpatterns syntax in the "Example" section immediately following this first section.  I'm guessing  this is because I am a relative "newbie" to Django and DRF, but a bit of explanation would go a long way here, because it's quite unlike the patterns used by the rest of the example code.  Perhaps a comment with a link to the convention or an example would be useful?

I know DRF is a great product, because it has worked quite well for me before, so as a work-around, I bypassed those last steps in the first part of the installation instructions, and went on to the "Example" section.  I implemented those instructions, and things went much smoother.

One final (minor) issue: I noticed, when testing, that the instructions for modifying urls.py seem to imply a swap out of the entire urlpatterns section, but if you do that, you lose the ability to get to the original Django admin page link at: 127.0.0.1:8000/admin/  (in fact it will throw another exception).  So perhaps the code in this section should be modified to fix that problem.

All that aside, I love the product.  I've got a great DRF example going, and I can continue my work.  I just wanted you all to know about these potential problematic issues, and see if there's a.way to make the introductory experience even better.

Wanderley S

unread,
Aug 7, 2020, 11:44:55 PM8/7/20
to django-res...@googlegroups.com
Hi Daniel.

Since you didn't posted any code, I can only make some assumptions here.
Since Django 2, the url function has been sort of replaced by path function, so instead writing your urls like:

urlpatterns = [ url(some_regexp, your_views)]

You will probably use:

urlpattern = [path("some-path/", your views]

Above examples are not actually code. I'm just giving you an idea.
The url function had been moved to another package in Django 2, and I'm not sure if it even exists in DJango3 

I'd suggest taking a look at the Django Documentation as well, since Django itself had some important changes since version 1.11
About the admin not working, I can only guess that since you're having issues with your urls mapping, it's probably getting lost and not been able to reach the admin page.

Hope I could help.
If not, please post some part of your code, and I might help you better on this.

Cheers, and happy coding!




--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/6307fb92-3cce-4c5f-b630-6d076fb081dao%40googlegroups.com.

Daniel Cunningham

unread,
Aug 8, 2020, 1:56:57 AM8/8/20
to Django REST framework
Appreciate the response.

I was working directly off a brand-new generated Django project, which I had named
restservice

...and following the installation instructions on:  https://www.django-rest-framework.org/#installation

So I (trivially) modified the skeleton to the state (below), and when I ran it, encountered (completely unexpected) exceptions.

"""
restservice URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""



#
# Original (default) now disabled per DRF Installation Guide: https://www.django-rest-framework.org/#example
# We intend to create a read-write API for accessing information on the users of our project.
#
from django.contrib import admin
from django.urls import path, include

urlpatterns
= [
    path
('admin/', admin.site.urls),
    url
(r'^api-auth/', include('rest_framework.urls'))   # Per: https://www.django-rest-framework.org/#installation:
                                                         
# ...add REST framework's login and logout views.
]


It hasn't stopped me -- I went on to adopt the Quickstart examples and moved on.  And, by the way, DRF looks nice!  As in  REALLY nice!

But I was thinking it wasn't good for the project to have errors from the very first example.

Is there someone responsible for the DRF documentation examples that I should notify of this issue?

Best regards,

-- Daniel
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Wanderley S

unread,
Aug 8, 2020, 12:32:52 PM8/8/20
to django-res...@googlegroups.com
Hi again Daniel.
You made your point, I don't agree with you though.
Seems to me that the documentation is fine 
In the "installation" link you were following you have just small pieces of code, so don't follow this as a tutorial, if you just want "copy and paste" go the the "Tuorial" part from the DRF (Django Rest Framework).
The "Installation" provides only some examples, but you should not just copy and paste from there.

In your code for example you're trying to use the url function without importing it, for this code to work properly, you should first import the url function.

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url #IMPORT THIS LINE IT WILL WORK.

urlpatterns 
= [

    path
('admin/', admin.site.urls),
    url
(r'^api-auth/', include('rest_framework.urls'))   # Per: https://www.django-rest-framework.org/#installation:
                                                         
# ...add REST framework's login and logout views.
]
as I said, follow the tutorial session and you will find a really working project.

That would be advises.

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/3535b2aa-5df4-47bd-8a81-74336e9f4c86o%40googlegroups.com.

Carl Nobile

unread,
Aug 8, 2020, 1:03:25 PM8/8/20
to django-res...@googlegroups.com
So the url() function is deprecated you should use re_path() if you need to use regex.

from django.urls import path, re_path

~Carl




--
-------------------------------------------------------------------------------
Carl J. Nobile (Software Engineer)
carl....@gmail.com
-------------------------------------------------------------------------------

Daniel Cunningham

unread,
Aug 8, 2020, 11:15:20 PM8/8/20
to Django REST framework
Thanks again, everyone!

I can see your point-of-view, for sure.  If you're a Django veteran, and a DRF expert (and I'm glad you and the others are) it probably is fine (and very obvious).

I'm just giving you my impression, as a "customer", of my first time "product touch" and impression.  Where I was coming from was going past the purely technical problem & solution to thinking about product-market fit, adoption, competition, and "soft" issues like that.  Hopefully, the project owners care about stuff like that?  I don't know.

My honest (and hopefully non-insulting) impression:  If you're a product or project manager, pressed for time to find a quick working prototype you can hand off to a dev team, it was simply... confusing.

I got past it (and again, thanks for the help!)  But it was... puzzling (think Spock eyebrow raise) and "full of friction".  I had to interpolate between two different approaches.

I think it's easily curable -- after all, it's "only" documentation (which is actually quite hard to produce, if you want high quality).  And if it was a product I was personally responsible for, I'd fix it.  With all the competing solutions out there, anything that detracts from fast evaluation & adoption should be eliminated.  If I had spare cycles, I'd see if I could get in touch with the maintainers and make a contribution.  But that's not realistic for me right now.  

Which is a shame, because it's a heck of a framework.  But keep in mind that you're also competing against RESTful solutions (like Node + Loopback -- actively used in our shop too).

Final status: encouraging.  I've made it way past my original problem, and have a nice instance of DRF up on the cloud.  Again, thanks very much!

--  Daniel


On Saturday, August 8, 2020 at 9:32:52 AM UTC-7, Wandss wrote:
Hi again Daniel.
You made your point, I don't agree with you though.
Seems to me that the documentation is fine 
In the "installation" link you were following you have just small pieces of code, so don't follow this as a tutorial, if you just want "copy and paste" go the the "Tuorial" part from the DRF (Django Rest Framework).
The "Installation" provides only some examples, but you should not just copy and paste from there.

In your code for example you're trying to use the url function without importing it, for this code to work properly, you should first import the url function.

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url #IMPORT THIS LINE IT WILL WORK.

urlpatterns 
= [
    path
('admin/', admin.site.urls),
    url
(r'^api-auth/', include('rest_framework.urls'))   # Per: https://www.django-rest-framework.org/#installation:
                                                         
# ...add REST framework's login and logout views.
]
as I said, follow the tutorial session and you will find a really working project.

That would be advises.

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

AJ Bowen

unread,
Aug 9, 2020, 6:43:18 AM8/9/20
to Django REST framework
By the way, there's an open pull request by @sanjusci to address this (completely valid, IMO) issue here.
Reply all
Reply to author
Forward
0 new messages