简体   繁体   中英

Django class-based view

django

i'm new to django. as per in doc i'm creating a django app. while o'm using class-based view I got a error. I didn't understand much about class based view. Can any one explain what's the different between normal view. Its the same poll app in the documentation page of Django

Here's my code:

class DetailView(generic.DetailView):
    model = Poll
    template_name = 'polls/details.html'
    def get_queryset(self):

    def detail(request, poll_id):
        try:
            poll = Poll.objects.get(pk=poll_id)
        except Poll.DoesNotExist:
            raise Http404
        return render(request, 'polls/details.html', {'poll': poll})

*********************Error ********************
TypeError at /polls/2/results/
as_view() takes exactly 1 argument (3 given)
Request Method: GET
Request URL:    <app-path>/polls/2/results/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:    
as_view() takes exactly 1 argument (3 given)    

*****the url***
 url(r'^(?P<pk>\d+)/$', views.DetailView.as_view, name='detail')

as_view should be called, not referenced, according to the docs , your url should look like:

url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail')

Note the usage of parenthesis.

Also, you should rather call your class PollDetailView to avoid confusion for code readers.

Also, the detail() method you have defined will not be called at all. So you shouldn't define it at all. Also, leave the get_queryset() method alone for the moment, try to get the basic View to work first.

When you use url for the CBV ensure that you can not use the reference of the of the url filed, so first you need to add change the as_view to as_view() .

And you can use DetailView like this.,

   class PollDetail(DetailView):
   model=Book

   def get_context_data(self,*args,**kwargs):                    
       context=super(PollDetail,self).get_context_data(*args,**kwargs)
       print(context) #It will give you the data in your terminal
       return context

And for accessing data you need to use {{object}},

and if you want to access other fields at that time use like this {{object.fieldname}}

In CBV the template name is automatically named as per class name so you don't need to give it.

Note:Don't give class name same as DetailView,In future you will confuse.

Since you are not modifying the view functionality, to use this generic view, you can simply do this:

in your urls.py (along with other stuff):

from django.views.generic.detail import DetailView
from poll.models import Poll

url(r'^(?P<pk>\d+)/$',
    DetailView.as_view(model=Poll,
                       template_name='polls/details.html'), name='detail')

Then in poll/details.html , you just need:

{{ object }}

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