简体   繁体   中英

How to convert class based detail view in function based view?

I am a learner I am learning django by doing an accounting application using django.

I was wondering how to convert a critical class based detail view to function based view

This is my code which I want to convert:

class ledger1DetailView(LoginRequiredMixin,DetailView):
    context_object_name = 'ledger1_details'
    model = ledger1
    template_name = 'accounting_double_entry/ledger1_details.html'

    def get_object(self):
        pk1 = self.kwargs['pk1']
        pk2 = self.kwargs['pk2']
        get_object_or_404(company, pk=pk1)
        ledger = get_object_or_404(ledger1, pk=pk2)
        return ledger

    def get_context_data(self, **kwargs):
        context = super(ledger1DetailView, self).get_context_data(**kwargs) 
        context['journal_list']    = journal.objects.all()
        context['company_list']    = company.objects.all()
        journal_details            = get_object_or_404(journal, pk=self.kwargs['pk'])
        context['Debitcount']      = journal_details.debitsum()
        context['company_list']    = journal_details.creditsum()
        company_details            = get_object_or_404(company, pk=self.kwargs['pk1'])
        context['company_details'] = company_details
    return context

This is my urls.py:

url(r'^company/(?P<pk1>\d+)/ledgerdetail/(?P<pk2>\d+)/$',views.ledger1DetailView.as_view(),name='ledgerdetail'),

So you can understand what pk1 and pk2 is...And pk is the id for the journal objects...I want to pass some function which I have performed in the journal model in ledger1detailview...

If anyone know how to do it...plz help

Thank you

First, you need to write the function signature. Since it's a function not a class, the recommended name changes to ledger1_detail_view . The login_required decorator does the same job as LoginRequiredMixin .

Then, you need to fetch the object. This is similar to the code in get_object . Note that you don't have self.kwargs any more, you have pk1 and pk2 from the function signature.

Then you need to build up the context dictionary. This will be similar to your get_context_data method.

Finally you can use the render shortcut to render the template.

Putting it together, the structure of your view will be:

from django.contrib.auth.decorators import login_required

@login_required
def ledger1_detail_view(request, pk1, pk2):

    get_object_or_404(company, pk=pk1)
    ledger = get_object_or_404(ledger1, pk=pk2)

    context = {}
    context['ledger1_details'] = ledger
    # Add other items to the context
    ...

    return render(request, 'accounting_double_entry/ledger1_details.html', context)

Remember to change your url pattern to use the new view:

url(r'^company/(?P<pk1>\d+)/ledgerdetail/(?P<pk2>\d+)/$', views. ledger1_detail_view, name='ledgerdetail'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM