{% csrf_token %}

2017-09-26 Thread Alex Kleider
I'm using test driven development (going through Harry J.W. Percival's 
book) and have found that the following code fails because the template tag 
( {% csrf_token %} ) is rendered by the home_page view function but not by 
the django.template.loader.render_to_string function (and so the 
assertEqual test fails.)

...templates/home.html:
...

 
{% csrf_token %}
 
...

Testing code:

def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
returned_html = response.content.decode()
expected_html = render_to_string('home.html')
self.assertEqual(returned_html , expected_html)

returned_html and expected_html are the same except that returned_html 
contains the following line (and the other doesn't:)

Infact, expected_html doesn't even contain the
{% csrf_token %}
line.

Can anyone suggest a work around?
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+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/c424de1a-4866-4f29-b93f-c06f46651ea2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


different behaviour of datetime.datetime object before vs after save

2015-09-24 Thread Alex Kleider

I'm baffled by the fact that the __str__ method of an instance fails
before the instance is saved but works fine afterwards.

The relevant snippet from my models.py file:
class Journal(models.Model):
date = models.DateTimeField(default=timezone.now)
user = models.CharField(max_length=24)
description = models.CharField(max_length=256)
def __str__(self):
ret = ["  #{:0>3} on {:<12} by {}."
.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append("{}".format(line))
return '\n'.join(ret)

Here's the result of some experimentation using the shell:
(venv)alex@x301:~/Py/debk$ ./manage.py shell
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

from journal.models import Journal
j = Journal(user='Alex', description='Test entry')
j

Traceback (most recent call last):
  File "", line 1, in 
  File
"/home/alex/Py/venv/lib/python3.4/site-packages/django/db/models/base.py",
line 496, in __repr__
u = six.text_type(self)
  File "/home/alex/Py/debk/journal/models.py", line 22, in __str__
self.user)]
TypeError: non-empty format string passed to object.__format__

j.save()
j






I don't understand why saving would change the behavior of an instance.
I'd like my code to be able to display such an instance before a
decision is made whether or not to save it.
I'd be grateful if anyone could explain what's happening and perhaps
suggest a way to use the __str__ method before saving.

Thanks in advance for any help.

Alex K

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/be4d9342d8d31cc5236a38845b8a5103%40sonic.net.
For more options, visit https://groups.google.com/d/optout.


Re: starting afresh

2015-09-23 Thread Alex Kleider

On 2015-09-22 19:04, James Schneider wrote:

Did you delete the previously created migration files?

.
There should be a 'migrations' folder that was created inside of your 
app.

You can whack the whole folder, it will be created next time you run
'makemigrations'.


Indeed, it is there!  (I could have sworn that I had checked but
I must have only looked in the project folder and thought I had
looked in both it and the app folder.
Thank you again and sorry to have bothered.
cheers,
Alex

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4c023a21319099ef88be6cf61ab338e7%40sonic.net.
For more options, visit https://groups.google.com/d/optout.


Re: starting afresh

2015-09-22 Thread Alex Kleider

On 2015-09-22 18:12, James Schneider wrote:



Did you delete the previously created migration files? Django won't ask
those questions unless previous migration files exist. If you're 
starting

with a fresh database, you probably should.

-James


Ah, ha! That must be it.  Much appreciate the tip.
...but the problem is I can't find the migration files in order to 
delete them.

Where are they?
A google search didn't get me far.
Thanks, Alex

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/baf7f7495fcaf03609ae5eb67a929411%40sonic.net.
For more options, visit https://groups.google.com/d/optout.


Re: starting afresh

2015-09-22 Thread Alex Kleider

On 2015-09-22 17:29, Nikolas Stevenson-Molnar wrote:

It's telling you that your default "date" value will be the same for
every instance of "Journal" you create (essentially it'll be the time
when the migration is created), when you probably intend it to be the
time each Journal entry is created.

Try changing default=timezone.now() to default=timezone.now (without
the parens). That should resolve the warning and yield more desirable
behavior. Even better, remove the default and add
auto_now_add=True 
(https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.DateField.auto_now_add)



Thank you,_Nik.
That did indeed resolve this issue and I am grateful to you.

Now I have another: I'm unable to make django forget what I've done 
before!


I still get the following error:
"""
 (venv)alex@x301:~/Py/debk$ ./manage.py makemigrations
Did you rename lineitems.acount to lineitems.dc_type (a CharField)? 
[y/N] y
You are trying to add a non-nullable field 'acnt_number' to lineitems 
without a default; we can't do that (the database needs something to 
populate existing rows).

Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:
"""
As mentioned before, I deleted my data base but the error persists

Here's journal/models.py currently:
from django.db import models

ACCOUNT_CODE_LENGTH = 4

class Journal(models.Model):
"""
Each instance represents a journal entry to be stored in the
Journal table.  The automatically created auto-incrementing
primary key serves as the entry number.
"""
date = models.DateTimeField(auto_now_add=True)
user = models.CharField(max_length=24)
description = models.CharField(max_length=256)
def __str__(self):
ret = ["  #{:0>3} on {:<12} by {}."
.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append("{}".format(line))
return '\n'.join(ret)

class LineItems(models.Model):
entry = models.ForeignKey(Journal)
acnt_number = models.CharField(max_length=ACCOUNT_CODE_LENGTH)
dc_type = models.CharField(max_length=1)  # D(ebit or C(redit
amount = models.IntegerField()  # Pennies i.e. $nn.dd * 100
def __str__(self):
if self.dc_type.upper() == 'D':
return ("{:>8}:{:>10,.2f}Dr"
.format(self.acnt_number, self.amount/100.0))
elif self.dc_type.upper() == 'C':
return ("{:>8}:{:>20,.2f}Cr"
.format(self.acnt_number, self.amount/100.0))
else:
return ("Huston, we have a problem!")


How does Django even know what was in this file before, let alone why 
should it now care?



--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fcf47bd983b673fca2c308d4f3f63b22%40sonic.net.
For more options, visit https://groups.google.com/d/optout.


starting afresh

2015-09-22 Thread Alex Kleider

Although at the intermediate level with regard to Python,
I'm just beginning with Django, running Ubuntu 14.4LTS
in a venv established by
$ virtualenv -p python3 venv

I set up a journal app with a models.py file and then
changed some of the fields.  Now I can't seem to get rid of
the following warning:
WARNINGS:
journal.Journal.date: (fields.W161) Fixed default value provided.
	HINT: It seems you set a fixed date / time / datetime value as default 
for this field. This may not be what you want. If you want to have the 
current date as default, use `django.utils.timezone.now`


I deleted the db.sqlite3 file thinking that would allow me to start 
fresh but somehow django remembers what was there before although it's 
not there now:

file journal/models.py:
from django.db import models
from django.utils import timezone

class Journal(models.Model):
"""
Each instance represents a journal entry to be stored in the
Journal table.  The automatically created auto-incrementing
primary key serves as the entry number.
"""
date = models.DateTimeField(default=timezone.now())
user = models.CharField(max_length=24)
description = models.CharField(max_length=256)
def __str__(self):
ret = ["  #{:0>3} on {:<12} by {}."
.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append("{}".format(line))
return '\n'.join(ret)

Any advice would be appreciated.
Alex

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/25bfa6241296e00dca1e76be74a85e04%40sonic.net.
For more options, visit https://groups.google.com/d/optout.