Tags: django
Django Sitemap Framework
2008/11/18 @ 21:22Using the Django sitemap framework is so easy it's almost no work at all. Just make a sitemap object and add it to the sitemap in urls.py. The sitemap framework calls items() in your Sitemap to get the list of objects to put in the sitemap and then calls get_absolute_url() on each object.
models.py
| from django.db import models | |
| ... | |
| class Entry(models.Model): | |
| ... | |
| @permalink | |
| def get_absolute_url(self): | |
| return ... | |
| ... |
sitemap.py
| from django.contrib.sitemaps import Sitemap | |
| from mysite.blog.models import Entry | |
| from django.contrib.sitemaps import Sitemap | |
| from mysite.blog.models import Entry | |
| class BlogSitemap(Sitemap): | |
| priority = 0.5 | |
| def items(self): | |
| return Entry.objects.filter(is_draft=False) | |
| def lastmod(self, obj): | |
| return obj.pub_date | |
| # changefreq can be callable too | |
| def changefreq(self, obj): | |
| return "daily" if obj.comments_open() else "never" |
urls.py
| from mysite.blog.sitemap import BlogSitemap | |
| ... | |
| sitemaps = { | |
| "blog": BlogSitemap | |
| } | |
| (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) | |
| ... |
You can even generate sitemap indexes and it will pagenate the indexes on Google's limit of 50,000 urls so that you don't have a problem with it crawling your indexes.
Django admin inline forms
2008/11/09 @ 01:09For my new project dlife, I went about implementing a simple comments interface that would allow users to make comments on imported feed items. I wanted to support this in the admin in the typical manner such that when you click on an item in the admin, you can see all the comments and edit them from the item's page.
I found that you can use inline forms in the admin but it seems to show a bunch of forms (3 by default) even though I don't have any comments for the item yet. I'll mess with this a bit more later to try to get the behavior I want.
models.py
| class Comment(models.Model): | |
| '''An item comment''' | |
| comment_item = models.ForeignKey(Item) | |
| comment_date = models.DateTimeField() | |
| comment_user = models.ForeignKey(User, null=True, blank=True) | |
| comment_name = models.CharField(max_length=30) | |
| comment_email = models.EmailField() | |
| comment_homepage = models.URLField(max_length=300) | |
| comment_content = models.TextField(null=True, blank=True) | |
| | |
| class Meta: | |
| db_table="comments" | |
| ordering=["comment_item", "-comment_date"] |
admin.py
| class CommentInline(admin.StackedInline): | |
| model = Comment | |
| max_num = 1 #TODO: Fix this | |
| exclude = ['comment_item','content_type','object_id'] | |
| class ItemAdmin(admin.ModelAdmin): | |
| list_display = ('item_title', 'item_date') | |
| exclude = ['item_clean_content',] | |
| list_filter = ('item_feed',) | |
| search_fields = ('item_title','item_clean_content') | |
| list_per_page = 20 | |
| | |
| inlines = [CommentInline,] |
Python Onsen Oct. 2008
2008/10/29 @ 17:12Last weekend I went to my second Python Onsen[jp] organized by Nakai-san(id:voluntas). I talked about Python Onsen in my first blog post here. Python Onsen is a 3 day event (Fri, Sat, Sun) but as before I only participated on Saturday and Sunday. This time I opted to work on creating a lifestream web app using feedparser and Django. feedparser is a snappy little parser for reading RSS and Atom feeds. The result was dlife which so far can parse a set of feeds and show them on a user's lifestream though it's not in any way user friendly yet (you have to update the feeds in the django shell
).
I also got to know my soon to be co-worker, Okano-san (id:tokibito[jp]), by talking about jQuery Internals' data() function and web/Django development.
Here's a recap:
- Worked on a sweetcron lifestream replacement in Django (dlife)
- Onsen is pretty lonely by yourself.
- Introduced id:tokibito[jp] to the jQuery Internals' data() function
- Since I come on Satruday, I always miss introductions so I never know who is who.
- feedparser is really simple and easy to use. Though I'm not sure what I'll do about pictures and video yet.
- No one mentioned my blog or linked to it in their posts
(Maybe because I never write anything? ) - In a raffle、I got a cool Python shirt from Accense Technologies'[jp] Masuda-san(id:whosaysni[jp]).
- It was lonely going home by myself from Kinomiya station.
Django
2008/07/28 @ 01:15I was thinking about using Django for one of my projects on GAE because it seems like a popular project and somewhat easy to use, but I'm not quite understanding yet why it's better to have helper functions rather than controller/handler classes like Pylons or GAE's normal WSGI handling has. With handler classes my controller might look like:
| from google.appengine.ext.webapp import RequestHandler | |
| class MainHandler(webapp.RequestHandler): | |
| def get(self): | |
| # Read data from BigTable here | |
| self.response.out.write(outputhtml) | |
| def post(self): | |
| # Write data to BigTable here | |
| #redirect back to the url | |
| self.redirect(self.request.url) |
Whereas the django helper function might look like
| from django.http import HttpResponse, HttpResponseRedirect | |
| def mainview(request): | |
| if request.method == 'POST': | |
| # Write to BigTable Here | |
| return HttpResponse(outputhtml) | |
| elif request.method == 'GET': | |
| # Read from BigTable Here | |
| return HTTPResponseRedirect(request.url) |
While the Django method might have the potential to have be a bit less verbose it feels like it would be harder to do things correctly, like factor code etc. I also don't really like the conditional checks to see what kind of HTTP method was used. So either I would need to split GETs and POSTs to separate urls or just live with the conditional checks.
Personally I feel better with the Pylons-ish controller/handler approach. Anyone have an opinion?









