On Wed, Aug 08, 2018 at 05:42:06AM -0700, Gerald Brown wrote:
>
>
> On Wednesday, August 8, 2018 at 8:15:21 PM UTC+8, Jason wrote:
> >
> > what have you tried so far? What issues are you having with the
> > documentation examples?
> >
> > "can't get them to work" is really uninformative, RIGHT.
> >
>
> What I tried was the TodayArchiveView by following the docs. When I enter
> http://127.0.0.1:8000/admin/visit/today/ in my browser I get a 404 error
>
> The code in my views.py file is:
> class PaymentTodayArchiveView(TodayArchiveView):
> vi = Visit.objects.all()
> date_field = "visit_date"
>
> The code in my URLS.py file is:
> path('today/', PaymentTodayArchiveView, name="today"),
>
> In the docs is says ".as_view()" should be added to
> PaymentTodayArchiveView. When I add that I get "AttributeError: 'function'
> object has no attribute 'as_view'" error.
This exception indicates that you didn't define
PaymentTodayArchiveView as a class, but rather as a function (which is
not consistent with the code you pasted above). As long as
PaymentTodayArchiveView is defined as a subclass of TodayArchiveView,
like in your snippet, you should be able to call ``as_view`` on it.
So yes, using ``PaymentTodayArchiveView.as_view()`` in your URL config
would be correct, as long as the definition of PaymentTodayArchiveView
is correct.
And regarding the name (``queryset`` vs. ``vi``) – the point of using
generic views is that the default implementation of the view supplies
most functionality. Most of the time, you're able to configure what
objects the view operates on by setting certain class attributes in
your customized subclasses. In this case, all of the generic date
views by default look at the ``queryset`` class attribute when they
want to read objects from the database, not ``vi``, which is why you'd
typically use that name in your subclass.
Good luck,
Michal
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20180809101435.GI1181%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment