Thursday, March 31, 2016

Re: Q() chaining with & returns empty set

> Den 31. mar. 2016 kl. 04.14 skrev Bai Shun <wanyancan@gmail.com>:
>
> I've got a problem in using Q()

To see what's going on, try to print the SQL that Django generates for working and non-working queryset:

> print Basket.objects.filter(Q(fruit__ofkind__name__in=['apple'])&Q(fruit__ofkind__name__in=['banana'])).distinct()

> Basket.objects.filter(fruit__ofkind__name__in=['apple']).filter(fruit__ofkind__name__in=['banana']).distinct()

Print SQL like this:

print(my_queryset.query)


Erik

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2C4D0275-020B-4236-9F11-154979510967%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Re: Django + Nginx + X-Accel-Redirect

Try changing file_url to be relative: 

file_url = '/media/users/avatar.png'

Also check the nginx error log to make sure the path is being constructed properly by nginx.


From: "Neto" <paulosouzamaciel@gmail.com>
To: "Django users" <django-users@googlegroups.com>
Sent: Thursday, March 31, 2016 11:04:07 AM
Subject: Django + Nginx + X-Accel-Redirect

How do I access protected directories using Django? I have the following configuration but is not working, there is always page 404 when I try to access the directory.

Django:

def test(request):
    file_url
= 'http://mysite.com/media/users/avatar.png'
    response
= HttpResponse()
    response
['X-Accel-Redirect'] = file_url
   
return response

Nginx:

server {
    listen
80;
    server_name mysite
.com;

    location
/media/ {
       
internal;
       
alias /home/ubuntu/webapps/myapp/media/;
   
}
}



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c965f092-bf02-4146-8a5f-2e5f7fef90db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: django admin_tools et file edition

CKEditor is appropriate for files that need solid typography and formatting. If you're looking for something simpler, Markdown is a common solution. Markdown is a markup syntax widespread on internet forums.

None of those really address "configuration files," though. Is fancy formatting actually the problem you're trying to solve? Are you trying to find a way to save and edit files on a hard drive? Save text to a database to affect the behavior of your website? Something else?

On Thu, Mar 31, 2016 at 11:51 AM, vassilux <v.gontcharov@gmail.com> wrote:
Hi alls,
It is my first django project and   I look for a solution to edit files, for exemple allow to an "advanced user" editing configuration files.


I looked into django-ckeditor  but it is not clear for the moment

Where  I can look for informations / examples ?


Thanks





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d9fda07-2a03-4fe9-8046-e853fd616943%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYVzpsutDAj%3Db%2BHxyRZQC3R-EVReyLXDLsa%2BP8TS9-qXvA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Issue with django docs translations and google indexing

See https://github.com/django/djangoproject.com/issues/621 for some recent improvements we tried to fix this. Perhaps more needs to be done but things look better for me since that change.

On Thursday, March 31, 2016 at 1:40:26 PM UTC-4, Piotr Gnus wrote:
Hello,

when translations were implemented in django docs, there is an issue with google indexing documentation pages - all language versions are indexed as english language, so searching for particular documentation page from google gives results on non-english documentation versions if your language is set to english in google. And it can be random, some results are in Japanese, some in Spanish, some in other languages.

Issue is probably caused by 'lang="en"' being set in <head> tag for all languages. Setting it to proper language should stop google indexing all languages as english.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4c613fd5-3bdb-4beb-87f5-3bcce9bca2ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Issue with django docs translations and google indexing

Hello,

when translations were implemented in django docs, there is an issue with google indexing documentation pages - all language versions are indexed as english language, so searching for particular documentation page from google gives results on non-english documentation versions if your language is set to english in google. And it can be random, some results are in Japanese, some in Spanish, some in other languages.

Issue is probably caused by 'lang="en"' being set in <head> tag for all languages. Setting it to proper language should stop google indexing all languages as english.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fdac0a8d-51c4-4557-807f-5578b5a3827c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

django admin_tools et file edition

Hi alls,
It is my first django project and   I look for a solution to edit files, for exemple allow to an "advanced user" editing configuration files.


I looked into django-ckeditor  but it is not clear for the moment

Where  I can look for informations / examples ?


Thanks





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d9fda07-2a03-4fe9-8046-e853fd616943%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Redefine html properties in widget

