django 1.6 with django_tables2 cannot change DATETIME_FORMAT - django

python 2.7.5+. django_tables2 0.14.0
In settings.py:
USE_L10N = True
DATETIME_FORMAT = 'M j, Y H:i:s.u'
Didn't work. I tried {% load L10N %} at the top of my template. Didn't work.
I created a formats directory at the top of my project containing an en directory containing formats.py and added the following to my settings.py (yes, there are init.py files in the formats directory and in the en directory):
FORMAT_MODULE_PATH = '<project>.formats'
Didn't work.
I tried putting in {{created_at|date: 'M j, Y'}}{{created_at|time: 'H:i:s.u'}} before the {% render_table table %} tag from django_tables2. Got an error.
Tried the recommended django_tables2 method of, in my tables.py definition, adding to the Meta class:
localize('created_at')
Didn't work.
Tried putting 'localize=True' in the column definition for created_at in tables.py.
Didn't work.
The only thing that worked was modifying python2.7/site-packages/django/conf/locale/en/formats.py (in my virtual environment) but I won't be allowed to do that on my project because it's not considered portable.
I see stackoverflow entries that say this works but they are many years old and many django versions back. I see others here that say this doesn't work and they haven't/aren't going to fix it.
This functionality logs requests; during testing the other devs could really use seeing seconds and probably microseconds to help them debug but I can't seem to give it to them.
Any suggestions that I haven't already tried?
Thanks.

OK, blush - I got it. I made the following change in the model for the DateTimeField created_at:
#property
def nice_date(self):
# mm/dd/yyyy HH:mm:ss.uuuuuu
t = self.created_at.strftime("%m/%d/%Y %H:%M:%S.%f")
return t
Then, I displayed nice_date instead of created_at.
I hope this helps someone else.

Related

How to query Django PostGIS / Postgres DB with the map / Leaflet boundary box view?

Results from query made with other fields are populated on Leaflet:
var myMap = L.map("mapid", {zoomControl: false});
L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {maxZoom: 18 }).addTo(myMap); myMap.setView([-34, 151], 12);
var markers = [
{% for journal in queryset %}
L.marker([{{ journal.latitude }}, {{ journal.longitude }}]),
{% endfor %}
]
var group = L.featureGroup(markers).addTo(myMap);
I now want the results populated by where the user places Leaflet's boundary box, but I'm bewildered on how to do that. I've got GDAL installed, have converted the long / lat cols to a spatial col in Postgis with ADD COLUMN geom geometry(Point, 4326) GENERATED ALWAYS AS (ST_SetSRID(ST_MakePoint("longitude", "latitude"), 4326)) STORED, added geom = models.PointField(blank=True, null=True) to models.py, but am lost on where to go from here.
This seems the most authoritative tutorial on the topic but I'm totally lost when he gets to views and talks of a generic class based view / list views / Django Rest Framework. Is there a dumbed down explanation somewhere? I feel like an idiot.
Unsure if relevant but the table isn't managed by Django.
Figured it out, although the solution is far more complicated than I initially understood.
This tutorial by the same guy who made the video was helpful. Please see the tutorial as he gives a better explanation than I can.
Also, I couldn't find anything anywhere on installing the PostgreSQL C Client library or what he installs as libpq5 on Win10, but it seems this was unneeded, perhaps because it might already be installed with Postgres.

Removing built-in fields from wagtailstreamforms

I'm trying to remove the date, time, and multiselect fields from the wagtailstreamforms Admin page, such that they can't be used in any form site-wide.
I've tried calling register('<field_name>', None) to get rid of it, but this doesn't work:
# wagtailstreamforms_fields.py
from wagtailstreamforms.fields import register
#register('date', None)
#register('time', None)
#register('multiselect', None)
And creating an AppConfig to manually purge the wagtailstreamforms.fields._fields dict of the entries, but that doesn't seem to work either. I've made sure that this AppConfig is part of an app that loads after wagtailstreamforms.
class UpdatedConfig(AppConfig):
name = 'my_new_app'
def ready(self):
from wagtailstreamforms.fields import _fields
_fields.pop('date')
_fields.pop('datetime')
_fields.pop('multiselect')
for x in _fields.keys():
print('{}: {}'.format(x, _fields[x]))
Is there any way to do this, hacky or otherwise? I'm using Wagtailstreamforms 3.1 and Wagtail version 2.2.2.
Im the author of wagtailstreamforms and came across this. The ability to restrict what default form fields are loaded from the package is a great idea.
What I propose is to not load them from the the register method but to load them from a settings dict ie:
WAGTAILSTREAMFORMS_DEFAULT_FIELDS = {
'singleline': 'wagtailstreamforms.fields.SingleLineTextField',
'multiline': 'wagtailstreamforms.fields.MultiLineTextField',
'dropdown': 'wagtailstreamforms.fields.DropdownField'
}
the defaults being all the internal fields. This way it can easily be overridden. We will leave the register decorator in place as to not break anything.
https://github.com/AccentDesign/wagtailstreamforms/pull/110
Please leave any comments / suggestions on the pr or the open issue. If you are happy with this will update docs, merge, release then can amend this as an answer.
Cheers, Stu.
One way would be to override this template: https://github.com/wagtail/wagtail/blob/master/wagtail/admin/templates/wagtailadmin/block_forms/stream_menu.html
{% for child_block in child_blocks.list %}
{% if child_block.name != "date" and child_block.name != "datetime" and child_block.name != "multiselect" %}
<li><button type="button" class="button action-add-block-{{ child_block.name }} icon icon-{{ child_block.meta.icon }}"><span>{{ child_block.label }}</span></button></li>
{% endif %}
{% endfor %}
We were able to get what we needed by putting following code in the app's wagtailstreamforms_fields.py
# wagtailstreamforms_fields.py
from wagtailstreamforms.fields import _fields
if _fields.get('date'):
del(_fields['date'])
if _fields.get('datetime'):
del(_fields['datetime'])
if _fields.get('multiselect'):
del(_fields['multiselect'])
So I guess you can say its a mix of the above 2 methods. It might be obsolete in the near future, see Stuart George's answer and its linked PR.

