Thursday, August 25, 2016

Attribute error, with a very basic banking app

{% csrf_token %} Your details:
Username:

Password:
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
#from django.template import loader
from django.views import generic

from .models import Person, Account1

# Create your views here.
def deposit(request):
selected_rbox = request.POST.get('Account1')
selected_rbox = request.POST.get('Account2')
selected_amount = request.POST.get('Amount')
person = Person
account1 = Account1
context = {'account1':account1,'person':person}
template = loader.get_template('banking/deposit.html')
return HttpResponse(template.render(context,request))

def hello(request):
person = Person
account1 = Account1
context = {'account1':account1,'person':person}
#template = loader.get_template('banking/login.html')
#return HttpResponse(template.render(context,request))
return render(request, 'banking/login.html')
#return render(request, 'banking/login.html', {
# 'person': person,
# 'error_message': "You didn't select a choice.",
# })

def login(request):
#account1 = get_object_or_404(Account1)
#person = get_object_or_404(Person)
account1 = Account1
person = Person
try:
selected_login = request.POST.get('login')
selected_username = request.POST.get('username')
#selected_login = person.choice_set.get(pk=request.POST['login'])
#selected_password = person.choice_set.get(pk=request.POST['password'])

# What is Choice object doing in the line below?
#except (KeyError, Choice.DoesNotExist):
except (KeyError, Account1.DoesNotExist):
#except (KeyError, Account1.DoesNotExist):
# Redisplay the question voting form.
#account1 = get_object_or_404(Account1)
#person = get_object_or_404(Person)
#person = Person
#account1 = Account1
#return render(request, 'banking/login.html', {
#'person': person,
#'error_message': "You didn't select a choice.",
#return render(request, 'banking/login.html')
return render(request, 'banking/login.html', {
'person': person,
'error_message': "You didn't select a choice.",
})

#person = Person
#account1 = Account1
#context = {'account1':account1,'person':person}
#template = loader.get_template('banking/login.html')
#return HttpResponse(template.render(context,request)
else:
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
#person = Person
#account1 = Account1
#context = {'account1':account1,'person':person}
#template = loader.get_template('banking/welcome.html')
#return HttpResponse(template.render(context,request))
return HttpResponseRedirect(reverse('banking:welcome'))


def transfer(request):
person = Person
account1 = Account1
context = {'account1':account1,'person':person}
template = loader.get_template('banking/transfer.html')
return HttpResponse(template.render(context,request))

def welcome(request):
person = Person
account1 = Account1
context = {'account1':account1,'person':person}
template = loader.get_template('banking/welcome.html')
return HttpResponse(template.render(context,request))

def withdraw(request):
person = Person
account1 = Account1
context = {'account1':account1,'person':person}
template = loader.get_template('banking/withdraw.html')
return HttpResponse(template.render(context,request))
from django.conf.urls import url

from . import views

app_name = 'banking'
urlpatterns = [
# ex: /banking/deposit
url(r'^deposit/$', views.DepositView.as_view(), name='deposit'),
# ex: /banking/
url(r'^$', views.HelloView.as_view(), name='hello'),
# ex: /banking/login
#url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^login/$', views.login, name='login'),
# ex: /banking/transfer
url(r'^transfer/$', views.TransferView.as_view(), name='transfer'),
# ex: /banking/withdraw
url(r'^withdraw/$', views.WithdrawView.as_view(), name='withdraw'),
# ex: /banking/welcome
url(r'^welcome/$', views.WelcomeView.as_view(), name='welome'),
]
from django.db import models

# Create your models here.
class Person(models.Model):
first_name = models.CharField(max_length=4,default="Bob")
last_name = models.CharField(max_length=6,default="Smith")
login_name = models.CharField(max_length=3,default="me")
password = models.CharField(max_length=7, default="password")
person_text = models.CharField(max_length=10, default="Bob Smith")

def get_first_name(self):
return self.first_name

def get_last_name(self):
return self.last_name

def get_login_name(self):
return self.login_name

def get_password(self):
return self.password

def __str__(self):
return self.person_text

class Account1(models.Model):
account_number = models.IntegerField(default=12345678)
bank_balance = models.DecimalField(max_digits=4, decimal_places=2, default=12.99)
interest_rate = models.IntegerField(default=2)
account_name = models.CharField(max_length=16, default="Current Account")
person = models.ForeignKey(Person, on_delete=models.CASCADE, default="")

def get_account_number(self):
return self.account_number

def get_bank_balance(self):
return self.bank_balance

def deposit(self, amount):
self.bank_balance = self.bank_balance + amount
return

def get_interest_rate(self):
return self.interest_rate

def withdraw(self, amount):
self.bank_balance = self.bank_balance - amount
return

#def __str__(self):
# return self.account_name

I've beem working on a simple banking app based on the Django tutorial. Thanks to your help it almost works now. It was working using templates but after making some changes to get HttpResponseRedirect to work I changed what was in the urls file like it shows in the tutorial. Now, the server doesn't run. It says there's an attribute error. I've temporarily left the user name and password in at the moment. I had a look at their tutorial how to do a use the Django authentication system (thanks for telling me about that) and I'm going to change that after I understand what's going on here. It's amazing how much you can do with Django. Do you think with time you get more used to what different errors mean? The errors seem new and confusing to me at the moment. Any help would be very much appreciated. Thanks in advnace.

--
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/b30091ce-fcbf-461e-869e-bba72eb9dcfe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment