I have started creating a personal expenses django app to learn python...
So far, I have written some models for categories, transactions, scheduled
transactions (for example re-occuring bills), type of accounts etc. And I
am stuck :)
What I am trying to do is to display all transactions in an html page.
Those that re-occur, should be listed also, as my app displays all
transactions you have until 365 days from now. I can't manage to display
all instances of repeating transactions.
model
class Transaction(models.Model):
category = models.ForeignKey(Category)
kind = models.ForeignKey(Kind)
account = models.ForeignKey(Account)
userA = models.ForeignKey(User, related_name='userA')
userA_contribution = models.DecimalField(max_digits=9,
decimal_places=2)
userB = models.ForeignKey(User, related_name='userB', blank=True,
null=True)
userB_contribution = models.DecimalField(max_digits=9,
decimal_places=2, blank=True, null=True)
transaction_text = models.CharField(max_length=200)
transaction_date = models.DateTimeField('transaction date')
transaction_price = models.DecimalField(max_digits=9,
decimal_places=2)
def __str__(self):
return self.transaction_text
# calculate days since transaction has been made
def days_ago(self):
days_ago = timezone.now() - self.transaction_date
return days_ago.days
class ScheduledTransaction(Transaction):
REPEATING_CHOICES = (
('0', 'Once'),
('1', 'Daily'),
('2', 'Every other day',),
('7', 'Every week'),
('14', 'Every two weeks'),
('30', 'Monthly'),
('90', 'Quarterly'),
('180', 'Six months'),
('365', 'Annually'),
)
repeated = models.CharField(max_length=30,
choices=REPEATING_CHOICES)
def days_to_payment(self):
days_to_payment = self.transaction_date - timezone.now()
return days_to_payment.days
def cost_per_month(self):
cost_per_month = self.userA_contribution / int(self.repeated) *
30
return cost_per_month
controller
def index(request):
latest_transactions_list =
Transaction.objects.all().order_by('-transaction_date')[:20]
scheduled_transactions = ScheduledTransaction.objects.all()
# create a list to save scheduleTransaction.object instances
s_instances = []
for sche in scheduled_transactions:
s_instances.append(sche)
next_payment_days = int(sche.repeated) # next payment day is the
repeated value, e.g. = 30 days
base_trans_date = sche.transaction_date
while next_payment_days < 365:
sche.transaction_date = base_trans_date +
datetime.timedelta(days=next_payment_days)
s_instances.append(sche)
next_payment_days = next_payment_days + int(sche.repeated)
accounts_dash = Account.objects.all().order_by('-name')
date_today = datetime.datetime.now()
context = { 'latest_transactions_list': latest_transactions_list,
'scheduled_transactions' : s_instances,
'accounts_dash' : accounts_dash,
'date_today' : date_today,
}
return render(request, 'transactions/index.html', context)
view
{% for strans in scheduled_transactions %}
{% if strans.repeated|add:0 <= 365 %}
<tr>
<td class="">{{ strans.transaction_date }}</td>
<td class="text-center"><span class="label
label-default">{{ strans.kind }}</span></td>
<td class="text-center"><span class="label label-info">{{
strans.category }}</span></td>
<td class="text-center"><a href="{{ strans.id }}/">{{
strans.transaction_text }}</a></td>
<td class="text-right">{{
strans.userA_contribution|floatformat:2 }}€</td>
<td class="text-center"><a href="{{ trans.repeated }}">{{
strans.repeated }}</a></td>
<td class="text-center">{{ strans.days_to_payment }}</td>
<td class="text-right">{{
strans.cost_per_month|floatformat:2 }}€</td>
</tr>
{% endif %}
{% endfor %}
The transaction is printed in the terminal webserver shell, but not
displayed even once on the html.
Any help would be appreciated !
EDIT: Updated the controller. Now the iteration works, but I always get the
latest date and the total of days_to_payment in all instances. Any ideas ?
--
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 [email protected].
To post to this group, send email to [email protected].
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/a768bce4-3a0f-435c-aa5d-7f423e6c7f64%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.