Home > database >  how can i post form object, correctly?
how can i post form object, correctly?

Time:01-08

i am practicing CBV , so i thought to check if i can override methodes, well one of biggest problems is that idk how to use data(like data just submitted ), i wrote this code for a DetailView so i could see post and comments under it:

class ArtDetailView(FormView, DetailView):
    model = Art
    form_class = CommentForm

    def get_context_data(self, **kwargs):
        context = super(ArtDetailView, self).get_context_data(**kwargs)
        context['time'] = timezone.now()
        context['form'] = self.get_form()
        return context

    def form_valid(self, form):
        form.instance.writer = self.request.user
        form.instance.text = self.post
        #form.instance.art = Art.objects.get(id=self.pk)
        form.save()
        return super().form_valid(form)


    def get_success_url(self) -> str:
        return reverse('pages:art_detail', args=(self.kwargs['pk'],))

forms.py:

from django import forms

from .models import Art, Comment

class CommentForm(forms.ModelForm):
    class Meta():
        model = Comment
        fields = ['text','art']

but when i post something it is in this shape:enter image description here

  • Related