使用DjangoFilterBackend过滤未找到网址

时间:2018-06-25 16:25:24

标签: django filter django-rest-framework http-status-code-404

我正在使用Django rest框架3.8.2。

我已阅读api-guide/filtering,并按照DjangoFilterBackend部分给出的示例进行操作。

models.py

class Pregunta(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=512, null=False)
    descripcion = models.CharField(max_length=200, null=False)
    categoria = models.ForeignKey(Pregunta_Categoria, on_delete=models.PROTECT)
    tipoRespuesta = models.ForeignKey(TipoRespuesta, on_delete=models.PROTECT, related_name='fk_id_tipoRespuesta') #sacar el default
    privated = models.BooleanField(default=False)
    status = models.CharField(max_length=1, default='A')
    created_by = models.ForeignKey(User, on_delete=models.PROTECT, related_name='proy_created_by8')
    created_on = models.DateTimeField(default=timezone.now)
    modified_by = models.ForeignKey(User, on_delete=models.PROTECT, related_name='proy_modified_by8')
    modified_on = models.DateTimeField(blank=True, null=True)

    class Meta:
        permissions = (
            ('view_pregunta', 'View Pregunta'),
        )

    def __str__(self):
        return "%s" % (self.name)

    def __unicode__(self):
        return u'%s' % (self.name)

views.py

class PreguntaViewSet(viewsets.ModelViewSet):
    queryset = Pregunta.objects.filter(estado='A')
    serializer_class = PreguntaSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('name', 'privated')

这是我的序列化器:

class PreguntaSerializer(serializers.ModelSerializer):
    nom_tipoRespuesta = serializers.CharField(
        source="tipoRespuesta.nombre", required=False, allow_blank=True)

    class Meta:
        model = Pregunta
        fields = (
            'id', 'name', 'descripcion', 'categoria', 'tipoRespuesta',
            'created_by', 'created_on', 'modified_by', 'modified_on',
            'status', 'privated', 'nom_tipoRespuesta',
        )

urls.py

from django.conf.urls import url, include
from rest_framework import routers
from proy import views

router = routers.DefaultRouter()
router.register(r'questions', views.PreguntaViewSet)
router.register(r'questionCategories', views.PreguntaCategoriaViewSet)
router.register(r'projects', views.ProyectoViewSet)
router.register(r'polls', views.EncuestaViewSet)
router.register(r'questionnaire', views.CuestionarioViewSet)
router.register(r'pollByUser', views.EncuestaPorUsuarioViewSet)
router.register(r'answerType', views.TipoRespuestaViewSet)
router.register(r'answers', views.RespuestaViewSet)
router.register(r'answerByUser', views.RespuestaPorUsuarioViewSet)
router.register(r'variablesType', views.TipoVariableViewSet)
router.register(r'variablesCategory', views.CategoriaVariableViewSet)
router.register(r'variables', views.VariableViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
]

settings.py:

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        #'rest_framework.permissions.IsAuthenticated',
    ),
    'EXCEPTION_HANDLER': 'adm.exceptions.core_exception_handler',
    'NON_FIELD_ERRORS_KEY': 'error',
    'DEFAULT_FILTER_BACKENDS': (
        'django_filters.rest_framework.DjangoFilterBackend',
    ),
}

但是,当我尝试使用网址

http://example.com/api/questions?name=someName&privated=True

我收到错误Page not found (404)

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

默认情况下,DefaultRouter创建的URL后面加上斜杠,请尝试以下URL:

http://example.com/api/questions/?name=someName&privated=True