awwester
awwester

Reputation: 10162

Django not reading database values correctly

I'm quite stumped on this one. I'm working with a Task Tracking system and after adding or updating task objects they are not being refreshed on the client-facing website, but in the admin site they are there.

The problem resides in my TaskCreateView, but I can't figure out why. I know this because I've created a simple test TaskListView and it displays the updated values. I need the TaskCreateView to both display task objects as well as create a new task object.

Here is my code, hopefully someone has an idea, because I've ran out.

#urls.py
urlpatterns = patterns('',
    url(r'^$',  TaskCreateView.as_view(), name='task-create'),
    ...
)

#views.py
class TaskCreateView(MultipleObjectMixin, CreateView):
    '''
    base create view for tasks
    '''
    model = Task
    form_class = TaskCreateForm
    template_name = "flowtask/content/tasks.html"
    object_list = Task.objects.all()
    success_url = reverse_lazy('task-create')
    load_modal = "createTaskModal"

    def form_invalid(self, form, **kwargs):
        #need to add in the error status and modal to the context_data
        context = self.get_context_data(**kwargs)
        context['status'] = 'error'
        context['load_modal'] = self.load_modal
        context['form'] = form
        # return super(TaskCreateView, self).form_invalid(form)
        return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super(TaskCreateView, self).get_context_data(**kwargs)
        context['create_form'] = self.get_form(self.form_class)
        return context

I am using Django 1.6 and python 3.3.

Upvotes: 0

Views: 123

Answers (2)

randlet
randlet

Reputation: 3706

Remove the object_list from your class definition and move it to your get_context_data method:

def get_context_data(self, **kwargs):
    context = super(TaskCreateView, self).get_context_data(**kwargs)
    context['create_form'] = self.get_form(self.form_class)
    context['object_list'] = Task.objects.all()
    return context

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599610

You shouldn't be defining object_list at the class level, but queryset or model.

Upvotes: 0

Related Questions