osdir.com
mailing list archive F.A.Q. -since 2001!



Subject: Newbie to Novice – Using Admin DateTime
Picker in your Form - msg#01717

List: DjangoUsers

Mail Archive Navigation:
by Date: Prev Next Date Index by Thread: Prev Next Thread Index

I've noticed many people new to Django (and sometimes also new to Python) often post the same/similar questions in various forums. How to I get something to work and/or do you have an example for X. As I've also experienced this problem I've decided to post my little solution. Its not the only way to do this, just the way I did it. Hopefully this will provide the answer people are searching for or at least get them started. I guess it will also serve as a quick code review by those so inclined to comment.


You've probably heard of DRY but I think something is missing. There should also be a Don't Repeat Other's Work (DROW) when they already solved the problem. Thus another motivation to post this.  On a side note there is a bit of debate out there about using the Django AdminDateWidget verse other solutions (Jquery, etc). Your decision to make but why increase dependencies when you don't have to.


As I'm still learning Django everything in the code below may not be required but I'm listing it anyway. You may need to modify to your particular environment.  As I don't have a blog to post this I'm sending it to this group. I'm open to any suggestions for a better place to post this.


For this example I will focus on adding a date picker for date of birth (dob) and a date time picker for sponsor sign date (sponsor_sign_date). Key items are in Bold.


Another reference to adding the AdminDateTime widget is here: http://stackoverflow.com/questions/38601/using-django-time-date-widgets-in-custom-form




---------------

My top level (django-admin.py startproject izbo) directory Structure:

mylogin@LX-D620:~/dev/izbo$ ll

drwxrwxr-x 2 mylogin mylogin 4096 2009-03-17 10:52 account/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-17 10:53 add/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-18 04:34 adjudicator/

drwxrwxr-x 2 mylogin mylogin 4096 2009-03-18 09:43 application/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-18 10:06 contract/

drwxrwxrwx 2 mylogin mylogin 4096 2009-03-18 09:49 DB/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-17 10:51 employer/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-18 04:34 entity/

-rw-r--r-- 1 mylogin mylogin 207 2009-03-08 04:54 exclude

drwxrwxrwx 2 mylogin mylogin 4096 2009-03-18 10:06 gzbo/

-rw-r--r-- 1 mylogin mylogin 0 2009-01-06 04:55 __init__.py

-rw-r--r-- 1 mylogin mylogin 546 2009-01-06 04:55 manage.py

drwxrwxrwx 5 mylogin mylogin 4096 2009-02-08 12:35 media/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-17 10:53 member/

drwxr-xr-x 2 mylogin mylogin 4096 2009-03-17 10:52 note/

drwxr-xr-x 2 mylogin mylogin 4096 2009-02-20 12:47 search/

-rw-r--r-- 1 mylogin mylogin 4192 2009-03-05 23:39 settings.py

drwxrwxrwx12 mylogin mylogin 4096 2009-03-16 11:48 templates/

-rw-r--r-- 1 mylogin mylogin 2118 2009-03-16 11:16 urls.py


--------------

Media directory Listing:

mylogin@LX-D620:~/dev/izbo/media$ ll

total 12

drwxr-xr-x 5 mylogin mylogin 4096 2009-03-18 10:56 admin/

drwxrwxrwx 2 mylogin mylogin 4096 2009-02-07 15:45 css/

drwxrwxrwx 2 mylogin mylogin 4096 2009-01-27 10:07 images/

lrwxrwxrwx 1 mylogin mylogin 36 2009-03-18 11:07 img -> /home/mylogin/dev/izbo/media/admin/img/


* Note: admin/ is 'cp -r' of directory /usr/lib/python2.5/site-packages/django/contrib/admin/media.  Then I linked the img directory.



------------

In my “settings.py”


import os.path

PROJECT_DIR = os.path.dirname(__file__)


MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

MEDIA_URL = 'http://www.izbo.com/media/'

ADMIN_MEDIA_PREFIX = '/media/admin/'

TEMPLATE_DIRS = (

   os.path.join(PROJECT_DIR, 'templates'),

   ...

)


----------------

In my top level “urls.py” file

from django.contrib import admin


urlpatterns = patterns('',

   # Uncomment the admin/doc line below and add 'django.contrib.admindocs'

   # to INSTALLED_APPS to enable admin documentation:

   (r'^admin/doc/', include('django.contrib.admindocs.urls')),


   # Add this to get widgets.AdminDateWidget() working for non is_staff, is_superuser

   # This must be placed before (r'^admin/(.*)', admin.site.root), as that gobals up everything

   (r'^admin/jsi18n/$', 'django.views.i18n._javascript__catalog'),


   # Uncomment the next line to enable the admin:

   (r'^admin/(.*)', admin.site.root),


   # IZBO related URLs

   (r'^$', splash),

   .....

)



-------------

In “gzbo/models.py” I have my class


#

# Application

#

class App (Record) :

# Personal Information