Translate date format in django: technical message id?

I'm working on a site that displays dates, and is in several languages via i18n.
I'm displaying dates with a filter (sometimes I want only the day, only the hour, etc.)
I've read the last paragraph of this: https://docs.djangoproject.com/en/1.2/topics/i18n/, and from what I understand I'm supposed to be able to specify the formats I want in the django.po locale and use them in the template filter ?
It doesn't seem to work though.
{{ date_start|date:"{% trans "DATE_HOUR_ONLY" %}" }}
With in the django.po for english language:
msgid "DATE_HOUR_ONLY"
msgstr "%P"
This completely blows up though...
And using single quotes instead of doubles ({{ date_start|date:"{% trans 'DATE_HOUR_ONLY' %}" }}) doesn't blow up but displays this :
{% 31Fri, 17 May 2013 00:00:00 -0500a.m.500 'FriAMCDTMay_00-05001368766800R_-0500MayFalse2013' %}
I can't find any concrete example online on how to proceed. Without the filters, the date switches correctly depending on the language, because of how DATE_FORMAT is set in my settings.py
Thanks a lot !
Well, after a bit of wandering around, this worked:
{{ date_start|date:_('DATE_WITH_HOUR') }}
Maybe you can try Django formats.py
https://docs.djangoproject.com/en/1.4/topics/i18n/formatting/
For example , i have a formats folder in my project, don't forget the "init.py"
if you want add a different locale,only need add a folder like 'zh_TW' in the 'en' same level
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
the formats.py look like
SHORT_DATE_FORMAT = 'Y/m/d'
SHORT_DATETIME_FORMAT = 'Y/m/d G:i:s'
and in my template.html
{ now | date:"SHORT_DATE_FORMAT" }
that can change by your locale, hope the solution will help you :)

Django-haystack search with whoosh returns 'No results' debugging unsuccessful

I am new to Django and Haystack. I have done everything suggested on the "getting started" page of the Haystack Documentation and on the debugging page, but I cannot see where my error lies. sqs.count() returns 0 even though rebuild_index seems to work, giving the right number of entries in the tables being indexed and producing index files into the whoosh_index directory.
search_indexes has the required text = lines, I have a search_sites.py, I have changed the settings.py and urls.py, and I have [model_name]_text.txt files in the right directory.
We are working on a university server where we had to update Django to work with Haystack after we installed Haystack because we realized it was an incompatible version (1.1 not 1.5) - could the old Django have messed with the Haystack installation somehow?
The following code will show you what has been indexed - replace with the correct path for your setup.
from whoosh.index import open_dir
ix = open_dir('<yourapp>/whoosh_indexes')
from pprint import pprint
pprint(list(ix.searcher().documents()))
If that doesn't shed any light you will have to post more details of your code - particularly the search_indexes.py
This might seem obvious but I had the same error and reading from http://django-haystack.readthedocs.org/en/latest/debugging.html I found out that I was using the wrong attribute name for the returned objects.
ensure that {{ result.object.title }} corresponds to your model field
for instance I had name as the model filed but kept wondering why I didn't get results.
I had to change to {{ result.object.name }} to list my results
name was the needed attribute. Hope this helps someone.

gae_mini_profiler {% profiler_includes %} gives Invalid block tag: 'profiler_includes'

I am attempting to install gae_mini_profiler in my django-nonrel app
I placed the {% profiler_includes %} tag at the bottom of my base.html
It results in a
Exception Type: TemplateSyntaxError
Exception Value: Invalid block tag: 'profiler_includes'
I placed
from gae_mini_profiler import profiler
application = profiler.ProfilerWSGIMiddleware(application)
at the bottom of djangoppengine/main/__init__.py
I followed all the other instructions at https://github.com/kamens/gae_mini_profiler#start
What am I doing wrong?
I solved this by changing gae_mini_profiler/templatetags.py to be a true template tag library.
To do this create a package called templatetags, and then move(and rename) the templatetags.py module to profiler_tags.py.
Inside of profiler_tags.py make the following changes:
Change:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
To:
from django.template import Library
register = Library()
Change:
path = os.path.join(os.path.dirname(__file__), "templates/includes.html")
To:
path = os.path.join(os.path.dirname(__file__), "../templates/includes.html")
In your settings file add gae_mini_profiler to your list of installed apps.
Remove all references to
template.register_template_library('gae_mini_profiler.templatetags')
In your templates whereever you had {% profiler_includes %} you then need to add a load block
{% load profiler_tags %}
I think that is all of the changes, but need to go check my git log.
Are you using the new Python 2.7 runtime for GAE? If so, the django template setup is slightly different and gae_mini_profiler hasn't been upgraded yet (anyone is welcome to submit this fix, haven't gotten to it yet).
It should be easy to work around as the only thing you need to do is find a way to render the HTML string returned by gae_mini_profiler.templatetags.profiler_includes() anywhere in your page. There are a number of methods of accomplishing this if the built-in template tag isn't working as-is. You could simply call the function in your base request handler and pass the resulting html into your base template if absolutely necessary (although this is admittedly a gross hack).
We'll hopefully have Python 2.7 working w/ gae_mini_profiler shortly. If you're not on Python 2.7, I'm not sure what the issue is as I'd expect the current code to work...