Re: Django tutorial - Misleading instructions

2017-06-29 Thread Tim Graham
The 'g_auth/templates' directory should be searched because of 'APP_DIRS': 
True. Is 'g_auth' in INSTALLED_APPS?

On Thursday, June 29, 2017 at 9:54:27 PM UTC-4, Justin Stubbs wrote:
>
> Hi Guys,
>
> Im following the tutorial 
> https://docs.djangoproject.com/en/1.11/intro/tutorial03/ and have found 
> that the section on templates doesn't appear to be correct.
>
> The section talks about how django templates are loaded by default if you 
> put them in the correct path:
>
>
>
> 
>
>
>
> def auth(request):
> return render(request, 'g_auth/index.html')
>
>
>
>
> but when I used a similar example django complained about not being able 
> to find the templates. I ended up having to change the django settings file 
> 'DIRS':
>
> TEMPLATES = [
> {
> 'BACKEND': 'django.template.backends.django.DjangoTemplates',
> 'DIRS': [
> os.path.join(BASE_DIR, 'g_auth/templates')
> ],
> 'APP_DIRS': True,
> 'OPTIONS': {
> 'context_processors': [
> 'django.template.context_processors.debug',
> 'django.template.context_processors.request',
> 'django.contrib.auth.context_processors.auth',
> 'django.contrib.messages.context_processors.messages',
> ],
> },
> },
> ]
>
>
> After reading 
> https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/
>
> Hoping to get this clarified as for a beginner this would be quite 
> complicated(assuming I haven't missed the boat completely here)
>
> Cheers,
>
> Justin
>

-- 
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+unsubscr...@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/013d969c-6a62-4ec9-bf38-f91b38ec6000%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django tutorial - Misleading instructions

2017-06-29 Thread Justin Stubbs
Hi Guys,

Im following the 
tutorial https://docs.djangoproject.com/en/1.11/intro/tutorial03/ and have 
found that the section on templates doesn't appear to be correct.

The section talks about how django templates are loaded by default if you 
put them in the correct path:






def auth(request):
return render(request, 'g_auth/index.html')




but when I used a similar example django complained about not being able to 
find the templates. I ended up having to change the django settings file 
'DIRS':

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'g_auth/templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]


After 
reading https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/

Hoping to get this clarified as for a beginner this would be quite 
complicated(assuming I haven't missed the boat completely here)

Cheers,

Justin

-- 
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+unsubscr...@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/d42b6488-73a9-4cc3-aeaf-2d9d00e5b124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: First models.py needs tuning

2017-06-29 Thread Rich Shepard

On Thu, 29 Jun 2017, Guilherme Leal wrote:


"An iterable (e.g., a list or tuple) consisting itself of iterables of
exactly two items"

I don't think that constructing choices as a "iterableof 1 item iterables"
will work.

Think of each item in the choices iterables as a key-value pair, where the
first item is the key (literally is the value that is saved on the DB
field), and the value is the "display value".


Guilherme,

  I missed the first sentence when I looked at the page. Your third
paragraph explains the syntax very clearly. I'll change the code to comply.

Thanks very much,

Rich


Re: First models.py needs tuning

2017-06-29 Thread Guilherme Leal
"An iterable (e.g., a list or tuple) consisting itself of iterables of
exactly two items"

I don't think that constructing choices as a "iterableof 1 item iterables"
will work.

Think of each item in the choices iterables as a key-value pair, where the
first item is the key (literally is the value that is saved on the DB
field), and the value is the "display value".

Em 29 de jun de 2017 7:14 PM, "Rich Shepard" 
escreveu:

> On Thu, 29 Jun 2017, Guilherme Leal wrote:
>
> It wouldn't be the case to use the field choices?
>> https://docs.djangoproject.com/en/1.11/ref/models/fields/#
>> django.db.models.Field.choices
>>
>
> Guilherme,
>
>   Now that I know Field.choices exists it will certainly do the job.
>
>   I read the example as translating a quoted string in two steps to a
> two-character, uppercase abbreviation. Would the following one-step list of
> choices be correct?
>
> industry_choices (
> ('Agriculture'),
> ('Business, other'),
> ('Chemicals'),
> ('Energy'),
> ('Law'),
> ('Manufacturing'),
> ('Mining'),
> ('Municipalities'),
> ('Ports/Marine Services'),
> ('Transportation'),
> )
> industry=models.CharField(max_length=24, choices=industry_choices,
> default='Agriculture')
>
> Thanks,
>
> Rich
>
>
>

-- 
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+unsubscr...@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/CAOs3Lp7HBbPEUBCQnpagDfqp7qt%3DfsYWpnRSqE2LkqDrx%2Bs7Og%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: First models.py needs tuning

2017-06-29 Thread Rich Shepard

On Thu, 29 Jun 2017, Guilherme Leal wrote:


It wouldn't be the case to use the field choices?
https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.Field.choices


Guilherme,

  Now that I know Field.choices exists it will certainly do the job.

  I read the example as translating a quoted string in two steps to a
two-character, uppercase abbreviation. Would the following one-step list of
choices be correct?

industry_choices (
('Agriculture'),
('Business, other'),
('Chemicals'),
('Energy'),
('Law'),
('Manufacturing'),
('Mining'),
('Municipalities'),
('Ports/Marine Services'),
('Transportation'),
)
industry=models.CharField(max_length=24, choices=industry_choices, 
default='Agriculture')

Thanks,

Rich




Re: Django ORM and TestCase

2017-06-29 Thread Jani Tiainen
Hi,

In TestCase Django wraps all tests within transaction and makes commit and
rollback as no-op.

So only code that is within transaction (your test) will see the data.

After test is finished a rollback is issued to remove all the data inserted
to db.

And there is no way to read that data outside transaction since its never
committed.


29.6.2017 22.01 "howie9393"  kirjoitti:

I was wondering how django.test.TestCase and the ORM interact. If I create
a databased backed object in a TestCase, the ORM can "see" the object when
I query for it, as expected.


from django.test import TestCase
class MyTests(TestCase):
def test_where_is_my_obj(self):
MyObj.objects.create(...)
self.assert(MyObj.objects.count(), 1) # this is true as expected


But let's say I put a breakpoint right before the assert statement. If I
were to connect to the test database directly using let's say "mysql -u
myuser -p test_database_that_django_made", the tables are empty. The MyObj
table has no rows. How does the ORM know about this uncommited object? And,
how can I see this object in the database shell?

Note: I can get the mysql shell to "see" this object if I use
TransactionTestCase instead of TestCase but TransactionTestCase is
extremely slow. Also, I'm just like to know how Django "sees" this object
in TestCase. 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+unsubscr...@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/41694eab-8800-49af-9124-6e241208d40d%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+unsubscr...@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/CAHn91oeEdPp95%2B2ZLigv8JEoBx7nJa-QGLvHUbiqaY421AUZSQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: First models.py needs tuning

2017-06-29 Thread Guilherme Leal
It wouldn't be the case to use the field choices?

https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.Field.choices

2017-06-29 18:07 GMT-03:00 Rich Shepard :

> On Wed, 19 Apr 2017, Mike Dewhirst wrote:
>
> Second, I have a couple of fields with postgres check constraints; that
>>> is, valid data is restricted to a list of strings. I've not found a
>>> Django model field validator teaching me how to write this.
>>>
>>
> Use the model's clean() method ...
>> https://docs.djangoproject.com/en/1.11/ref/models/instances/
>> #django.db.models.Model.clean
>>
>
>   What I see in Model.clean is a validation check which returns an error
> message to the user if the check fails. I understand using this in cases
> where required data are missing (such as the example of a draft article not
> having a blank publication date). What I don't see is how to write a
> validation check for an attribute whose value should be limited to a list.
>
>   Example: a table attribute (column) for 'industry' in which valid entries
> are limited to a list such as 'Agriculture', 'Business, other',
> 'Chemicals',
> 'Energy', 'Law').
>
>   In postgres SQL this would be a constraint check. How do I write this in
> a
> django Model.clean or its relatives?
>
> Rich
>

-- 
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+unsubscr...@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/CAOs3Lp6rtnJeT%3Do4QNE1EPKPwmiAUTvagyrEjJCyzg-wSYRDrg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: First models.py needs tuning

2017-06-29 Thread Rich Shepard

On Wed, 19 Apr 2017, Mike Dewhirst wrote:


Second, I have a couple of fields with postgres check constraints; that
is, valid data is restricted to a list of strings. I've not found a
Django model field validator teaching me how to write this.



Use the model's clean() method ...
https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.Model.clean


  What I see in Model.clean is a validation check which returns an error
message to the user if the check fails. I understand using this in cases
where required data are missing (such as the example of a draft article not
having a blank publication date). What I don't see is how to write a
validation check for an attribute whose value should be limited to a list.

  Example: a table attribute (column) for 'industry' in which valid entries
are limited to a list such as 'Agriculture', 'Business, other', 'Chemicals',
'Energy', 'Law').

  In postgres SQL this would be a constraint check. How do I write this in a
