How to redirect on conditions with class based views in Django 1.3?

I know this is old, but I actually agree with Tommaso. The dispatch() method is what handles the request and returns the HTTP response. If you want to adjust the response of the view, thats the place to do it. Here are the docs on dispatch(). class VideosView(ListView): # use model manager queryset = Videos.on_site.all() … Read more

Django: Search form in Class Based ListView

I think your goal is trying to filter queryset based on form submission, if so, by using GET : class ProfileSearchView(ListView) template_name=”/your/template.html” model = Person def get_queryset(self): name = self.kwargs.get(‘name’, ”) object_list = self.model.objects.all() if name: object_list = object_list.filter(name__icontains=name) return object_list Then all you need to do is write a get method to render template … Read more

Is it okay to set instance variables in a Django class based view?

According to the source of django.views.generic.base.View.as_view: on django startup, as_view() returns a function view, which is not called on request, view() is called, it instantiates the class and calls dispatch() the class instance is thread safe According to the source of django.views.generic.base.View.__init__, the request object is out of scope at this point so you can’t … Read more

Accessing request.user in class based generic view CreateView in order to set FK field in Django

How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect. class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(PlaceFormView, self).dispatch(*args, **kwargs) def form_valid(self, form): obj = form.save(commit=False) obj.created_by = self.request.user obj.save() return http.HttpResponseRedirect(self.get_success_url())

success_message in DeleteView not shown

I think this issue in the Django issue tracker should answer your question. SuccessMessageMixin hooks to form_valid which is not present on DeleteView to push its message to the user. It also gives an alternative way which works for me: from django.views.generic.edit import DeleteView from django.core.urlresolvers import reverse_lazy from django.contrib import messages from .models import … Read more

What is context_object_name in django views?

If you do not provide “context_object_name”, your view may look like this: <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> But if you provide like {“context_object_name”: “publisher_list”}, then you can write view like: <ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> That means … Read more

How to process a form (via get or post) using class-based views?

The default behaviour of the FormView class is to display an unbound form for GET requests, and bind the form for POST (or PUT) requests. If the bound form is valid, then the form_valid method is called, which simply redirects to the success url (defined by the success_url attribute or the get_success_url method. This matches … Read more

tech