jazzband / django-recurrence

Utility for working with recurring dates in Django.
https://django-recurrence.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
486 stars 190 forks source link

Error: Can't find variable "pggettext" and partial solution/workaround #147

Open nlittlejohns opened 5 years ago

nlittlejohns commented 5 years ago

Hi, I hit this issue while using Django-recurrence (1.10, latest), Django 2.2.2 and Python 3.7 and I think a few others are having the same problem. The problem is that when loading a form with RecurrenceField, the field just shows a blank textfield and my browser's debugger says it can't find the variable "pggettext", which should be loaded as part of the JavaScriptCatalog view.

I've isolated the issue to a couple of different areas:

1: Documentation for JavaScriptCatalog results in an error If you use the code supplied in the Django-recurrence documentation for adding the JavaScriptCatalog to your URLs, you get this error when you try to load that view:

Request Method: | GET http://127.0.0.1:8000/jsi18n/ 2.2.2 AttributeError 'tuple' object has no attribute 'split'

I adapted the code from the Django documentation and got a working jsi18n URL as follows:

urlpatterns += [
    path('jsi18n/recurrence/',
         JavaScriptCatalog.as_view(packages=['recurrence']),
         name='javascript-catalog'),
]

Using this code, you get a valid javascript file at http://127.0.0.1:8000/jsi18n/recurrence/ (or whatever your server address is)

2: Code to find the JavaScriptCatalog doesn't find the JavaScriptCatalog This could be because I'm using a nonstandard implementation for the JavaScriptCatalog, but I tried lots of variations to make it work, and none of them found it.

What did work is a total hack: Hard-coding the path of the JavaScriptCatalog in recurrence/forms.py

_recurrence_javascript_catalog_url = '/jsi18n/recurrence'

This goes right before def find_recurrence_i18n_js_catalog():

This has solved my issue, but it's an inelegant solution. Hopefully it puts a more skilled code on the path to finding a better solution.

Now it's working, I'm having an issue that it doesn't render very well, I think this is a compatibility issue with crispy forms, but if anyone has any tips, let me know!

dominicrodger commented 5 years ago

Thanks for this investigation @nlittlejohns. Which code in the docs were you using? Slightly confusingly, the docs had references to how to set up the JavaScript catalog pre-Django 1.10, and a second example for Django 1.10 onwards. The code you ended up with is very similar to the second section. I've removed the first section for now, since we don't support Django < 1.11.

I'm not sure what's going on in (2) - do you mean you were using a non-standard version of the code for urls.py? I have to admit that I've never used django-recurrence outside of the admin (honestly, I don't think I've ever looked at find_recurrence_i18n_js_catalog before!), and that code looks pretty strange. I wonder if we're better off requiring people to configure the URL in settings.py.

nlittlejohns commented 5 years ago

Thanks for your response Dominic!

If I use the code in the docs, I get the AttributeError. This is what I originally added to my project's urls.py:

# If you already have a js_info_dict dictionary, just add
# 'recurrence' to the existing 'packages' tuple.
js_info_dict = {
    'packages': ('recurrence', ),
}

# jsi18n can be anything you like here
urlpatterns += [
    re_path(r'^jsi18n/$', JavaScriptCatalog.as_view(), js_info_dict),
]

The only change from the docs is that I use re_path instead of url, but the result should be the same. If instead I use the code I shared in my original post, I can successfully load the JavaScriptCatalog view at the specified location.

I agree that the find_recurrence_i18n_js_catalog does a bit of guesswork on behalf of the user and that it would be much simpler to include it in Settings. If the default setting matched the path in the installation documentation (and the documentation produced a consistently valid JavaScriptCatalog view), then the issue should be fixed.

Edited for clarity.

dominicrodger commented 5 years ago

OK - thanks Neil!

If you're open to it - pull requests welcome to make this all a bit better - I'm not actually using django-recurrence at the moment (and haven't since about Django 1.8!), so I'm a little out of date :)

nlittlejohns commented 5 years ago

Happy to submit something when I'm a bit more confident, still very new to lots of this! Thanks again 😊

nlittlejohns commented 5 years ago

I did it! I made my first ever pull request.

Hopefully I didn't screw it up!

nlittlejohns commented 5 years ago

Hmm, just looking at new issues and wondering if this is a better solution? https://github.com/django-recurrence/django-recurrence/issues/153

manuelkappler commented 4 years ago

Hmm, just looking at new issues and wondering if this is a better solution?

153

153 solved that issue for me as well. It seems like form.media was not importing jsi18n.