简体   繁体   中英

How to use date based generic view in django?

I want to display objects based on the datetime information they have. My model looks like this:

models.py

class Person(models.Model):
    name = models.CharField(max_length=50)

class Registration(models.Model):
    name = models.ForeignKey(Person)
    reg_id = models.IntegerField()
    reg_date = models.DateTimeField(db_index=True)
    reg_type = models.CharField(max_length=50)
    description = models.CharField(max_length-200)

urls.py

from app.models import Registration
from dajngo.views.generic.date_based import archive_day

queryset = registration.objects.all()


urlpatterns = patterns('',
    url(r'^$', 'ui.views.index'),
    url(r'^registrations/(?P<person_name>)\w+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', archive_day, {'year': year, 'month': month, 'day': day, 'queryset': queryset, 'date_field': reg_date})
)

)

My Intent:

I want to display all the registrations in a person's name, in a particular day. Yes, one person can have more than one registration.

I know I am wrong:

I read the docs, but it isn't much handy for a newbie like me. And while I figured out that their are these required parameters and some optional which need to be sent as a key: value pair. But what I don't seem to understand is that how can I reference the year , month , day , reg_time into the key: value pair of the dictionary. I mean to say, that obviously it wont recognize what year is right? So from where can I get the reference? Do I sound confused? I really am. Some help in clearing it will be much appreciated.

Update:

There's one thing I realize. The date is really going to be wrapped inside the request . Because the user is going to request that URL right? So probably I need to extract the values of year month and day from the request body.So how can this be done?

Well it depends, if it's CBV then:

self.kwargs['year']

if it's the view function then:

def my_view(request, year, month, day):
    ...

https://docs.djangoproject.com/en/1.5/topics/http/urls/#notes-on-capturing-text-in-urls

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