Some attributes such as IntegerField.min_value need to be customized on the field rather than the widget. This will ensure that validation is also done server-side. (see https://github.com/django/django/blob/f3595b25496691966d4ff858a3b395735ad85a6e/django/forms/fields.py#L278-L285 for example)

On Thursday, March 31, 2016 at 11:47:34 AM UTC-4, Denis Makarov wrote:
Hello!

I have my own ModelForm with widgets in Meta class.

class Meta:

 model
= Foods

 fields
= '__all__'

 widgets
= {

 
'quantity': NumberInput(attrs={'min': 1, 'max': 10, 'name': "quantity2"}),

 
'comment': TextInput(),

 
}


My quantity field have NumberInput as standard.

I want to redefine html properties such name and min value.

But when I did this (code above), my fiend quantity have old values (name and min value). Max value correct.

<p><label for="id_quantity">Quantity:</label> <input id="id_quantity" max="10" min="0" name="quantity" type="number" value="1" /></p>

Django (1.9.4)

What's wrong?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c25f5b58-f5bc-4e31-aa67-f763bef60034%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Django + Nginx + X-Accel-Redirect

How do I access protected directories using Django? I have the following configuration but is not working, there is always page 404 when I try to access the directory.

Django:

def test(request):
    file_url
= 'http://mysite.com/media/users/avatar.png'
    response
= HttpResponse()
    response
['X-Accel-Redirect'] = file_url
   
return response

Nginx:

server {
    listen
80;
    server_name mysite
.com;

    location
/media/ {
       
internal;
       
alias /home/ubuntu/webapps/myapp/media/;
   
}
}



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c965f092-bf02-4146-8a5f-2e5f7fef90db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Redefine html properties in widget

Hello!

I have my own ModelForm with widgets in Meta class.

class Meta:

 model
= Foods

 fields
= '__all__'

 widgets
= {

 
'quantity': NumberInput(attrs={'min': 1, 'max': 10, 'name': "quantity2"}),

 
'comment': TextInput(),

 
}


My quantity field have NumberInput as standard.

I want to redefine html properties such name and min value.

But when I did this (code above), my fiend quantity have old values (name and min value). Max value correct.

<p><label for="id_quantity">Quantity:</label> <input id="id_quantity" max="10" min="0" name="quantity" type="number" value="1" /></p>

Django (1.9.4)

What's wrong?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dc1f06a5-351c-4bbf-8fbb-8799baae41c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

How to reference previous instance when looping in a template?

Hi,

I'm doing a lot of this type of looping in my templates (from the docs: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#for):
<ul>
{% for athlete in athlete_list %}
   
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>

In this context, I find myself occasionally wanting to refer to data of the previous athlete (if the instance exists) while working on the current athlete.

The way I do this right now is within a custom template tag. I copy the data of interest into a session variable for use next time the custom template tag is called. This works, but I'm wondering if there is a better way?

My thought was the following: what if there existed a .previous property attached to the current instance that contains the previous instance for this purpose?

IE:

<ul>
{% for athlete in athlete_list %}
   
<li>This time: {{ athlete.name }}, last time: {{ athlete.previous.name }}</li>
{% endfor %}
</ul>

athlete.previous would obviously be None the first time round the loop.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fd318850-ac12-4abb-b6dd-62c0a3ee5b1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: run python function in output of annotated query

no specific reason. This value is later converted to json, and consumed by javascript from Ajax call..

this was the simplest way I thought, without being obliged to rewrite an one ton of code.

( yes it is a lovely project with spaghetti code, which I hope will one day be authorized to rewrite it from scratch.  :-( )



On Thu, Mar 31, 2016 at 11:28 AM, Simon Charette <charette.s@gmail.com> wrote:
Hi Fábio,

Since the deprecation of `SubfieldBase`[1] you must implement a
`from_db_value()`[2] method to convert the value as returned by the database
to the correct Python object.

Out of curiosity, is there a reason you are doing this the model level? It
looks like something that belongs to the form or template layer.

Cheers,
Simon


[1] https://docs.djangoproject.com/en/1.9/releases/1.8/#subfieldbase
[2] https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.Field.from_db_value


Le jeudi 31 mars 2016 10:15:38 UTC-4, Fabio Caritas Barrionuevo da Luz a écrit :
Hello django-users,

I have a unmanaged model, of legacy database, used only to read.

I need to clean the value of a field returned in a annotated query.

I try created a new model field inherits from TextField and override to_python method to clean the value (remove all html using python-bleach), and use in a query like:

FooBar.objects.annotate(title=ExpressionWrapper(F('html_title'), output_field=RemoveHTMLTextField())) .

I expected the method to_python was called, but it does not. 

Any idea how to solve this problem?

this is probably wrong, but it was the only way I thought so far....


####
import bleach
from django.db import models
from django.template.defaultfilters import striptags
from django.utils.safestring import mark_safe


class RemoveHTMLTextField(models.TextField):
    def to_python(self, value):
        original_value = super(RemoveHTMLTextField, self).to_python(value)
        striped_str = striptags(original_value)
        return mark_safe(bleach.clean(striped_str))
####




--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/444e33cb-5f81-4339-a751-95e7314e3770%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMYQWDoGfuyqTk5SXEKPcx47YUnRQPicGy9htQxSwmdQhA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: run python function in output of annotated query

Hi Fábio,

Since the deprecation of `SubfieldBase`[1] you must implement a
`from_db_value()`[2] method to convert the value as returned by the database
to the correct Python object.

Out of curiosity, is there a reason you are doing this the model level? It
looks like something that belongs to the form or template layer.

Cheers,
Simon


[1] https://docs.djangoproject.com/en/1.9/releases/1.8/#subfieldbase
[2] https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.Field.from_db_value

Le jeudi 31 mars 2016 10:15:38 UTC-4, Fabio Caritas Barrionuevo da Luz a écrit :
Hello django-users,

I have a unmanaged model, of legacy database, used only to read.

I need to clean the value of a field returned in a annotated query.

I try created a new model field inherits from TextField and override to_python method to clean the value (remove all html using python-bleach), and use in a query like:

FooBar.objects.annotate(title=ExpressionWrapper(F('html_title'), output_field=RemoveHTMLTextField())) .

I expected the method to_python was called, but it does not. 

Any idea how to solve this problem?

this is probably wrong, but it was the only way I thought so far....


####
import bleach
from django.db import models
from django.template.defaultfilters import striptags
from django.utils.safestring import mark_safe


class RemoveHTMLTextField(models.TextField):
    def to_python(self, value):
        original_value = super(RemoveHTMLTextField, self).to_python(value)
        striped_str = striptags(original_value)
        return mark_safe(bleach.clean(striped_str))
####




--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/444e33cb-5f81-4339-a751-95e7314e3770%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

run python function in output of annotated query

Hello django-users,

I have a unmanaged model, of legacy database, used only to read.

I need to clean the value of a field returned in a annotated query.

I try created a new model field inherits from TextField and override to_python method to clean the value (remove all html using python-bleach), and use in a query like:

FooBar.objects.annotate(title=ExpressionWrapper(F('html_title'), output_field=RemoveHTMLTextField())) .

I expected the method to_python was called, but it does not. 

Any idea how to solve this problem?

this is probably wrong, but it was the only way I thought so far....


####
import bleach
from django.db import models
from django.template.defaultfilters import striptags
from django.utils.safestring import mark_safe


class RemoveHTMLTextField(models.TextField):
    def to_python(self, value):
        original_value = super(RemoveHTMLTextField, self).to_python(value)
        striped_str = striptags(original_value)
        return mark_safe(bleach.clean(striped_str))
####




--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMZCRffePrUkJ6kZFhcn3x-fpjyMmZaoGd4oAh_ba24j1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Model share question

That sounds right. Thank you.

On Thursday, March 31, 2016 at 2:19:45 AM UTC-4, Mike Dewhirst wrote:
On 31/03/2016 8:38 AM, moon jo wrote:
> I'm new to django and have a question regarding model design.
> For practice, I'm working on a web app - a simplified version of imdb
> that handles movie and music.
> I've created 2 apps; Movies and Music. With main models in both having
> the usual fields (title, release date, rating, artist/actor).
> My problem is, I don't know where to put the Person model. I want it to
> be shared by both apps.
> Should there be an app for Person and put the model in there?

Short answer is yes. Long answer is yes.

Once you put the app 'person' into INSTALLED_APPS in settings in each of
the other two apps you can exploit 'person.Person' in foreign keys and
Django will find it. If you prefer using the person model directly you
do "from person.models import Person" then just use Person in foreign keys.

> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2104a588-cb08-42cf-a26c-6259bee1c7c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Example to creatre a dataGrid with djblets

I would like to create a datagrid with Django. I have been reading about how to do that, but I can't find information about it, almost of them don't fix well with Django 1.8

I have read that this is possible with djblets, but I don't know how to begin.

Could someone help me? Is there any example for starting.

I think Django is not make to this kind of components. is is true?

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a94ed167-4e2a-451a-8c24-4d614cd47313%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Test Client response.json() UnicodeDecodeError

Looks reasonable, although I am not entirely clear what role charset has in a JsonResponse. See https://code.djangoproject.com/ticket/23949.

On Thursday, March 31, 2016 at 6:15:53 AM UTC-4, Daniel Blasco wrote:
Hi,

I'm testing the REST API of my application and I found an error that I think that's a Django's issue.

I'm using:
  • Django==1.9.4
  • djangorestframework==3.3.3

I have set the rest framework setting UNICODE_JSON to True.

When i'm testing a JSON response that contains a special character like 'á' then I get a UnicodeDecodeError. This is the traceback:

Traceback (most recent call last):
  File ".../tests/assets/test_views.py", line 584, in test_get
    response.json()
  File ".../lib/python2.7/site-packages/django/utils/functional.py", line 13, in _curried
    return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
  File ".../lib/python2.7/site-packages/django/test/client.py", line 662, in _parse_json
    return json.loads(response.content.decode(), **extra)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 268: ordinal not in range(128)


If I change the line 662 in django/test/client.py to:

return json.loads(response.content.decode(response.charset), **extra)

then it works fine. In this case response.charset is utf-8.


Thanks,
Daniel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c3fba296-1bf0-45dc-97e7-c8d33cee310c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Why doesn't {{ form.field.value }} return cleaned data?



On 31.03.2016 01:08, Ryan Prater wrote:
Say I have a TypedMultipleChoiceField and I'm building a custom select multiple widget in my template.

If I want to check a box based on an initial value (ie. default checked options), OR check a box based on form data that has been returned (ie. error state), I would do this:

<input type="checkbox" {% if form.field.value %}checked{% endif %}>

HOWEVER, {{form.field.value}} source documentation (boundfield.py) states: "Returns the value for this BoundField, using the initial value if the form is not bound or the data otherwise."

So basically the equivalent of:
return (form.field.data or form.field.initial)


Not really, since data that are falsey may be valid data. So it's not equvalent to that.

Wouldn't it be more appropriate and useful for boundfield.value to return CLEANED data that has gone through to_python()? As in:
return (form.field.cleaned_data or form.field.initial)

No because then you would lose non-cleaned data in case of errors.

Also there is no guarantee that cleaned up data is valid when re-evaluated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/62a1b22e-3d8b-4241-bbf7-20145686ab33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Test Client response.json() UnicodeDecodeError

Hi,

I'm testing the REST API of my application and I found an error that I think that's a Django's issue.

I'm using:
  • Django==1.9.4
  • djangorestframework==3.3.3

I have set the rest framework setting UNICODE_JSON to True.

When i'm testing a JSON response that contains a special character like 'á' then I get a UnicodeDecodeError. This is the traceback:

Traceback (most recent call last):
  File ".../tests/assets/test_views.py", line 584, in test_get
    response.json()
  File ".../lib/python2.7/site-packages/django/utils/functional.py", line 13, in _curried
    return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
  File ".../lib/python2.7/site-packages/django/test/client.py", line 662, in _parse_json
    return json.loads(response.content.decode(), **extra)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 268: ordinal not in range(128)


If I change the line 662 in django/test/client.py to:

return json.loads(response.content.decode(response.charset), **extra)

then it works fine. In this case response.charset is utf-8.


Thanks,
Daniel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cf6e065-4bb9-401b-8017-951233890c51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wednesday, March 30, 2016

Re: Model share question

On 31/03/2016 8:38 AM, moon jo wrote:
> I'm new to django and have a question regarding model design.
> For practice, I'm working on a web app - a simplified version of imdb
> that handles movie and music.
> I've created 2 apps; Movies and Music. With main models in both having
> the usual fields (title, release date, rating, artist/actor).
> My problem is, I don't know where to put the Person model. I want it to
> be shared by both apps.
> Should there be an app for Person and put the model in there?

Short answer is yes. Long answer is yes.

Once you put the app 'person' into INSTALLED_APPS in settings in each of
the other two apps you can exploit 'person.Person' in foreign keys and
Django will find it. If you prefer using the person model directly you
do "from person.models import Person" then just use Person in foreign keys.

> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56FCC151.808%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

getting error while trying example part-4 in Django 1.9

NoReverseMatch: Reverse for 'vote' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

link to error

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/919273aa-7786-4e1a-a21d-01b13cb83710%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Q() chaining with & returns empty set

I've got a problem in using Q()


Let's say we have baskets of fruits. How to filter out those baskets that contains all fruits in a given basket?

In this document https://docs.djangoproject.com/en/dev/ref/models/querysets/#in 'in' method seems will return any basket which contains any of the given fruit. Is there any "set_contains" method to be used for filtering ? Such as Basket.objects.filter(fruit_set_containsAll=fruitSet) ?

Edit: I found that Q() is not working as expected. I'll post the test here:

class Basket(models.Model):      weight = models.FloatField(default=1)  class Fruitname(models.Model):      name = models.CharField(max_length=32)  class Fruit(models.Model):      ofkind = models.ForeignKey(Fruitname, on_delete=models.CASCADE)        inbasket = models.ForeignKey(Basket, on_delete=models.CASCADE)        weight = models.FloatField(default=1)

the database is set as 'apple' only in 1 basket, 'pear' in 2 baskets, and 'banana' in 3 basket:

print Basket.objects.all()  print Fruitname.objects.all()  [<Basket: id 31 has 4 fruits       1. id - 53 apple(28), weight 1.00      2. id - 54 apple(28), weight 2.00      3. id - 55 apple(28), weight 3.00      4. id - 62 banana(30), weight 10.00  >, <Basket: id 32 has 2 fruits       1. id - 56 pear(29), weight 4.00      2. id - 57 banana(30), weight 5.00  >, <Basket: id 33 has 4 fruits       1. id - 58 pear(29), weight 6.00      2. id - 59 banana(30), weight 7.00      3. id - 60 pear(29), weight 8.00      4. id - 61 pear(29), weight 9.00  >]  [<Fruitname: apple(28)>, <Fruitname: pear(29)>, <Fruitname: banana(30)>]

If I try to query with 'apple' and 'banana', it gives empty set !!

print Basket.objects.filter(Q(fruit__ofkind__name__in=['apple'])&Q(fruit__ofkind__name__in=['banana'])).distinct()  []

similarly,

print Basket.objects.filter(Q(fruit__ofkind__name__in=['banana'])&Q(fruit__ofkind__name__in=['pear'])).distinct()  []

Here's how I use 'in' to filter, it is not what I need which is supposed to be an empty set.

print Basket.objects.filter(Q(fruit__ofkind__name__in=['apple','pear'])).distinct()  [<Basket: id 31 has 4 fruits       1. id - 53 apple(28), weight 1.00      2. id - 54 apple(28), weight 2.00      3. id - 55 apple(28), weight 3.00      4. id - 62 banana(30), weight 10.00  >, <Basket: id 32 has 2 fruits       1. id - 56 pear(29), weight 4.00      2. id - 57 banana(30), weight 5.00  >, <Basket: id 33 has 4 fruits       1. id - 58 pear(29), weight 6.00      2. id - 59 banana(30), weight 7.00      3. id - 60 pear(29), weight 8.00      4. id - 61 pear(29), weight 9.00  >]

The only way that is working properly is chaining with filter:

Basket.objects.filter(fruit__ofkind__name__in=['apple']).filter(fruit__ofkind__name__in=['banana']).distinct()  [<Basket: id 31 has 4 fruits       1. id - 53 apple(28), weight 1.00      2. id - 54 apple(28), weight 2.00      3. id - 55 apple(28), weight 3.00      4. id - 62 banana(30), weight 10.00  >]

These code is tested with Django 1.9.4 Any explaining? I would undo the accepted answer for the moment.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87375b5f-4951-4f50-9bed-1984645658eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.