Django Generic Date Views

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Django Generic Date Views

Gerald Brown-2
Greetings:

Tonight I discovered something at https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-date-based/ that I thought is just what I am looking for.  The only problem is I can't get them to work.

They are designed to present views by Year, Month, Week, Day, Today or specific Date.

If anyone has experience using them I would be interested in how it works for you.

Thanks.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/f50ac4bf-6da5-4850-b4a2-0ba9abea804f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Jason Johns
what have you tried so far?  What issues are you having with the documentation examples?

"can't get them to work" is really uninformative, 

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/3ccfa3d5-afc8-4a82-82e6-884da1f4b4ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Gerald Brown-2


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.

Guess I will have to dig more into the docs as I just discovered this tonight and "MAYBE" I didn't read all of the  docs thoroughly enough.

Thanks for your reply.  I have discovered that by discussing my problem I am then able to come up with a solution.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/08676940-3bd7-405d-b738-c7134a902dd9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Jason Johns
are you sure your url is in admin/visit?

path('today/', PaymentTodayArchiveView, name="today"),

404 says you have a url routing issue, not a view issue.

On Wednesday, August 8, 2018 at 8:42:06 AM UTC-4, 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 <a href="http://127.0.0.1:8000/admin/visit/today/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2F127.0.0.1%3A8000%2Fadmin%2Fvisit%2Ftoday%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzvJ9pPycBKbARqrDAGvdXpf8cdw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2F127.0.0.1%3A8000%2Fadmin%2Fvisit%2Ftoday%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzvJ9pPycBKbARqrDAGvdXpf8cdw&#39;;return true;">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.

Guess I will have to dig more into the docs as I just discovered this tonight and "MAYBE" I didn't read all of the  docs thoroughly enough.

Thanks for your reply.  I have discovered that by discussing my problem I am then able to come up with a solution.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/1c0ee6e7-5463-45e6-bbf8-7c49ace576cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Jason Johns
also, I note you don't set the queryset field in your view class.  you set it to `vi`.  that's probably an issue there

eg 

from django.views.generic.dates import TodayArchiveView

from myapp.models import Article

class ArticleTodayArchiveView(TodayArchiveView):
    queryset = Article.objects.all()  <-- 
    date_field = "pub_date"
    allow_future = True

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/067458f4-6bf2-4ada-a18f-a904d72fa81d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Gerald Brown-2
As far as I know both "vi" and "queryset" are variable names and can be called "anything"

On Thursday, August 9, 2018 at 12:08:07 AM UTC+8, Jason wrote:
also, I note you don't set the queryset field in your view class.  you set it to `vi`.  that's probably an issue there

eg 

from django.views.generic.dates import TodayArchiveView

from myapp.models import Article

class ArticleTodayArchiveView(TodayArchiveView):
    queryset = Article.objects.all()  <-- 
    date_field = "pub_date"
    allow_future = True

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/4f740c1b-2352-4d64-aeb9-ffbaa7d73db4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Michal Petrucha-3
In reply to this post by Gerald Brown-2
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 [hidden email].
To post to this group, send email to [hidden email].
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.

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Gerald Brown-2

One thing I discovered is that I had the line for the url.py file in my top level url.py.  I moved it to myapp/url.p but still same 404 error, however no longer get the "has no attribute" error

How do I call this view?  Tried from the browser "http://127.0.0.1:8000/today/" and I get "__init__() takes 1 positional argument but 2 were given" error.

Tried "http://127.0.0.1:8000/visit/today/" and I get 404 error.

In the line "queryset = Visit.objects.all()" won't that select all of the records in the database when I only want the records for today which I then use to create a reportlab pdf file? Prior to trying to use these generic date views I was able to generate the file but only for the current date.

Are there any more suggestions?

TIA...

On Thursday, 09 August, 2018 06:14 PM, Michal Petrucha wrote:
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): This is exactly as it is in my code and also in the docs except I changed Article to Payment.
    vi = Visit.objects.all() I changed vi to queryset and it made NO difference.
    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 [hidden email].
