Search code examples
pythondjangodjango-viewsdjango-urlsslug

Django empty request object


I have a custom UpdateView in Django that should manage the update process for a given object. I am currently using a slug (the identifier of the object rather than the pk), so I define the field "slug_field = 'cluster_id'" with the name of the slug as given in the url pattern:

**views.py**

class LeopUpdateView(edit_views.UpdateView):
    model = leop_models.Cluster
    slug_field = 'cluster_id'
    form_class = leop_forms.LeopForm
    template_name = 'staff/leop_update.html'
    success_url = django_resolvers.reverse_lazy('leop_management')

    def get_object(self, queryset=None):
        print '>>> self.request.GET = ' + misc.dict_2_string(
            self.request.GET.dict()
        )
        print '>>> self.request.POST = ' + misc.dict_2_string(
            self.request.POST.dict()
        )

        print '>>> cluster_id = ' + str(self.request.GET.get('cluster_id'))
        print '>>> user = ' + str(self.request.user)
        print '>>> '
        return self.model.objects.get(
            identifier=self.request.GET.get('cluster_id')
        )

**urls.py**

urlpatterns = urls.patterns(
    '',
    urls.url(
        r'^update/(?P<cluster_id>\w+)/$',
        decorators.login_required(leop_views.LeopUpdateView.as_view()),
        name='leop_update'
    ),
)

However, when I receive the request at the overriden "get_object" method within the view, the request.GET and request.POST objects are empty and the slug is not set; however, the username is set in the request (the following is the output that I get in the console):

>>> self.request.GET = {
}
>>> self.request.POST = {
}

>>> cluster_id = None
>>> user = satnet_admin

The URL that is generated and matched by the urlpattern is: "/leop/update/elana/"

What am I doing wrong? Should I access to the slug field in a different way?


Solution

  • request.GET contains querystring argument (?key=value&key1=value1) and request.POST cformontains post data (usually form)

    I think the mistake is in this line

    slug_field = 'cluster_id'
    

    should be

    slug_url_kwarg = 'cluster_id'
    

    slug_field should contain the name of the Model field (chars as per your url) to use as search.

    PS. you don't need to override get_object