views:

234

answers:

3

I can see the problem, I attached my code and error page.

In my template, I have:

{% if user.get_profile.is_store %}
    <!--DO SOME LOGIC-->
{%endif%}

In my view, I have:

def downloads(request):
"""
Downloads page, a user facing page for the trade members to downloads POS etc
"""
if not authenticated_user(request):
        return HttpResponseRedirect("/professional/")

if request.user.get_profile().is_store():
        return HttpResponseRedirect("/")

user = request.user
account = user.get_profile()

downloads_list = TradeDownloads.objects.filter(online=1)[:6]
downloads_list[0].get_thumbnail()
data = {}
data['download_list'] = downloads_list

return render_to_response('downloads.html', data, RequestContext(request))

Environment:

    Request Method: GET
    Request URL: http://localhost:8000/professional/downloads
    Django Version: 1.1.1
    Python Version: 2.6.2
    Installed Applications:
    ['django.contrib.auth',
     'django.contrib.admin',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
     'sico.news',
     'sico.store_locator',
     'sico.css_switch',
     'sico.professional',
     'sico.contact',
     'sico.shop',
     'tinymce',
     'captcha']
    Installed Middleware:
    ('django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',

My error report:

Traceback:
    File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
      92.                 response = callback(request, *callback_args, **callback_kwargs)
    File "/var/www/sico/src/sico/../sico/professional/views.py" in downloads
      78.   if request.user.get_profile().is_store():
    File "/var/www/sico/src/sico/../sico/shop/models.py" in is_store
      988.         return not self.account is None
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py" in __get__
      191.             rel_obj = self.related.model._base_manager.get(**params)
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py" in get
      120.         return self.get_query_set().get(*args, **kwargs)
    File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in get
      305.                     % self.model._meta.object_name)

    Exception Type: DoesNotExist at /professional/downloads
    Exception Value: Account matching query does not exist.

My BaseAccount Class

class BaseAccount(models.Model):
user = models.ForeignKey(User, unique=True)

def __unicode__(self):
    """
    Return the unicode representation of this customer, which is the user's
    full name, if set, otherwise, the user's username
    """
    fn = self.user.get_full_name()
    if fn:
        return fn
    return self.user.username

def user_name(self):
    """
    Returns the full name of the related user object
    """
    return self.user.get_full_name()

def email(self):
    """
    Return the email address of the related user object
    """
    return self.user.email

def is_store(self):
    return not self.account is None

def is_professional(self):
    return not self.professional is None

My Account class`

lass Account(BaseAccount):
"""
The account is an extension of the Django user and serves as the profile
object in user.get_profile() for shop purchases and sessions
"""
telephone = models.CharField(max_length=32)
default_address = models.ForeignKey(Address, related_name='billing_account', blank=True, null=True)
security_question = models.ForeignKey(SecurityQuestion)
security_answer = models.CharField(max_length=200)
how_heard = models.CharField("How did you hear about us?", max_length=100)
feedback = models.TextField(blank=True)
opt_in = models.BooleanField("Subscribe to mailing list", help_text="Please tick here if you would like to receive updates from %s" % Site.objects.get_current().name)
temporary = models.BooleanField()

def has_placed_orders(self):
    """
    Returns True if the user has placed at least one order, False otherwise
    """
    return self.order_set.count() > 0

def get_last_order(self):
    """
    Returns the latest order that this customer has placed. If no orders
    have been placed, then None is returned
    """
    try:
        return self.order_set.all().order_by('-date')[0]
    except IndexError:
        return None

def get_currency(self):
    """
    Get the currency for this customer. If global currencies are enabled
    (settings.ENABLE_GLOBAL_CURRENCIES) then this function will return
    the currency related to their default address, otherwise, it returns
    the site default
    """
    if settings.ENABLE_GLOBAL_CURRENCIES:
        return self.default_address.country.currency
    return Currency.get_default_currency()
currency = property(get_currency)

def get_gateway_currency(self):
    """
    Get the currency that an order will be put through protx with. If protx
    currencies are enabled (settings.ENABLE_PROTX_CURRENCIES), then the
    currency will be the same returned by get_currency, otherwise, the
    site default is used
    """
    if settings.ENABLE_PROTX_CURRENCIES and settings.ENABLE_GLOBAL_CURRENCIES:
        return self.currency
    return Currency.get_default_currency()
gateway_currency = property(get_gateway_currency)

`

A: 

It looks like the user.get_profile() return value is empty, and therefore it fails on the next is_store() call:

return not self.account is None

That fails, because self is empty (i.e. None).

[edit] After reading this (under Profiles) it looks like the profile for that user does not exist and therefore you get the DoesNotExist exception.

tomlog
Except the stack trace shows that the `DoesNotExist` is coming off of the `is_store()` method, not the `get_profile()` method
jcdyer
What does that mean?
sico87
@jcd: good point. It would be good if sico87 can post the code of his Account profile object.
tomlog
@tomlog, I have enter code have added the class to my original post
sico87
@sico87: where are self.account and self.professional defined? Do they exist in your BaseAccount class?
tomlog
Yeah in the BaseAccount class they are the last 2 definitions in that code sample.
sico87
Thats not true bare with me
sico87
I beleive account is reference to the User model(?) and self.professional is no where to be seen at the moment
sico87
@sico87 -- The stack trace is the long error message that you got. It's also called the traceback. If you read it carefully, it will tell you everything that was happening at the time of the error. The top line was calling the second line, which was calling the third line, and so on. Some of them are in the django framework, and some are in your code. The ones that are in your code will tell you where your error happened. This is not always exactly where your error is, but it's a good place to start looking.
jcdyer
+1  A: 

self.account is pointing at a non-existent Account object when it's trying to process is_store(). I would guess you are using a database that doesn't enforce foreign keys *cough*MySQL*cough*, and your data got messed up.

jcdyer
A: 

It's not redirect that is not working- it's your is_store() procedure or non-existing profile. You should check what exactly is self.account referring.
As far as I can tell from looking at code, you have two tables in your database- one for baseaccount, which only stores user id, and account, which stores all fields associated with account + baseaccount id. You can try to replace is_store with something like this

def is_store(self):
try: 
    return self.account is None 
except Account.DoesNotExist: 
    return False

I am by no means an Django expert, but I think this could do the trick.

fest
Sorry I have inherited this code and I new to Django so I am learning by doing, how see what Account refers to, could I do with do something with ObjectDoesNotExist?
sico87