To post to this group, send email to [hidden email].
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/9206a581-d513-0397-77e9-20cfe8ef1524%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Michal Petrucha-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Thu, Aug 09, 2018 at 07:54:29PM +0800, Gerald Brown wrote:
> One thing I discovered is that I had the line for the url.py file in my top
> level url.py.  I moved it to myapp/url.p but still same 404 error, however
> no longer get the "has no attribute" error
>
> How do I call this view?  Tried from the browser
> "http://127.0.0.1:8000/today/" and I get "__init__() takes 1 positional
> argument but 2 were given" error.
>
> Tried "http://127.0.0.1:8000/visit/today/" and I get 404 error.

I'm afraid you're going to need to provide more detail – how about you
post the full traceback that you're getting?

Also, the question which URL is correct is hard to answer unless you
post all the relevant URL configs.

> In the line "queryset = Visit.objects.all()" won't that select all of the
> records in the database when I only want the records for today which I then
> use to create a reportlab pdf file? Prior to trying to use these generic
> date views I was able to generate the file but only for the current date.

You lost me at the part about reportlab, but ``Visit.objects.all()``
does not actually fetch anything from the database yet. It only
returns a QuerySet that can be processed further (you can add more
filter expressions, set ordering, and whatnot), and only fetches data
if you actually evaluate it (for example by iterating over it).

So you'd typically just set that as the ``queryset`` attribute on a
generic view, and then every time the view is executed, it will take
that unevaluated queryset, add all the necessary filters to only
select items for today, and then fetch those.

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJbbC7JAAoJEHA7T/IPM/klCh8P/j9iQowdzLeuTNpz5sQpahiU
KhiePgASItMSnKhwFN5Qv++5eV21dl7fViBFPfkQtQ6hNxJXewX8M7kElvr9dmh3
/8GxmkzrW+yuqBgagzNXHOtMi3zvmrFxkiwR6vKcMoTJUR8jsMFnvvkpeCEqFJas
vj7kCbNPJ26GuIWCvFkmtBf2wpd18L49EuPly88nqt+R5tLNNzZ9h9hHzLB8Jr7f
Lq6m397Rxz9oAk/tXw7XFiOB/tIm3jG77XUGYzhv424qEo9a5R1WFOL+s+MoOkdi
wxdb/WEzk9gxf5+v9HtDI3eRYkqaE1WWXQOe7cGTWlDhW/FPXmuI8dSqQOKYbDCL
bNEzcq/bXpIvy0Ot5Ja4RB7OKXBOyd7QElfkF5TTYCyc9JEH8IavwhAtJlL1S6J9
mkzcUjsv9nTqjPqSBBd0bLrNtm5VCyz731w4KvrSHPVTMPJkplkYDWsf1kL6XPEZ
yybYAlMmNF2RA0MelSAjQw3FNAGK2CEXN0Pq+wja3dZs16zv+Y2vhEUhTgFgVmld
KxGmcoULk05UAsbRqboQMBDubfatUxLuGY4MNKARI39T5vLmuzbwkdlv3T0viFVu
OAKIliDdgYP7yUb3uZI1DK1UhgZKxjHTbLt/Es7yGkXFMRYdvJ7pv6TQ69prjrVp
8B5S03RXDeThmAEffLjr
=erht
-----END PGP SIGNATURE-----

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/20180809120842.GJ1181%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Django Generic Date Views

Michal Petrucha-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi Gerald,

Couple of points:
- - please try to stick to the mailing list when posting follow-up
  questions or materials rather than replying off-list; this makes it
  possible for other people to jump in and offer their advice (and
  also doesn't send the message that you expect the one person who
  tried to help you to personally coach you through your issue), and
- - rather than attaching your code in a tarball, posting the relevant
  parts of your code in-line typically makes it easier for people to
  help you – I can just look at the email and directly see the code,
  rather than having to fetch the attachment, unpack it, and go
  through all that hassle. (And don't even get me started on the trend
  of posting screenshots of stack traces or even code; some of us are
  reading our mail in text-mode terminals. Get off my lawn, kids! :P )

With that out of the way, let's get back to your questions.

On Thu, Aug 09, 2018 at 09:15:36PM +0800, Gerald Brown wrote:

> Thanks for your info.
>
> I am attaching my views.py, url.py and index.html.  The index.html creates a
> menu on the main admin page.  I have included some comments there about the
> strange happenings that are going on.  I am not able to include any trace
> info because I have gone back to my original program from before these
> generic date views.
>
> The URLS1.py file is from my top level and URL.py is from my visit app. In
> the views.py file most of the code is formatting the reportlab pdf file with
> the data from the queryset assigned to various locations in the pdf.

