Boa tarde,
Uso windows e através do VirtualBox instalei o redis no ubuntu, permitindo esse subsistema no windows (depois de bastante trabalho, o "make" não funcionou).
Agora estou tentando conectar o django (windows) com o redis que está rodando no ubuntu.
Estou recebendo o seguinte erro:
redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. Nenhuma conexão pôde ser feita porque a máquina de destino as recusou ativamente.[03/Jan/2022 14:15:19] "GET / HTTP/1.1" 500 146441
Configurações do Redis no Ubuntu:
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"
PIDFile=/run/redis/redis-server.pid
[Install]
WantedBy=multi-user.target
----
bind 127.0.0.1 ::1
protected-mode no
port 6379
tcp-backlog 511
supervised systemd
pidfile /run/redis/redis-server.pid
daemonize no
Código do django (windows), no settings do setup:
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.QueryParameterVersioning',
'DEFAULT_PERMISSION_CLASSES':[
'rest_framework.permissions.DjangoModelPermissions',
'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication'
],
'DEFAULT_THROTTLE_CLASSES':[
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon':'10/day',
'user':'50/day'
},
}
CORS_ALLOWED_HEADERS = [
"http://localhost:3000",
]
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": 'django_redis.client.DefaultClient'
}
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
Código das viewsets:
from rest_framework import status, viewsets, generics
from escola.models import Aluno, Curso, Matricula
from escola.serializer import AlunoSerializer, AlunoSerializerV2, CursoSerializer, MatriculaSerializer, ListaMatriculasAlunoSerializer, ListaAlunosMatriculadosSerializer
from rest_framework.authentication import BasicAuthentication
from rest_framework.permissions import IsAuthenticated, DjangoModelPermissions
from rest_framework.response import Response
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
class AlunosViewSet(viewsets.ModelViewSet):
"""Exibindo todos os alunos e alunas"""
queryset = Aluno.objects.all()
authentication_classes = [BasicAuthentication]
permission_classes = [IsAuthenticated, DjangoModelPermissions]
def get_serializer_class(self):
if self.request.version == 'v2':
return AlunoSerializerV2
else:
return AlunoSerializer
class CursosViewSet(viewsets.ModelViewSet):
"""Exibindo todos os cursos"""
queryset = Curso.objects.all()
serializer_class = CursoSerializer
authentication_classes = [BasicAuthentication]
permission_classes = [IsAuthenticated]
http_method_names = ['get', 'put', 'patch']
def create(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
serializer.save()
response = Response(serializer.data, status=status.HTTP_201_CREATED)
id = str(serializer.data['id'])
response['Location'] = request.build_absolute_uri() + id
return response
class MatriculaViewSet(viewsets.ModelViewSet):
"""Listando todas as matrículas"""
queryset = Matricula.objects.all()
serializer_class = MatriculaSerializer
authentication_classes = [BasicAuthentication]
permission_classes = [IsAuthenticated]
@method_decorator(cache_page(40))
def dispatch(self, *args, **kwargs):
return super(MatriculaViewSet, self).dispatch(*args, **kwargs)
class ListaMatriculasAluno(generics.ListAPIView):
"""Listando as matrículas de um aluno ou aluna"""
def get_queryset(self):
queryset = Matricula.objects.filter(aluno_id=self.kwargs['pk'])
return queryset
serializer_class = ListaMatriculasAlunoSerializer
authentication_classes = [BasicAuthentication]
permission_classes = [IsAuthenticated]
Será que alguém poderia me ajudar?