2

我正在创建一个运行 virtualenv 的简单 Django 应用程序(1.3.1)。它已在 WebFaction 上启动并运行。只需要使用 Flatpages 就足够简单了——我仍然想对 FlatPages 进行一些增强,所以我只是扩展了模型——没什么复杂的。

模型.py

from django.db import models
from django.contrib.flatpages.models import FlatPage

class ExtendedFlatPage(FlatPage):
    order = models.PositiveIntegerField(unique=True)

    class Meta:
        ordering = ["order"]
        verbose_name = "Page"

问题与注销有关。当我尝试从该站点的入口点注销时,没有问题,即 http://{domainname}/admin/logout/ 但是,当我尝试从管理站点的更远位置注销系统时 - 一个示例位于扩展 FlatPages 列表中 http://{domainname}/admin/basic/extendedflatpage/ 然后注销 (http://{domainname}/admin/basic/extendedflatpage/logout/) 会生成错误,归结为最后的观察

ValueError: int() 以 10 为底的无效文字:'logout'

条目底部的完整追溯。

网址.py

from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^favicon\.ico$', 'django.views.generic.simple.redirect_to', {'url': '/static/images/favicon.ico'}),
)

在互联网上四处搜寻带回了以下页面,并提出了修复建议。问题是,我似乎无法让它工作 - 我试图实现它的方式是调整 urls.py 如下:

...
admin.autodiscover()
admin.site.root_path = '/admin/' #should this go here?
urlpatterns = patterns('',
...

追溯

Traceback (most recent call last):

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/contrib/admin/options.py", line 307, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/utils/decorators.py", line 93, in _wrapped_view
    response = view_func(request, *args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 197, in inner
    return view(request, *args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/transaction.py", line 217, in inner
    res = func(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/reversion/revisions.py", line 279, in do_revision_context
    return func(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/reversion/admin.py", line 362, in change_view
    return super(VersionAdmin, self).change_view(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/utils/decorators.py", line 28, in _wrapper
    return bound_func(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/utils/decorators.py", line 93, in _wrapped_view
    response = view_func(request, *args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/utils/decorators.py", line 24, in bound_func
    return func(self, *args2, **kwargs2)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/transaction.py", line 217, in inner
    res = func(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/contrib/admin/options.py", line 947, in change_view
    obj = self.get_object(request, unquote(object_id))

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/contrib/admin/options.py", line 451, in get_object
    return queryset.get(pk=object_id)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/query.py", line 341, in get
    clone = self.filter(*args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/query.py", line 550, in filter
    return self._filter_or_exclude(False, *args, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/query.py", line 568, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1194, in add_q
    can_reuse=used_aliases, force_having=force_having)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1129, in add_filter
    connector)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/sql/where.py", line 67, in add
    value = obj.prepare(lookup_type, value)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/sql/where.py", line 316, in prepare
    return self.field.get_prep_lookup(lookup_type, value)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/fields/related.py", line 136, in get_prep_lookup
    return self._pk_trace(value, 'get_prep_lookup', lookup_type)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/fields/related.py", line 209, in _pk_trace
    v = getattr(field, prep_func)(lookup_type, v, **kwargs)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 292, in get_prep_lookup
    return self.get_prep_value(value)

  File "/home/niceguydave/.virtualenvs/iwama/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 479, in get_prep_value
    return int(value)

ValueError: invalid literal for int() with base 10: 'logout'


<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'csrftoken': '7cad0c7ee5b93f897e82934aac3f9224',
 'sessionid': 'b7440ccc530704ca15b315f8f1f9f7ed'},
META:{'CSRF_COOKIE': '7cad0c7ee5b93f897e82934aac3f9224',
 'DOCUMENT_ROOT': '/usr/local/apache2/htdocs',
 'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
 'HTTP_ACCEPT_LANGUAGE': 'en',
 'HTTP_CONNECTION': 'close',
 'HTTP_COOKIE': 'sessionid=b7440ccc530704ca15b315f8f1f9f7ed; csrftoken=7cad0c7ee5b93f897e82934aac3f9224',
 'HTTP_HOST': 'www.iwamaaikidocornwall.com',
 'HTTP_HTTPS': 'off',
 'HTTP_HTTP_X_FORWARDED_PROTO': 'http',
 'HTTP_REFERER': 'http://www.iwamaaikidocornwall.com/admin/basic/extendedflatpage/',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/4.1.3 Safari/533.19.4',
 'HTTP_X_FORWARDED_FOR': '89.241.106.48',
 'HTTP_X_FORWARDED_HOST': 'www.iwamaaikidocornwall.com',
 'HTTP_X_FORWARDED_PROTO': 'http',
 'HTTP_X_FORWARDED_SERVER': 'www.iwamaaikidocornwall.com',
 'HTTP_X_FORWARDED_SSL': 'off',
 'PATH_INFO': u'/admin/basic/extendedflatpage/logout/',
 'PATH_TRANSLATED': '/home/niceguydave/webapps/iwama/production.wsgi/admin/basic/extendedflatpage/logout/',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '40468',
 'REQUEST_METHOD': 'GET',
 'REQUEST_URI': '/admin/basic/extendedflatpage/logout/',
 'SCRIPT_FILENAME': '/home/niceguydave/webapps/iwama/production.wsgi',
 'SCRIPT_NAME': u'',
 'SERVER_ADDR': '127.0.0.1',
 'SERVER_ADMIN': '[no address given]',
 'SERVER_NAME': 'www.iwamaaikidocornwall.com',
 'SERVER_PORT': '80',
 'SERVER_PROTOCOL': 'HTTP/1.0',
 'SERVER_SIGNATURE': '',
 'SERVER_SOFTWARE': 'Apache/2.2.17 (Unix) mod_wsgi/3.3 Python/2.7.3',
 'mod_wsgi.application_group': 'web318.webfaction.com|',
 'mod_wsgi.callable_object': 'application',
 'mod_wsgi.handler_script': '',
 'mod_wsgi.input_chunked': '0',
 'mod_wsgi.listener_host': '',
 'mod_wsgi.listener_port': '47425',
 'mod_wsgi.process_group': 'iwama',
 'mod_wsgi.request_handler': 'wsgi-script',
 'mod_wsgi.script_reloading': '1',
 'mod_wsgi.version': (3, 3),
 'wsgi.errors': <mod_wsgi.Log object at 0x7f5a450c8f30>,
 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x7f5a44eb2828>,
 'wsgi.input': <mod_wsgi.Input object at 0x7f5a44e833b0>,
 'wsgi.multiprocess': True,
 'wsgi.multithread': True,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 1)}>

2012 年 5 月 7 日更新

我在此处的帖子中进行了一些观察,并将其应用于应用程序以进行修复,并在我的扩展 Flatpage 应用程序中创建了一个新视图

视图.py

def logout(request):
    from django.contrib.auth.views import logout
    return logout(request)

然后我将此视图导入 urls.py 并调用它

网址.py

...
from basic.views import logout as custom_logout
...
urlpatterns = patterns('',
    url(r'/logout/$', custom_logout),
    url(r'^admin/', include(admin.site.urls)),
...

虽然这现在解决了我的问题,但似乎我在某种程度上“修补”了一些东西。

有人可以建议一种更简洁的方法吗?或者我在这里应用的补丁似乎是合理的做法?

4

0 回答 0