In this Django REST Framework lesson we are going to learn about Django REST Framework Views & Urls, even tough we are not going to use Django as frontend, but it is good to know about Django view functions and URL mapping.
Views are basically Python regular functions, and as the name suggests it is used for creating of views in Django, now open your views.py in your api app, this is the place that we can create our views in django, in the later part of the course we will change this with django rest framework viewsets and routers.
1 2 3 4 5 6 |
from django.shortcuts import render, HttpResponse # Create your views here. def Index(request): return HttpResponse('Geekscoders.com') |
Now let’s create our url mapping, you need to create a new python file in your django app, we are going to call it urls.py, in here we have simply mapped our view to the urls.
1 2 3 4 5 6 7 |
from django.urls import path from .views import Index urlpatterns = [ path('', Index), ] |
Now we need to include this url in our main projects urls.py.
1 2 3 4 5 6 7 |
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('api.urls')) ] |
Run your project and go to http://localhost:8000/, this will be the result.