Django UpdateView with ImageField attribute

I am having problems creating UpdateView Class.I managed to do createView and UpdateView without adding any form before adding imageField. But now I have an imageField that is creating the problem. Luckily I can do createView and it works fine.

Below is my code for CreateView

class CreatePostView(FormView):
    form_class = PostForm
    template_name = 'edit_post.html'

    def get_success_url(self):
        return reverse('post-list')
    def form_valid(self, form):
        form.save(commit=True)
        # messages.success(self.request, 'File uploaded!')
        return super(CreatePostView, self).form_valid(form)
    def get_context_data(self, **kwargs):
        context = super(CreatePostView, self).get_context_data(**kwargs)
        context['action'] = reverse('post-new')
        return context

      

However, I tried to do UpdateView (ViewForm). Below is my code:

class UpdatePostView(SingleObjectMixin,FormView):
model = Post
form_class = PostForm
tempate_name = 'edit_post.html'

# fields = ['title', 'description','content','published','upvote','downvote','image','thumbImage']

def get_success_url(self):
    return reverse('post-list')
def form_valid(self, form):
    form.save(commit=True)
    # messages.success(self.request, 'File uploaded!')
    return super(UpdatePostView, self).form_valid(form)

def get_context_data(self, **kwargs):
    context = super(UpdatePostView, self).get_context_data(**kwargs)
    context['action'] = reverse('post-edit',
                                kwargs={'pk': self.get_object().id})
    return context

      

When I try to run updateView it gives me the following error:

AttributeError in / posts / edit / 23 /

UpdatePostView has no attribute 'get_object'

Request Method: GET Request URL:    http: // localhost: 8000 / posts / edit / 23 / Django version: 1.8.2 Exception Type: AttributeError Value:

UpdatePostView has no attribute 'get_object'

Exception Location: /home/PostFunctions/mysite/post/views.py in get_context_data line 72 Python Executable: / usr / bin / python Python Version: 2.7.6

Below is my url.py:

#ex : /posts/edit/3/

url(r'^edit/(?P<pk>\d+)/$', post.views.UpdatePostView.as_view(),
    name='post-edit',),

      

+3


source to share


1 answer


I have a form to update a model using ImageField. I am actually extending ModelForm for my model (PostForm for me).

But my CustomUpdateView extends UpdateView, from a generic django view.

from django.views.generic.edit import UpdateView
from django.shortcuts import get_object_or_404


class CustomUpdateView(UpdateView):
    template_name = 'some_template.html'
    form_class = CustomModelForm
    success_url = '/some/url'

    def get_object(self): #and you have to override a get_object method
        return get_object_or_404(YourModel, id=self.request.GET.get('pk'))

      



You just need to define a get_object method and the update will update the object with the value in the form, but it needs to get the object you want to update.

get_object_or_404()

works like a function get()

on the model, so replace id with your field_id name.

Hope it helps

+2


source







All Articles