У меня возникли проблемы с созданием UpdateView Class.I удалось сделать createView и UpdateView без добавления какой-либо формы перед добавлением imageField. Но теперь у меня есть imageField, который создает проблему. К счастью, я могу сделать createView и его работу отлично.Django UpdateView с атрибутом ImageField
Ниже мой код 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
Однако я пытался сделать UpdateView (ViewForm). Ниже мой код:
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
Когда я пытаюсь запустить updateView, его дает мне следующую ошибку:
AttributeError at /posts/edit/23/
'UpdatePostView' object has no attribute 'get_object'
Request Method: GET Request URL: http://localhost:8000/posts/edit/23/ Django Version: 1.8.2 Exception Type: AttributeError Exception Value:
'UpdatePostView' object 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
Ниже мой url.py:
#ex : /posts/edit/3/
url(r'^edit/(?P<pk>\d+)/$', post.views.UpdatePostView.as_view(),
name='post-edit',),
Где был установлен 'get_object()'? – Matt
не могли бы вы помочь мне разобраться в этом? – user3487775
Ну, что stacktrace говорит вам, что ошибка возникает, потому что вы вызвали метод ('get_object'), который нигде не определен. То есть, если вы не опубликовали весь свой код? – Matt