Python Django重定向不工作

Python Django重定向不工作,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我可以看到问题,我附加了我的代码和错误页面 在我的模板中,我有: {% if user.get_profile.is_store %} <!--DO SOME LOGIC--> {%endif%} 环境: Request Method: GET Request URL: http://localhost:8000/professional/downloads Django Version: 1.1.1 Python Version: 2.6.

我可以看到问题,我附加了我的代码和错误页面

在我的模板中,我有:

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

    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',
我的错误报告:

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.
我的基本帐户类

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
我的账户类`

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)

`

它看起来像用户。get\u profile()返回值为空,因此在下一个is\u store()调用中失败:

这失败了,因为self是空的(即无)


[编辑]读取(在配置文件下)后,该用户的配置文件似乎不存在,因此您会得到DoesNotExist异常。

它看起来像该用户。get\u profile()返回值为空,因此在下一个is\u store()调用时失败:

这失败了,因为self是空的(即无)


[编辑]阅读(在配置文件下)后,该用户的配置文件似乎不存在,因此您会得到DoesNotExist异常。

self。帐户在试图处理
is\u store()
时指向一个不存在的
帐户对象。我猜您使用的数据库没有强制外键*cough*MySQL*cough*,您的数据被弄乱了。

self.account
在试图处理
is\u store()
时指向一个不存在的
account
对象。我猜您使用的数据库不强制使用外键*cough*MySQL*cough*,您的数据被弄乱了。

不起作用的不是重定向,而是您的is_store()过程或不存在的配置文件。您应该检查self.account引用的确切内容。
从代码中可以看出,您的数据库中有两个表—一个用于BaseCount,它只存储用户id,另一个用于account,它存储与account+BaseCount id关联的所有字段。您可以尝试用类似的内容替换is_store

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

我决不是Django专家,但我认为这可以解决问题。

不起作用的不是重定向,而是您的is_store()过程或不存在的配置文件。您应该检查self.account引用的确切内容。
从代码中可以看出,您的数据库中有两个表—一个用于BaseCount,它只存储用户id,另一个用于account,它存储与account+BaseCount id关联的所有字段。您可以尝试用类似的内容替换is_store

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


我决不是Django专家,但我认为这可以解决问题。

你有两个模板标记({%if…)吗以及模板中的实际下载视图?如果这些位位于不同的位置,请在问题中使用单独的代码块,否则看起来会非常混乱。抱歉,我不明白,重定向在下载视图中,并且在加载页面时检查是否完成。我错了吗?这可能没问题,但如果可以,则更好将显示的代码分为不同的代码块,否则看起来会很混乱。我们应该如何调试此代码块?您没有说明预期会发生什么。很明显,profile
is\u store
方法失败。您是否希望
authenticated\u user
调用在这之前重定向到别处?如果因此,发布该函数的代码。如果没有,发布
is_store
函数的代码。我期望的是,用户被重定向到站点的根目录,如果没有正确的用户类型,这就是is_store方法所做的,is_store应该确定用户的用户类型。如果用户尝试访问s页面,并且是商店用户,他们应该被重定向。您是否同时拥有模板标记({%if…)以及模板中的实际下载视图?如果这些位位于不同的位置,请在问题中使用单独的代码块,否则看起来会非常混乱。抱歉,我不明白,重定向在下载视图中,并且在加载页面时检查是否完成。我错了吗?这可能没问题,但如果可以,则更好将显示的代码分为不同的代码块,否则看起来会很混乱。我们应该如何调试此代码块?您没有说明预期会发生什么。很明显,profile
is\u store
方法失败。您是否希望
authenticated\u user
调用在这之前重定向到别处?如果因此,发布该函数的代码。如果没有,发布
is_store
函数的代码。我期望的是,用户被重定向到站点的根目录,如果没有正确的用户类型,这就是is_store方法所做的,is_store应该确定用户的用户类型。如果用户尝试访问s页面,并且是商店用户,他们应该被重定向。抱歉,我继承了此代码,而且我是Django新手,因此我正在边做边学,如何查看帐户引用的内容,我可以使用ObjectDoesNotExist做些什么吗?抱歉,我继承了此代码,并且我是Django新手,因此我正在边做边学,如何查看帐户引用的内容,我可以使用h如何处理ObjectDoesNotExist?堆栈跟踪显示
DoesNotExist
来自
is\u store()
方法,而不是
get\u profile()方法
method@jcd:说得好。如果sico87可以发布他的帐户配置文件对象的代码就好了。@tomlog,我已经输入了代码,并将该类添加到了我的原始类中post@sico87:self.account和self.professional在哪里定义?它们是否存在于您的BaseCount类中?是的,在BaseCount类中,它们是该co中的最后两个定义取消采样。堆栈跟踪显示
DoesNotExist
来自
is\u store()
方法,而不是
get\u profile()
method@jcd:说得好。如果sico87可以发布他的帐户配置文件对象的代码就好了。@tomlog,我已经输入了代码,并将该类添加到了我的原始类中post@sico87:self.account和self.professional在哪里定义?它们是否存在于BaseCount类中?是的,在BaseCount中
def is_store(self):
try: 
    return self.account is None 
except Account.DoesNotExist: 
    return False