django Model.clean or its relatives?

Rich


Re: Pre-loading static data into database table

2017-06-29 Thread Rich Shepard

On Thu, 29 Jun 2017, John Thomas wrote:


419 is not that many. I'd probably do that via a Python shell. import
models.Cities for item in lists_of_lists_of_419 temp =
Cities(city+name=item[0], county_name=item[1]) temp.save()


John,

  I should have mentioned that the table does not currently exist in the
database. If the above is the preferred solution with an existing table,
then to add the table and date I can use the psql shell to read the file.

Thanks very much for clarifying,

Rich


Re: Pre-loading static data into database table

2017-06-29 Thread John Thomas
Hi Rich,

419 is not that many.  I'd probably do that via a Python shell.  import
models.Cities  for item in lists_of_lists_of_419  temp =
Cities(city+name=item[0], county_name=item[1]) temp.save()


John Thomas
310-947-8572

On Thu, Jun 29, 2017 at 11:16 AM, Rich Shepard 
wrote:

>   Reading
>  ions/#django.db.migrations.operations.RunSQL>
> tells me that runSQL allows arbitrary SQL to run on the database ... except
> for the postgres backend: "On most database backends (all but PostgreSQL),
> Django will split the SQL into individual statements prior to executing
> them."
>
>   I want to insert data into the two columns of the database. The model
> reads:
>
> class Cities (models.Model):
> city_name=models.CharField(max_length=24, unique=True)
> county_name=models.CharField(max_length=12, unique=True)
>
> and I have a list of 419 pairs of names I want to pre-load into this
> database table. The rows contain the two variables; e.g.,
>   Bend,Deschutes
>   Birkenfeld,Columbia
>   Blachly,Lane
>   Black Butte Ranch,Deschutes
>
>   So, using a postgres back end can I write,
>
>   migrations.RunSQL("INSERT INTO Cities (city_name,county_name) VALUES
> ('Bend','Dechutes');")
>
> repeated for each row in the table (easy to do with emacs)? And, can I put
> this code in models.py after the classes?
>
> Rich
>

-- 
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+unsubscr...@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/CAJ22gMA%3Dir3HGr3_7fjcWi9jxQ%2BaY8Rciies43MfD29D%3DxgVNA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Django ORM and TestCase

2017-06-29 Thread howie9393
I was wondering how django.test.TestCase and the ORM interact. If I create 
a databased backed object in a TestCase, the ORM can "see" the object when 
I query for it, as expected.


from django.test import TestCase
class MyTests(TestCase):
def test_where_is_my_obj(self):
MyObj.objects.create(...)
self.assert(MyObj.objects.count(), 1) # this is true as expected


But let's say I put a breakpoint right before the assert statement. If I 
were to connect to the test database directly using let's say "mysql -u 
myuser -p test_database_that_django_made", the tables are empty. The MyObj 
table has no rows. How does the ORM know about this uncommited object? And, 
how can I see this object in the database shell?

Note: I can get the mysql shell to "see" this object if I use 
TransactionTestCase instead of TestCase but TransactionTestCase is 
extremely slow. Also, I'm just like to know how Django "sees" this object 
in TestCase. 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+unsubscr...@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/41694eab-8800-49af-9124-6e241208d40d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django ORM and TestCase

2017-06-29 Thread howie9393
I was wondering how django.test.TestCase and the ORM interact. If I create 
a databased backed object in a TestCase, the ORM can "see" the object when 
I query for it, as expected.


from django.test import TestCase
class MyTests(TestCase):
def test_where_is_my_obj(self):
MyObj.objects.create(...)
self.assert(MyObj.objects.count(), 1) # this is true as expected


But let's say I put a break point right before the assert statement. If I 
were to connect to the test database directly using let's say "mysql -u 
myuser -p test_database_that_django_made", the myobj table is empty. How 
does the ORM know about this uncommited object? And, how can I see this 
object in the database shell?

Note: I can get mysql shell to "see" myobj if I use TransactionTestCase 
instead (which proves that TestCase isn't committing?) but it's extremely 
slow compared to TestCase.

-- 
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+unsubscr...@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/60a64019-e575-4c56-94df-87dd7464bb68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: recommendation for Django + Bootstrap

2017-06-29 Thread Ryan Nowakowski
I like starting with Pinax since it uses bootstrap by default.

http://pinaxproject.com/

On Tue, Jun 27, 2017 at 06:55:29PM -0300, Alceu Rodrigues de Freitas Junior 
wrote:
> Hello folks,
> 
> Could you please recommend any options available to integrate Django and
> Boostrap?
> 
> I find out projects like https://github.com/dyve/django-bootstrap3, but I'm
> new to Boostrap and need something very basic: my views/templates should
> render nicely in different sizes os screens (desktop, smartphones, etc).
> 
> Thanks!
> 
> -- 
> Alceu
> 
> 
>   
>   
> 
> -- 
> 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+unsubscr...@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/ae316257-4b43-4c96-c2b3-6237847e3e87%40gmail.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+unsubscr...@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/20170629182939.GV0%40fattuba.com.
For more options, visit https://groups.google.com/d/optout.


Pre-loading static data into database table

2017-06-29 Thread Rich Shepard

  Reading

tells me that runSQL allows arbitrary SQL to run on the database ... except
for the postgres backend: "On most database backends (all but PostgreSQL),
Django will split the SQL into individual statements prior to executing
them."

  I want to insert data into the two columns of the database. The model
reads:

class Cities (models.Model):
city_name=models.CharField(max_length=24, unique=True)
county_name=models.CharField(max_length=12, unique=True)

and I have a list of 419 pairs of names I want to pre-load into this
database table. The rows contain the two variables; e.g.,
  Bend,Deschutes
  Birkenfeld,Columbia
  Blachly,Lane
  Black Butte Ranch,Deschutes

  So, using a postgres back end can I write,

  migrations.RunSQL("INSERT INTO Cities (city_name,county_name) VALUES
('Bend','Dechutes');")

repeated for each row in the table (easy to do with emacs)? And, can I put
this code in models.py after the classes?

Rich


Re: Adding a lookup table

2017-06-29 Thread Rich Shepard

On Wed, 28 Jun 2017, Rich Shepard wrote:


Yes, that's the way to do it. I wrote my first idea before realizing that
two models/tables would be the way to go.


  After further consideration I realized that a single model/table is the
way to go. Because each city needs to be associated with a county a separate
model/table of county names adds nothing useful.

  Now I need to learn how django allows me to populate that table. The SQL
'INSERT INTO' statement can be appended to the CREATE TABLE command and both
read into the database using psql.

Thanks,

Rich




How to combine change_list_template in ModelAdmin mixin

2017-06-29 Thread ecas

I would like to create a mixin to extend the ModelAdmin class that adds a 
description text to the view, overriding the default change_list_template.

[admin_extra.py]
class DescriptionAdminMixin(object)
change_list_template = "description_change_list.html"
...

def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
extra_context.update({
'description': self.description,
})
return super(DescriptionAdminMixin, self).changelist_view(request, 
extra_context=extra_context)

[description_change_list.html]
{% extends "admin/change_list.html" %}
{% block object-tools %}
{% block description %}
{% if description %}{{ description }}{% endif %}
{% endblock description %}
{{ block.super }}
{% endblock %}


This works fine if used on a ModelAdmin, but it fails if used on an already 
subclassed ModelAdmin that also defines change_list_template, as it gets 
overwritten.


Ex. adding django-import-export mixin:

class MyModelAdmin(DescriptionAdminMixin, ExportMixin, ModelAdmin): 
...

where ExportMixin is defined as:

class ExportMixin(ImportExportMixinBase):
...
change_list_template = 'admin/import_export/change_list_export.html'
...

Is there a preferred way to combine both templates?


The solution I use for now is defining a parent_template in the context, 
and use that in the template to extend from it

[admin_extra.py]
class DescriptionAdminMixin(object)
change_list_template = "description_change_list.
...
def changelist_view(self, request, extra_context=None):
# Define parent object template, or use the default admin change 
list template
opts = self.model._meta
app_label = opts.app_label
parent_template_list = super(DescriptionAdminMixin, 
self).change_list_template or [
'admin/%s/%s/change_list.html' % (app_label, opts.model_name),
'admin/%s/change_list.html' % app_label,
'admin/change_list.html'
]
parent_template = 
SimpleTemplateResponse(None).resolve_template(parent_template_list)

extra_context = extra_context or {}
extra_context.update({
'description': self.description,
'parent_template': parent_template,
})
return super(DescriptionAdminMixin, self).changelist_view(request, 
extra_context=extra_context)

[description_change_list.html]
{% extends parent_template %}
{% block object-tools %}
{% block description %}
{% if description %}{{ description }}{% endif %}
{% endblock description %}
{{ block.super }}
{% endblock %}

Is there any better solution? Is there a way for the AdminObject to provide 
the default template, something like get_change_list_template() to avoid 
doing the template resolution? Or even better some built-in way of getting 
the template of the parent class?



-- 
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+unsubscr...@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/62cb6134-cae1-48a0-8938-41604553d07f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.