I am creating a Todo App using Django 3.2 with python 3.6.8 and react js.
I have installed the djangorestframework and Django-cors-headers. However, I cannot get the App views. I could not do the migration as well.
I got the following error when I try to run the server:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 423, in check
databases=databases,
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\Users\User\Desktop\mydjango\mydjango\urls.py", line 22, in
path('todoappApi/', include('todoappApi.urls')),
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\Users\User\Desktop\mydjango\todoappApi\urls.py", line 2, in
from . import views
File "C:\Users\User\Desktop\mydjango\todoappApi\views.py", line 4, in
from rest_framework.decorators import todoappApi_view
ImportError: cannot import name 'todoappApi_view'
Here is my views:
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import todoappApi_view
from rest_framework.response import Response
from .serializers import TaskSerializer
from .models import Task
# Create your views here.
#The function todoappApiOverview will allow React js to get responses from the API
@todoappApi_view(['GET'])
def todoappApiOverview(request):
todoappApi_urls = {
'List':'/task-list/',
'Detail View':'/task-detail/<str:pk>/',
'Create':'/task-create/',
'Update':'/task-update/<str:pk>/',
'Delete':'/task-delete/<str:pk>/',
}
return Response(todoappApi_urls)
#React js will get the data responses using the taskListfollowing functions
@todoappApi_view(['GET'])
def taskList(request):
tasks = Task.objects.all().order_by('-id')
serializer = TaskSerializer(tasks, many=True)
return Response(serializer.data)
@todoappApi_view(['GET'])
def taskDetail(request, pk):
tasks = Task.objects.get(id=pk)
serializer = TaskSerializer(tasks, many=False)
return Response(serializer.data)
@todoappApi_view(['POST'])
def taskCreate(request):
serializer = TaskSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@todoappApi_view(['POST'])
def taskUpdate(request, pk):
task = Task.objects.get(id=pk)
serializer = TaskSerializer(instance=task, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@todoappApi_view(['DELETE'])
def taskDelete(request, pk):
task = Task.objects.get(id=pk)
task.delete()
return Response('Item succsesfully delete!')
Here is the app urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.todoappApiOverview, name="todoappApi-overview"),
path('task-list/', views.taskList, name="task-list"),
path('task-detail/<str:pk>/', views.taskDetail, name="task-detail"),
path('task-create/', views.taskCreate, name="task-create"),
path('task-update/<str:pk>/', views.taskUpdate, name="task-update"),
path('task-delete/<str:pk>/', views.taskDelete, name="task-delete"),
]
Here is the app settings:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todoappApi.apps.TodoappApiConfig',
'rest_framework',
'corsheaders',
]
