Nuevakenia
Nuevakenia

Reputation: 75

get_context_data() missing 1 required positional argument: 'request'

i got a problem, when i try to redirect from a class based view without 'redirect' on def get_context_data, i got errors like 'str' object has no attribute 'user'. so i think the best way to solve this is adding request as a parameter on my get_context_data. but i get the title error.

UPDATE: as you told me that i have to use self.request, the error i get is this: 'str' object has no attribute 'user' i updated the return url to the method 'redirect' that redirect to the home of each user.

Here is my views:

@login_required
def redirect(request):
    '''Redirige al home de cada usuario luego de validar las credenciales'''
    user=request.user
    grupo=request.user.groups.values_list('name', flat=True).first()
    print("nombre grupo: ",grupo)
    if len(grupo)>0:
        redireccion='inventario:'+grupo.lower()+"-inventarios"
        return HttpResponseRedirect(reverse_lazy(redireccion))
    '''Anula sesión si postulante no se encuentra presente en clase'''
    do_logout(request)
    return render(request, "account/login.html")

class EstandarViews:
'''Vistas usuario Estandar'''

@staticmethod
def estandar_home(request):
    ''' Vista del home de estandar '''
    return render(request, "pages/editor-estandar/home.html")


class EditorEstandarView(LoginRequiredMixin, TemplateView):
template_name = "pages/editor-estandar/estandar.html"

    def get_context_data(self, request, **kwargs):
    user = self.request.user
    print("usuario: ", user)
    if not self.request.user.groups.filter(name="Editor-Estandar").exists():
        print("no puedes estar aqui")
        return redirect('redirect)

    context = super().get_context_data(**kwargs)
    context["sedes"] = Sede.objects.values("nombre", "id")
    context["recintos"] = Recinto.objects.values("id", "codigo", "nombre")
    context["carreras"] = Carrera.objects.values("id", "codigo", "nombre")
    return context

    editor_estandar_view = EditorEstandarView.as_view()

URLS.PY

app_name = "inventario"

general_urlpatterns = [
    path("", view=home_view, name="home"),
]

editor_estandar_urlpatterns = [
    path("estandar-inventario/", view=editor_estandar_view, name="editor-estandar-inventarios"),
]


editor_inventario_urlpatterns = [
    path("estandar-inventario/", view=editor_inventario_view, name="editor-inventario-inventarios"),
]

visualizador_urlpatterns = [
    path("estandar-inventario/", view=visualizador_inventario_view, name="visualizador-inventarios"),
]

urlpatterns = [
# path("", include(general_urlpatterns)),
    path("home/", include(general_urlpatterns)),
    path('', LoginView.as_view(redirect_authenticated_user=True,template_name="account/login.html"), name="login"),
    path('redirect/', redirect, name='redirect'),
    path("editor-estandar-inventarios/", include(editor_estandar_urlpatterns)),
    path("editor-inventario-inventarios/", include(editor_inventario_urlpatterns)),
    path("visualizador/", include(visualizador_urlpatterns)),
    path("mantenedor-productos/", view=mantenedor_productos_view, name="mantenedor-productos"),
    path("mantenedor-laboratorios/", view=mantenedor_recintos_view, name="mantenedor-recintos"),
    path("mantenedor-sedes/", view=mantenedor_sedes_view, name="mantenedor-sedes"),
    path("mantenedor-carreras/", view=mantenedor_carreras_view, name="mantenedor-carreras"),
    path("mantenedor-proveedor/", view=mantenedor_proveedores_view, name="mantenedor-proveedores"),
    path("mantenedor-usuario/", view=mantenedor_usuarios_view, name="mantenedor-usuarios"),
    path("estandar-inventario/", view=estandar_inventario_view, name="estandar-inventarios"),
    path("inventario/", view=inventario_view, name="inventario"),
    path("cotizaciones/", view=cotizaciones_view, name="cotizaciones"),
    path("cotizaciones-por-producto/", view=cotizaciones_por_producto_view, name="cotizaciones-por-producto"),
    path("ficha-presentacion/", view=ficha_presentacion_view, name="ficha-presentacion"),
    path("sugerencias/", view=sugerencias_view, name="sugerencias"),
    path("cumplimiento/", view=cumplimiento_estandar_view, name="cumplimiento"),
    path("faltantes/", view=faltantes_estandar_view, name="faltantes"),
    path("sugerencias-reporte/", view=sugerencias_estandar_view, name="repo_sugerencias"),

    # path("~update/", view=user_update_view, name="update"),
    # path("<str:username>/", view=user_detail_view, name="detail"),
]

Upvotes: 0

Views: 1407

Answers (2)

Shreyash mishra
Shreyash mishra

Reputation: 790

in your views remove request and then try

class EditorEstandarView(LoginRequiredMixin, TemplateView):
template_name = "pages/editor-estandar/estandar.html"

    def get_context_data(self, **kwargs):
    user = self.request.user
    print("usuario: ", user)
    if not self.request.user.groups.filter(name="Editor-Estandar").exists():
        print("no puedes estar aqui")
        return redirect('/home/')

    context = super().get_context_data(**kwargs)
    context["sedes"] = Sede.objects.values("nombre", "id")
    context["recintos"] = Recinto.objects.values("id", "codigo", "nombre")
    context["carreras"] = Carrera.objects.values("id", "codigo", "nombre")
    return context

    editor_estandar_view = EditorEstandarView.as_view()

get_context_data is a method, so the current instance is always passed to it implicitly by the interpreter which is usually named self.

and please do read about full documentation on views https://docs.djangoproject.com/en/3.2/topics/class-based-views/#:~:text=A%20view%20is%20a%20callable,by%20harnessing%20inheritance%20and%20mixins.

Upvotes: 0

Mojtaba Arezoomand
Mojtaba Arezoomand

Reputation: 2380

get_context_data method doesn't take request in its arguments.

It should be get_context_data(self, **kwargs)

If you want to have access to request inside get_context_data just use self.request

Upvotes: 2

Related Questions