#Not getting any results on GET request.

33 messages · Page 1 of 1 (latest)

fallow venture
#

I am developing a backend app in django rest framework that will facilitate e-voting. I have a bunch of django apps and one of them is users. I've completed the models.py, migrated to DB, added the serializers.py, modified views and urls. However, when I submit a GET request on the url specified in the users/urls.py i'm getting a page not found.

#
#users/models.py
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.auth.models import AbstractBaseUser 
from django.core.validators import MaxLengthValidator, MinLengthValidator

class User(AbstractBaseUser):
    reg_date = models.DateField(auto_now=True)
    token = models.CharField(unique=True, max_length=8)
    idnp = models.PositiveIntegerField(unique=True, validators=[MinLengthValidator(limit_value=13), MaxLengthValidator(limit_value=13)])
    phone = PhoneNumberField(null=False, blank=False, unique=True)
    last_login = models.DateField()
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'phone'
#
#users/serializers.py
from rest_framework import serializers
from users import models

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.User
        fields = '__all__'
#
#users/urls.py
from django.urls import path
from users import views

urlpatterns = [
    path('user-list', views.UserList.as_view()),
]
#
#users/views.py
from rest_framework import generics
from users import models, serializers
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAdminUser


class UserList(generics.ListCreateAPIView):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSerializer
#
#app/settings.py
INSTALLED_APPS = [
    #'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
    'elections',
    'authen'
]
#
#app/urls.py
from django.urls import path, include


urlpatterns = [
    path('api/', include([
        path('users/', include('users.urls'))
    ]))
]
#
Page not found (404)
Request Method:     GET
Request URL:     http://127.0.0.1:8000/api/users/user-list/

Using the URLconf defined in evoteapp.urls, Django tried these URL patterns, in this order:

    api/ users/ user-list

The current path, api/users/user-list/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
sand tree
#
# users/urls.py
urlpatterns = [
    path('user-list', views.UserList.as_view()),
]

I think your end-point here is not terminated by a /, so Django only sees api/users/user-list as a valid URL.

#

You can either change it to path('user-list/', ...) or query it without the trailing slash.

fallow venture
#

Oh, it seems to have worked...? but now i'm getting a TemplateDoesNotExist error :')

#
TemplateDoesNotExist at /api/users/user-list/

rest_framework/api.html

Request Method:     GET
Request URL:     http://127.0.0.1:8000/api/users/user-list/
Django Version:     4.2.5
Exception Type:     TemplateDoesNotExist
Exception Value:     

rest_framework/api.html

Exception Location:     /home/annannas/Documents/PBL/env/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Raised during:     users.views.UserList
Python Executable:     /home/annannas/Documents/PBL/env/bin/python3.8
Python Version:     3.8.18
Python Path:     

['/home/annannas/Documents/PBL/evoteapp',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/annannas/Documents/PBL/env/lib/python3.8/site-packages']

Server time:     Tue, 03 Oct 2023 06:20:49 +0000
sand tree
#

Do you have app dirs enabled?

# settings.py
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "APP_DIRS": True,
    },
]
fallow venture
#

I'll check

#

Yeah it's enabled

sand tree
#

Is rest_framework in your INSTALLED_APPS?

#

I think that happened to me last time 😅

fallow venture
#

No, it's not...?

#

how do i install it

sand tree
#

You installed it in your virtual env using pip install?

sand tree
#

Then you just need to add it in settings.py inside in the INSTALLED_APPS list.

#
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    # your apps here
]
fallow venture
#

odd

#

ModuleNotFoundError: No module named 'rest-framework'

sand tree
#

It's an underscore, not a dash 🙂

fallow venture
#

Ohhh, okay

#

niiiiiiiiiiice, it works

#

what an odd thing tho

sand tree
#

If modules used dashes, the interpreter would think you mean rest minus framework 😅

fallow venture
#

hahahah, makes sense.

#

Thank you a lot.