Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Migrating custom view to class-based generic views ; Using staticfiles assets in CSS files? [RSS Feed]

#1 Jan. 18, 2011 01:11:51

Victor H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Migrating custom view to class-based generic views ; Using staticfiles assets in CSS files?


Hi,

I'm currently migrating one of my apps to use the new contrib.staticfiles
module in Django 1.3.

>From the documentation I can see there's two ways of referring to static
files:http://docs.djangoproject.com/en/dev/howto/static-files/1. Use {{ STATIC_URL}}
2. Use {% load static %}, then {% get_static_prefix %}

However, option 1 seems to only work if you're using RequestContext - the
easiest way of doing this seems to be using Generic (Class-based) Views.

Currently, I'm using a fairly simple custom view that returns a QuerySet of
"Article" objects matching certain date/filtering criteria, then passes it
to render_to_response. The start-date and entry-date are passed as part of
the URL ie..http://server.com/report_foo/2011-01-01/to/2010-01-05def report_foo(request, start_date, end_date=None):
if end_date:
article_list =
Article.objects.filter(in_daily_briefing=True).filter(entry_date__gte=start_date).filter(entry_date__lte=end_date).order_by('category',

'headline')
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
else:
article_list =
Article.objects.filter(in_daily_briefing=True).filter(entry_date=start_date).order_by('category',

'headline')
return render_to_response('report_foo.html', {'article_list':
article_list, 'start_date': datetime.datetime.strptime(start_date,
'%Y-%m-%d'), 'end_date': end_date})

First question - what is the best way to migrate this to a generic view? I
was thinking:

from django.views.generic improt TemplateView
class ReportFooView(TemplateView):
if end_date:
article_list =
Article.objects.filter(in_daily_briefing=True).filter(entry_date__gte=start_date).filter(entry_date__lte=end_date).order_by('category',

'headline')
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
else:
article_list =
Article.objects.filter(in_daily_briefing=True).filter(entry_date=start_date).order_by('category',

'headline')
params = {'article_list': article_list, 'start_date':
datetime.datetime.strptime(start_date, '%Y-%m-%d'), 'end_date': end_date}
template_name = "report_foo.html"

Or is there a smarter way of doing this with the provided mixins/generics
views?

And now my second question - I can use STATIC_URL or get_static_url in my
template files - but how do I use these values in my CSS files? I.e. my CSS
files need to reference assets stored as part of staticfiles.

Cheers,
Victor

--
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
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Migrating custom view to class-based generic views ; Using staticfiles assets in CSS files? [RSS Feed]

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support