urls.py::

    from django.contrib import admin
    from django.urls import include, path

    #from visit import views # as visit_views
    from visit.views import PaymentTodayArchiveView

    urlpatterns = [
            path('today/', PaymentTodayArchiveView.as_view, name="today"),
            ]

urls1.py::

    from django.contrib.admin.sites import AdminSite
    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import include, url
    #from django.views.generic.dates import DateDetailView

    from patient import views as patient_views
    from visit import views as visit_views

    admin.site.site_header = 'MEDREC Administration'

    class DashboardSite(AdminSite):
            def get_urls(self):
                    urls = super(DashboardSite, self).get_urls()
                    custom_urls = [
                            path('^$', self.admin_view(HomeView.as_view()), name='admin'),
                            ]
                    del urls[2]
                    return custom_urls + urls

    urlpatterns = [
            path('admin/patient/', patient_views.prescription_view),
           
            path('admin/visit/', visit_views.daily_payment_view),
           
            path('admin/', admin.site.urls),
            ]

views.py::

    @staff_member_required
    # ~ class PaymentTodayArchiveView(TodayArchiveView):
            # ~ queryset = Visit.objects.all()
            # ~ date_field = "visit_date"

So, starting from the beginning – the correct way to reference a
generic class-based view in an URL config is to use
MyViewClass.as_view() – note the parentheses. You can see that in
action in the custom AdminSite that you wrote. The parentheses are
missing in your urls.py.

Second, I don't see urls.py being referenced anywhere in urls1.py,
which you say is the root URL config.

Third, your usage of the ``staff_member_required`` decorator is not
correct – I recommend reading the section of the docs which covers
decorating class-based views [1] to learn how to do that. Not to
mention that you left the decorator in place, while you commented out
the view itself, which means the decorator would get applied to the
next definition following it.

I'm not quite sure how I can help you here, since you don't actually
have any code in place that would use the GCBV...

> It seems like all tutorials I try to follow they are only half complete and
> they leave out a lot of stuff..like how to access these views.  Maybe that
> is something I should know, but don't.
>
> As it is getting late here I will be taking another look at this tomorrow.

Michal

[1]: https://docs.djangoproject.com/en/2.1/topics/class-based-views/intro/#decorating-class-based-views
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJbbXEsAAoJEHA7T/IPM/kl410QAM6IM+hMFu7+0n41MysAJebW
qk7LE/YaO5lWeBFtbBnzKuQTQ3cIV102Q2e/YBYLOwh0lmlefD01iqR+pOJLdWAu
/Fbr6uqG5Nlem7hFoc0g6QvQLADej4AqsdGlQ80de12gb66PHvHaAkqV726cPodI
13V8qXsfZsj4ix0XsAZmPEmYKW6PoL4TV2J9FFQYLr8ZqlzD3Y+QkEojtnspfVod
ayK7nH3DcABEU1R0SGx+e/7ifwCFXkk3KjIC4ESRGH7pEaozJZu9onb7YP32Yejd
B9sUA1+7A5Cj1IQvlKc8cDayZ3qLRTV7tOG3oUvy/adiRFoBU1MgTodNJccT5Rrx
+WdEgYeGsmd5rrBZAio0S6fhGuCClRMQiknmFjX4YJ2pN4aBCZo7R8RlmkCkODmw
JgKecTujBLC8ax2EDYH887W/qALIsyk5+J1tOZlDblg3+cYMm8LCGQLUCpz5hARb
z35ZXRjb8f7WY6KN8JXDPvL9oNvypOV+AxmJP6NsYAPufA0RtTvkrTrPdXr0a5VK
YCB1E4VK5vBLvjf+qrrOu7mCYBG1qGiPXLXGOFd6uQCZDAJvqOAfEroC7PTxRlW0
min7N+lU5An2vWh8snDQ1P4oOZYvm8YGLeC9bdFNaWl79cpvG4PLwQfWb/R8LJum
MdtV/G8XgnwJWWaVG/ya
=rSMt
-----END PGP SIGNATURE-----

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/20180810110413.GL1181%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.