> Having your URLs end with a slash is good. It looks better and shortens 
> your pages address. Also, in the future, you may find that not all of 
> your pages are going to be text/html.

While I'm ambivalent about the trailing slash (the below assumes 
that it's disabled), many of my urls/views have something like 
the following that allows me to have templates for 
html/xml/json/csv files and one URL can serve up whatever flavor 
the requester/link wants.  It also has an intuitive URL 
interface, as it's obvious that

  http://example.com/path/to/foo.html

should be HTML, and

  http://example.com/path/to/foo.xml

should return XML, and

  http://example.com/path/to/foo.json

should return a JSON object.

It does require that I have N versions of each of my templates, 
but that's generally not been much of a problem.  Most of the 
time I've done something like this, I just have a select few 
formats that I use.  Given that some of our clients want their 
data in such varied flavors for viewing, downloading, or for 
programatic interaction, this gives me the flexibility to keep 
them all happy with minimal fuss/work on my part. :)

Hope this is helpful to you too,

-tim


    ### urls.py has a line something like ###
   (regexp + '(?:\.(?P<format>html|htm|xml|csv))?',
     'proj.app.myview')


   ### views.py has something like ###

   CONTENT_TYPE = 'Content-Type'
   CONTENT_DISPOSITION = 'Content-Disposition'
   TEXT_PLAIN = 'text/plain'
   TEXT_XML = 'text/xml'
   TEXT_HTML = 'text/html'
   DISP_ATTACH = 'attachment; filename=%(filename)s'

   KNOWN_FORMATS = {
     'csv': {
       CONTENT_TYPE: TEXT_PLAIN,
       CONTENT_DISPOSITION: DISP_ATTACH,
       },
     'xml': {
       CONTENT_TYPE: TEXT_XML,
       CONTENT_DISPOSITION: DISP_ATTACH,
       },
     'html': {
       CONTENT_TYPE: TEXT_HTML,
       },
     #  other formats here
     }

   HTML_FORMAT = 'html'
   def myview(request, ... , format=HTML_FORMAT):
     if format == 'htm': format = HTML_FORMAT
     # your code here...
     template_name = 'foo.%s' % format
     response = render_to_response(template_name, context)
     for header, value in KNOWN_FORMATS[format].items():
         response[header] = value % {
             'filename': template_name,
             'date': datetime.date.today().strftime('%Y%d%m'),
             # add other things you might want in the filename
             }
     return response




.

--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to