Search code examples
pythondjangodetailview

Combine independent form with DetailView


I want to display DetailView and independent form to send API request to the other website server. I made views.py but only i get is empty page. I'm trying to figure out how to adjust it for over past fiew days and still don't have any clue how to do this. Hope you will help me with this

views.py

class DetailPostDisplay(DetailView):
    model = EveryPost
    template_name = 'post/detailpost.html'

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

class DictWindowForm(SingleObjectMixin, FormView):
    template_name = 'post/detailpost.html'
    form_class = DictForm
    model = EveryPost

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('detailpost', kwargs={'slug': self.object.slug})

class DetailPostList(View):
    def get(self, request, *args, **kwargs):
        view = DetailPostDisplay.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = DictWindowForm.as_view()
        return view(request, *args, **kwargs)

HTML I'm not sure whether action should be empty or include url DetailPostDisplay(require to pass slug, which i don't have how to get)

        <form method="POST" action="">
        {% csrf_token %}
        {{ form }}
        <input type="submit" class="btn btn-dark float-right mt-2" value="Tłumacz">
        </form>

urls.py

from django.urls import path
from . import views
from .views import PostListPl, PostListRu, DetailPostDisplay

urlpatterns = [
    path('', PostListPl.as_view(), name='index_pl'),
    path('ru/', PostListRu.as_view(), name='index_ru'),
    path('about/', views.about, name='about'),
    path('<slug:slug>/', DetailPostDisplay.as_view(), name='detailpost'),
]

Solution

  • For the future generations, i mixed and overthinked it. If you want to just put form into DetailView, create def post and put logic there. Code below:

    views.py

    class DetailPostDisplay(DetailView):
        model = EveryPost
        template_name = 'post/detailpost.html'
    
        def get_context_data(self, **kwargs):
            context = super(DetailPostDisplay, self).get_context_data(**kwargs)
            context['form'] = DictForm
            return context
    
        def post(self, request, *args, **kwargs):
            form = DictForm(request.POST)
            if form.is_valid():
                self.object = self.get_object()
    

    And later code to pass variables into template from form

                context = super(DetailPostDisplay, self).get_context_data(**kwargs)
                context['form'] = DictForm
                context['word'] = request.POST.get('word')
                return self.render_to_response(context=context)