Maybe I'm thinking about this wrong

28 views
Skip to first unread message

Dom Armstrong

unread,
Apr 10, 2012, 12:40:27 PM4/10/12
to django...@googlegroups.com
Im not sure if I am going about this the wrong way.

Trying to learn class based views *sigh* currently seeming more complicated and less human readable with no less code currently but I expect most people feel that at first.

class HomePage(ListView, BaseFormView):
    template_name = 'index.html'
    queryset = Notes.objects.filter(user=1)
    context_object_name = 'notes'
    form_class = LoginForm
    success_url = '/notes/'

    def get_context_date(self, **kwargs):
        context = super(HomePage, self).get_context_data(**kwargs)
        context['loginform'] = LoginForm
        for kwarg in kwargs:
            context[kwarg] = kwargs[kwarg]
        return context

    def form_valid(self, form, **kwargs):
        """do some form stuff"""
        if "errors with login":
            kwargs['error'] = 'account not active'
            kwargs['error_type2'] = 'check details'
        return self.get_context_data(**kwargs)

what I want to do really is return the errors back to the start as extra context, if it was a function I would understand where to send the kwargs.. currently I get errors regarding missing the original context that would be passed to get_context_data since it is only getting my kwargs and not the original. 

Do I need to be adding 

if 'error': 
    error=error
    return render_to_response('template' {'error':error, 'form':form, 'queryset':queryset})

as well as the original context I need, seems messy.

hari jayaram

unread,
Apr 12, 2012, 10:53:37 AM4/12/12
to django...@googlegroups.com
Hi Don,

Wondering why you dont inherit from just Formview..and define the
success URL to a separate class based ListView. It splits it up into
two class based views with the Formview taking care of returning the
errors on failure. On success it hands off to your Listview based
class that knows which queryset to get and display the response.

I am new to class based views as well , but I find they are a lot
easier to comprehend looking at the source code for the views
(https://github.com/django/django/blob/master/django/views/generic/edit.py
and https://github.com/django/django/blob/master/django/views/generic/list.py).

Or maybe you could just inherit from Formview instead of BaseFormView
and ovveride the "get" to handoff to the form and return the response.

Hope this helps..

Hari


BaseFormView implements the same behavior as FormView, but doesn't
include the TemplateResponseMixin.

Mixins

django.views.generic.edit.FormMixin
django.views.generic.edit.ProcessFormView

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/f1cCwSjvzFsJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages