I'm trying to make an class TopicUpdateView(UpdateView) to edit topics, but I can get the data. I'm glad if someone could give me a hand on this

The TopicDeleteView works fine. Just the update view is not working.

App-View
> @login_required

    def new_topic(request, pk):
        board = get_object_or_404(Board, pk=pk)
        if request.method == 'POST':
            form = NewTopicForm(request.POST, request.FILES)
            if form.is_valid():
                topic = form.save(commit=False)
                topic.board = board
                topic.starter = request.user
                topic.save()
                Post.objects.create(
                    message=form.cleaned_data.get('message'),
                    topic=topic,
                    created_by=request.user
                )
                return redirect('topic_posts', pk=pk, topic_pk=topic.pk)
        else:
            form = NewTopicForm()
        return render(request, 'new_topic.html', {'board': board, 'form': form})

    class TopicListView(ListView):
        model = Topic
        context_object_name = 'topics'
        template_name = 'topics.html'
        paginate_by = 9

        def get_context_data(self, **kwargs):
            kwargs['board'] = self.board
            return super().get_context_data(**kwargs)

        def get_queryset(self):
            self.board = get_object_or_404(Board, pk=self.kwargs.get('pk'))
            queryset = self.board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 0)
            return queryset

    @method_decorator(login_required, name='dispatch')
    class TopicUpdateView(UpdateView):
        template_name = 'topic_update.html'
        model = Topic
        success_url = '/'

        def get_object(self):
            self.board.pk = get_object_or_404(Board, topics__pk=self.kwargs.get('topics_pk'), pk=self.kwargs.get('pk'))

        def get_queryset(self):
            queryset = super().get_queryset()
            return queryset.filter(starter=self.request.user)

    @method_decorator(login_required, name='dispatch')
    class TopicDeleteView(DeleteView):
        template_name = 'topic_delete.html'
        model = Topic
        success_url = '/'

        def get_object(self):
            pk_ = self.kwargs.get("pk")
            return get_object_or_404(Topic, pk=pk_)

        def get_queryset(self):
            queryset = super().get_queryset()
            return queryset.filter(starter=self.request.user)

App-URLs

urlpatterns = [
url(r’^topics/(?P\d+)/update/$’, views.TopicUpdateView.as_view(), name=‘topic_update’),
url(r’^topics/(?P\d+)/delete/$’, views.TopicDeleteView.as_view(), name=‘topic_delete’),
url(r’^boards/(?P\d+)/new/$’, views.new_topic, name=‘new_topic’),
]

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/topics/18/update/ Raised by: boards.views.TopicUpdateView No Board matches the given query. You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Check these examples https://www.programcreek.com/python/example/105313/django.views.generic.UpdateView

1 Like

Thanks, I’ll do some research Just to inform you that this project is the same as the @vitor series tutorials https://simpleisbetterthancomplex.com/series/beginners-guide/1.11/, with only a few implementations.

I also tried this code below, but it didn’t work either. I will keep looking and if I get something that works I post here.

@method_decorator(login_required, name='dispatch')
class TopicUpdateView(UpdateView):
    template_name = 'topic_update.html'
    form_class = NewTopicForm

def dispatch(self, request, *args, **kwargs):
    pk_ = self.kwargs.get('board_topic')
    topic = get_object_or_404(Topic, board_id=pk_, pk=self.kwargs.get('pk'))

    if not login_required(request.user, topic.subject):
        return redirect(reverse_lazy('subjects:home'))

    return super(TopicUpdateView, self).dispatch(request, *args, **kwargs)

def get_queryset(self):
    queryset = super().get_queryset()
    return queryset.filter(starter=self.request.user)

Other thing that I’m thinking is about URLs.

My delete view works fine with:

url(r'^topics/(?P<pk>\d+)/delete/$', views.TopicDeleteView.as_view(), name='topic_delete'),

So I thought the update view could be the same:

url(r'^topics/(?P<pk>\d+)/update/$', views.TopicUpdateView.as_view(), name='topic_update'),

but it’s not working like that it’s not find the data so It’s may have to be like this:

url(r'^boards/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/update/$', views.TopicUpdateView.as_view(), name='topic_update'),

I’m not sure about that. Some light please.

I tried this code:

@method_decorator(login_required, name='dispatch')
class TopicUpdateView(SuccessMessageMixin, UpdateView):
    model = Topic
    fields = ('picture_topic', 'subject',)
    template_name = 'topic_update.html'
    success_url = "/"
    success_message = "História editada com sucesso!"

    def get_object(self):
        pk_ = self.kwargs.get("pk")
        return get_object_or_404(Topic, pk=pk_)

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(starter=self.request.user)

and now I can get the data, but I just can change the subject. The picture_topic is not get updated. I might need a function to post image file or something like that. Don’t I?

I fond the error, it was in the template file. I forgot to enctype=“multipart/form-data”.
Now is working.



{% csrf_token %}
{% include ‘includes/form.html’ %}

Cancelar
Salvar alterações