dob = DateField(null=True,

    verbose_name=_('Date of Birth'),

    help_text=_('Enter Date of Birth Format: CCYY-MM-DD'))

sponsor_sign_date = DateTimeField(null=True,

    blank=True,

    verbose_name=_('Date Sponsor Electronically Signed'))


--------------

In “application/” I have a forms.py that describes the fields I want to have a date time picker

So in application/forms.py


#

# DJANGO Libraries

#

from django.forms import ModelForm

from django import forms

from django.contrib.admin import widgets


class AppForm(ModelForm) :

# Personal Information

dob = forms.DateField(

    widget = widgets.AdminDateWidget)

sponsor_sign_date = forms.DateTimeField ( required = False,

    widget = widgets.AdminSplitDateTime)

.... # other fields

class Meta :

   model = App


----------------

In “templates/application/appCreate.html” I have the follow. The src="" is referencing directory ~/dev/izbo/media


{% extends 'base.html' %}


{% block css %}

<link href="" rel="stylesheet" type="text/css" media="screen" />

<script type="text/_javascript_" src=""

<script type="text/_javascript_" src=""

<script type="text/_javascript_" src=""

{{ form.media }}

{% endblock css %}


{% block title %}

IZBO - Application

{% endblock title %}

...

{{ form.as_p }}

...


Key scripts for date picker seem to be:

/admin/jsi18n/

/media/admin/js/core.js

and {{ form.media }} which adds the two lines below (calendar.js and DateTimeShortcuts.js)

/media/admin/js/calendar.js

/media/admin/js/admin/DateTimeShortcuts.js


Summary:

  • Update your settings.py file MEDIA_ROOT, MEDIA_URL and ADMIN_MEDIA_PREFIX to point to where you plan to keep the images and js for the picker. TEMPLATE_DIRS points to where you keep your template files.

  • Create a ModelForm based upon the class containing the date and/or datetime field/s

  • Reference the correct widgets for the date and/or datetime fields. I.E. widget = widgets.AdminDateWidget or widget = widgets.AdminSplitDateTime

  • In your template file reference the correct _javascript_ stuff

  • Update your urls.py file to intercept the jsi18n call and send the correct version.


  • Have Fun, Stay Safe and Keep the Django Faith
























    --~--~---------~--~----~------------~-------~--~----~
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To post to this group, send email to django-users@xxxxxxxxxxxxxxxx
    To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en
    -~----------~----~----~----~------~----~------~--~---

    Thread at a glance:

    Previous Message by Date:

    Getting logged in username in admin

    Dear all, How do I get the current logged in user to be used and inserted to a model? from django.db import models from django.contrib.auth.models import User class News(models.Model): creator = models.ForeignKey(User) I want the creator field to be the current logged in user. Thank you very much. -- If you can't believe in God the chances are your God is too small. Read my blog: http://joshuajava.wordpress.com/ Follow me on twitter: http://twitter.com/jpartogi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

    Next Message by Date:

    Re: How do I 'redirect' in my test code after a 301 response?

    On Thu, Mar 19, 2009 at 9:22 AM, Johan <djjordaan@xxxxxxxxx> wrote: Hiya, ÂI trying to write test code for my views. I am using the following approach. >>>from django.test.client import Clien >>>c = Client() >>>c.get('/courses/system/home').status_code 301 ÂThe question is now: How do I 'redirect' in my code? I would expect something along the lines of: >>> response = c.get('/courses/system/home') >>> if response.status_code == 301: >>>  actual_response = c.get(response.ulr) ÂAny ideas? Help? Pointers to documentation? Thanks In the Django development version you can do this: http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client see the bits on follow = True. On 1.0 you'll need to do it manually. Alex-- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire"The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

    Previous Message by Thread:

    Getting logged in username in admin

    Dear all, How do I get the current logged in user to be used and inserted to a model? from django.db import models from django.contrib.auth.models import User class News(models.Model): creator = models.ForeignKey(User) I want the creator field to be the current logged in user. Thank you very much. -- If you can't believe in God the chances are your God is too small. Read my blog: http://joshuajava.wordpress.com/ Follow me on twitter: http://twitter.com/jpartogi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

    Next Message by Thread:

    Re: Unicode issue with Oracle

    On 1/27/09, Karen Tracey <kmtracey@xxxxxxxxx> wrote: > Don't use cx_Oracle 5.0, use a 4.X version. The docs were recently changed > to specify that, after investigation into > http://code.djangoproject.com/ticket/9935 revealed the problem is due to a > bug in cx_Oracle 5.0. Just for the record: this bug doesn't exist anymore with cx_Oracle 5.0.1. -- João Olavo Baião de Vasconcelos Bacharel em Ciência da Computação Analista de Sistemas - Infraestrutura joaoolavo.wordpress.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
    blog comments powered by Disqus

    Home | News | Sitemap | FAQ | advertise | OSDir is an Inevitable website. GBiz is too!