Django调试404错误

时间:2015-11-24 10:03:51

标签: django

更新 电子邮件中的错误日志

Internal Server Error: /lifestyle/the-
Traceback (most recent call last):
  File "/home/kbuzz/lib/python2.7/django/core/handlers/base.py", line 149, in get_response
    callback, param_dict = resolver.resolve404()
  File "/home/kbuzz/lib/python2.7/django/core/urlresolvers.py", line 395, in resolve404
    return self._resolve_special('404')
  File "/home/kbuzz/lib/python2.7/django/core/urlresolvers.py", line 386, in _resolve_special
    return get_callable(callback), {}
  File "/home/kbuzz/lib/python2.7/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/kbuzz/lib/python2.7/django/core/urlresolvers.py", line 118, in get_callable
    (lookup_view, mod_name))
ViewDoesNotExist: Could not import common.views.search_404_view. View does not exist in module common.views.

common.views:

import datetime, calendar, re

from django.contrib.contenttypes.models import ContentType
from django.contrib.redirects.models import Redirect
from django.core.paginator import Paginator, InvalidPage
from django.db.models import Q
from django.http import Http404,  HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic import ListView, DetailView

from common.models import Area, Category, HotItem
from stats.models import FileNotFoundItem
from stats.models import StatItem

from articles.models import Article
from events.models import Event
from marketplace.models import Entry
from directory.models import Venue



class StatsDetailView(DetailView):
    message = ""
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()

        if not self.object.visible and self.object.created_by != self.request.user:
            raise Http404


        today = datetime.date.today()
        ct = ContentType.objects.get_for_model(self.object)
        try:
            si = StatItem.objects.get(
                date=today,
                content_type=ct,
                object_id=self.object.id,
            )
            si.hits = si.hits +1
            si.save()
        except StatItem.DoesNotExist:
            StatItem.objects.create(
                date=today,
                content_type=ct,
                object_id=self.object.id,
            )
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

class AreaList(ListView):
    model = Area
    context_object_name = 'areas'

    def get_queryset(self):
        return Area.objects.filter(parent=None).exclude(slug='not-found')

class AreaDetail(StatsDetailView):
    model = Area
    context_object_name = 'area'
    context_object = None

    def get_object(self):
        if self.kwargs.get('slug', None):
            slugs = self.kwargs['slug'].strip('/').split('/')
            self.context_object = get_object_or_404(Area, slug=slugs[-1])
            return self.context_object
        raise Http404

    def get_context_data(self, **kwargs):
        today = datetime.date.today()
        context = super(AreaDetail, self).get_context_data(**kwargs)
        area_ids = [a.id for a in self.context_object.get_child_areas()]
        area_ids.append(self.context_object.id)
        context['events'] = Event.objects.filter(venue__area__in=area_ids, visible=True).order_by('hot', '-created')[:10]
        context['entries'] = Entry.objects.filter(area__in=area_ids, visible=True).order_by('hot', '-created')[:10]
        context['venues'] = Venue.objects.filter(area__in=area_ids, visible=True).order_by('hot', '-created')[:10]
        context['areas'] = Area.objects.filter(parent=None, visible=True)
        return context

class CategoryList(ListView):
    model = Category
    context_object_name = 'categories'

    def get_queryset(self):
        return Category.objects.filter(parent=None).exclude(slug='not-found')

class CategoryDetail(StatsDetailView):
    model = Category
    context_object_name = 'category'
    context_object = None

    def get_object(self):
        if self.kwargs.get('slug', None):
            slugs = self.kwargs['slug'].strip('/').split('/')
            self.context_object = get_object_or_404(Category, slug=slugs[-1])
            return self.context_object
        raise Http404

    def get_context_data(self, **kwargs):
        today = datetime.date.today()
        context = super(CategoryDetail, self).get_context_data(**kwargs)
        category_ids = [c.id for c in self.context_object.get_child_categories()]
        category_ids.append(self.context_object.id)
        context['events'] = Event.objects.filter(categories__in=category_ids, visible=True).order_by('hot', '-created')[:10]
        context['entries'] = Entry.objects.filter(category__in=category_ids, visible=True).order_by('hot', '-created')[:10]
        context['venues'] = Venue.objects.filter(categories__in=category_ids, visible=True).order_by('hot', '-created')[:10]
        context['articles'] = Article.objects.filter(categories__in=category_ids, visible=True).order_by('hot', '-created')[:10]
        return context

我还没有找到视图,但我想重写视图。寻找404页面的示例视图。

我在网站上调试一个问题,我维护所以大部分代码都不是由我编写的。在像www.example.com/lifestyle/the-这样的链接上,当debug为False时会抛出内部服务器错误但是当我将debug设置为True时,我会收到Page Not Found错误。这是错误

VariableDoesNotExist: Failed lookup for key [top_menu] in u"[{'False': False, 'None': None, 'True': True}, {}]"

top_menu是网站上的顶级菜单。然而,它在网站上加载正常。 调用get object或404的视图是

context['page'] = get_object_or_404(Page, slug=slugs[0], parent=None)

和404模板

{% extends 'base.html' %}
{% block title %}Page Not Found {{ block.super }}{% endblock %}
{% block content %}
<div class="stack">
    <h1 class="stackContent">Page <span>Not</span> Found</span></h1>
    <div class="box stackContent">
        We are sorry but we can't find the page you tried to access.
    </div>
</div>
{% endblock content %}

我知道它用于返回建议页面列表的404页面,所以我相信它是使用haystack来做到这一点。所以我的猜测是有一个覆盖404电话的观点,我正在寻找如何做到这一点。

1 个答案:

答案 0 :(得分:1)

我在common.views中创建了这个视图并修复了问题

def search_404_view(request, template_name='404.html'):
    context = RequestContext(request)
    return render_to_response(template_name, context)