0

I've a django website set in French in the settings file.

In my template, I've the following entry <a href="/{{ year }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>.

In my URLconf, I have the entry url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/$',MonthArchivePostView.as_view())

The problem is that in the template, the date generated is in French (for example, for February, I've got <a href="/2012/fév/">Février</a>) but the url expects the English version (/2012/feb/).

I don't mind having the dates in the url in English or in French, I just need to have the same generated in with the template and expected in the URLconf file.

Thank you


Update quick-fix-not-really-a-solution : use the m format everywhere instead of b to have /02/ instead of /feb/

So in the urls.py

url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$',
    MonthArchivePostView.as_view()
),

and in the generic views

class MonthArchivePostView(dates.MonthArchiveView):
    model = Post
    month_format = '%m'
    date_field = 'publish'
Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58

2 Answers2

3

You can use unlocalize to avoid localization in the template.

Jakub Roztocil
  • 15,930
  • 5
  • 50
  • 52
  • How do you combine it with `month|date:"b"` ? `month|unlocalize|date:"b"` output an empty string and `month|date:"b"|unlocalize` has no effect (still in french) – Martin Trigaux Feb 16 '12 at 21:46
  • Then maybe try this: `{% localize off %}{{ month|date:"b" }}{% endlocalize %}` – Jakub Roztocil Feb 16 '12 at 21:51
  • Strangely, the `{% localize off %}` as no effect, even on just `{{ month }}`. On the other hand, `{{ month|unlocalize }}` works (just not the right format). Maybe a bug – Martin Trigaux Feb 16 '12 at 22:04
  • I just asked on irc, we can not chain filters like I did. The advice was to use custom tags or process it in the view. – Martin Trigaux Feb 16 '12 at 22:25
  • Okay, I usually use `unlocalize` when I don't want have certains numbers localized so I assumed that it would work for dates as well. Good like with that. – Jakub Roztocil Feb 16 '12 at 22:29
1

As @jkbr already said, unlocalize should do the job.

Beside of that, have you ever been thinking about adding a function 'get_absolute_url(self)' in the model definition of your entry model, instead of defining the url in the template?

It would be better to keep separated content definition (model) from content presentation (template).

Also it could help you in solve your problem.

Try adding a function similar to this one at the end of your entry model:

def get_absolute_url(self):
    from time import strftime
    return "%s%s" % (settings.SITE_URL,strftime("%Y/%b/",self.creation_date))

This should work (if I've understood it right, it's something about displaying data instead of parsing it, as explained in Format localization section of Django documentation.

Then in your template you could do something like:

<a href="{{entry.get_absolute_url}}">{{ month|date:"F" }}</a>
dolma33
  • 4,133
  • 6
  • 28
  • 48
  • Thank you but I'm not sure how to do that. I use generic class (cf my update) so I don't really have a model for the month archive. I use the model Post where I have a function get_absolute_url like explained [here](https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.permalink) – Martin Trigaux Mar 03 '12 at 14:34
  • The generic class view [returns](https://docs.djangoproject.com/en/dev/ref/class-based-views/#django.views.generic.dates.YearArchiveView) only a DateQuerySet and the year, no models. I was using generic classes because it was supposed to be faster and easier but I'm not sure anymore. Also the get_absolute_url [must](https://docs.djangoproject.com/en/1.3/ref/models/instances/#django.db.models.Model.get_absolute_url) contain ascii char only, so `/2012/Feb/` and not `/2012/fév/`. Or maybe `/2012/f%C3%A9v/` but not that good... – Martin Trigaux Mar 04 '12 at 09:41
  • 1
    It could be better if you use slugs for urls, and avoid non-ascii chars. Template tag [slugify](https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#slugify) can be used also in model definitions importing `slugify` from `django.template.defaultfilters` as mentioned [here](http://stackoverflow.com/a/837835/302076) – dolma33 Mar 06 '12 at 14:42