Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django REST Framework RESTful Django Installation and Setup

Ian Salmon
PLUS
Ian Salmon
Courses Plus Student 10,687 Points

Getting it to work in modern Django

This was a question, but now it's just a guide for anyone trying to get this code imported onto their local machine with a modern version of everything. It took me too long to figure this out, and I hope anyone following along can save themselves time with this little guide.

Change your urls to path. You need to change your import as well as default regex.

from django.urls import path, include
from django.contrib import admin


urlpatterns = [

    path('admin/', admin.site.urls),

    path('api-auth/', include('rest_framework.urls',
                             namespace='rest_framework')),

]

Next, in settings.py.

1) Change MIDDLEWARE_CLASSES to MIDDLEWARE. This was change in Django 2.0 I believe.

2) Remove SessionAuthenticationMiddleware from the MIDDLEWARE list. This is enabled by default since Django 1.10

Finally, in models.py

Anything that uses a ForeignKey needs to have the mandatory on_delete default setting. So in your Review class:

class Review(models.Model):

    course = models.ForeignKey(Course, related_name='reviews', on_delete=models.CASCADE)

I believe that's it.

MOD EDIT: Fixed markdown

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Ian,

Thanks for providing this guide.

A quick comment on urls.path. This is a new, simplified, way of describing url paths in modern Django. Paths are often, but not always, the same as the regex from old-style Django. If you want to play it safe, new Django introduced urls.re_path at the same time as urls.path, and you can use exactly the same regex syntax as before.

Cheers

Alex