On Tue, 2008-10-07 at 18:23 -0400, J wrote:
> Hi everyone,
> 
> I'm working on a bilingual website in django for which the visitor has
> the option of choosing which language he/she prefers. All the
> interface, as well as the content, will be available in Spanish and
> English. The interface is set up to be handled by gettext and django's
> has excellent support for that.
> 
> I'm having a hard time right now with the bilingual "posts" system,
> which is for contributors to add articles in different categories.
> Each post will have two items for it, one in each language.
> 
> The way I've got it set up in the models is:
> 
> ######
> # The parent of all the posts,
> # only one item per post in here
> ######
> class Post(models.Model):
>     referencetitle = models.CharField(max_length=30)
>     date = models.DateTimeField('Date')
> 
> ######
> # language content for each post
> # there will be two items per post in here, one for each language
> ######
> class PostI18N(models.Model):
>     post = models.ForeignKey(Post)
>     slug = models.SlugField(
>         'Slug',
>         help_text='Automatically built from the title.'
>     )
>     title = models.CharField('Title', max_length=30)
>     body = models.TextField('Body Text')
>     lang = models.CharField(max_length = 5, choices =
> settings.LANGUAGES)
> 
> 
> 
> How do it do this in views so that I only get the "PostI18N" items
> that are in the current language chosen by the users,

There's a currently undocumented function in django.utils.translation
called get_language() that you can use to return the currently active
locale. So something like

        PostI18N.objects.filter(lang=get_language())
        
will filter out only the articles matching the current language. You can
safely use that function, even though I said it was undocumented. That's
because it will be documented inside the next 48 hours. I'm currently
rewriting the i18n documentation and just made a note to include that (I
hadn't realised until just now that we had left it out).

>  and how do I iterate through both of these models at the same time in
> templates so that I can access the Date field from the related table
> ("Post") to sort them, but display the main content fields from
> "PostI18N".

Sorting is generally best done in the view. Even better, sorting can be
done by the database at selection time. Modifying the above queryset,
you could write

        PostI18N.objects.filter(lang=get_language()).order_by('post__date')
        

> 
> I'm initially attempting to display this data using generic views
> (django.views.generic.date_based.archive_index). Do you think these
> generic views can handle something like this? If not, how would I
> write an equivalent custom view?

Generic views can handle anything you can create as a queryset. However,
the above queryset actually depends on some information that is not
available until the view is active (you can't compute it once). This is
because the results of get_language() depend on the locale. So you would
need to wrap things up like this:

        def my_view(request, ....):
           qs = PostI18N... # <-- as above
           return archive_index(qs, ....)
        
That way, the get_language() call is executed each time in the right
environment.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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