Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecimalBlock values become strings #4647

Closed
balazs-endresz opened this issue Jun 20, 2018 · 4 comments
Closed

DecimalBlock values become strings #4647

balazs-endresz opened this issue Jun 20, 2018 · 4 comments

Comments

@balazs-endresz
Copy link

Issue Summary

DecimalBlock values work as expected when previewing a page, however they become strings when the published page is rendered.

Steps to Reproduce

  1. Add a StructBlock with a DecimalBlock field
  2. Add a template filter that returns the type of the decimal block value, e.g. register.filter(name='type')(lambda v: type(v))
  3. Add a custom template for the StructBlock conatining: {{ value.some_decimal_field|type }}
  4. Previewing the page will print <class 'decimal.Decimal'>
  5. Publish and view the live page -> the type will be <class 'str'>

(As a quick fix I've just added |add:0 because that converts it to a number.)

Technical details

  • Python version: 3.6
  • Django version: 2.0.4
  • Wagtail version: 2.1
@konrad147
Copy link

Another workaround {{ value.some_decimal_field|floatformat:"2" }}

@lb-
Copy link
Member

lb- commented Apr 11, 2022

Probably still happening but should be validated. Good first issue label and other labels added.

Whoever wants to pick this up should start looking at this code.

https://github.com/wagtail/wagtail/blob/main/wagtail/blocks/field_block.py

Most likely the value being shown in the Preview is based on the field widget but the value from the DB is from JSON and maybe not getting correctly parsed when pulled back out.

value_from_datadict and value_from_form would be good places to start logging on the DecimalBlock and then from there see what may be missing or different.

@yvess
Copy link
Contributor

yvess commented Jun 15, 2022

I have validated this on the wagtail main branch (short after 3.0 release) not an Issue anymore.
Test code is here https://github.com/yvess/bakerydemo/tree/test-5176

With this template code:

<hr>
<span>{{ self.text }}</span>
<span>{{ self.number }}</span>

I get this in the bakery demo, looks right
Screen Shot 2022-06-15 at 15 56 51

update:
my test was not complete issue is still occurring looking into it

@gasman
Copy link
Collaborator

gasman commented Jun 15, 2022

Hi @yvess,
Your test code isn't quite complete - as well as displaying the number, you need to display its type (using the template filter code provided above). This shows the difference in behaviour between the preview and the live page:

(preview)
Screenshot 2022-06-15 at 17 38 40

(live)
Screenshot 2022-06-15 at 17 38 56

I'll open a PR against your bakerydemo fork with this additional code. (edit: opened now yvess/wagtail-bakerydemo#1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants