异常值:Django基于类的View对象没有属性' campaignId'

时间:2014-11-27 20:18:28

标签: python django

我被困在这几个小时;我可能遗漏了一些简单的东西,但我不确定。非常感谢任何帮助。

我最近将Django项目的数据库从MongoDB更改为Neo4J。在迁移过程中,我正在修改一些视图,URL等。

我看到的追溯:

Exception Value: 'TweetListView' object has no attribute 'campaignId'

以下是完整的追溯和相关代码:https://gist.github.com/joefusaro/820270c1ff0999d3d3fd

来自campaign_list.html

<a href="{% url 'campaigns:tweets:tweet_list' campaignId=campaign.campaignId %}" type="button" class="btn btn-default"></a>

广告系列/ urls.py

from django.conf.urls import patterns, url, include

urlpatterns = patterns(
    '',

    url(
        r'^(?P<campaignId>\d+?)/tweets/',
        include('providers.twitter.urls', namespace='tweets')
    ),

叽叽喳喳/ urls.py

from django.conf.urls import patterns, url

from .views import TweetListView


urlpatterns = patterns(
    '',
    url(
        r'^list/$',
        TweetListView.as_view(),
        name='tweet_list'
    ),

叽叽喳喳/ views.py

class TweetListView(LoginRequiredMixin, TemplateView): # CampaignOwnerPermission,
    '''Displays TwitterPosts relevant to a Campaign; The campaignId variable
    is passed to get_object request method from the campaign_list page.'''

    template_name = 'twitter/stream.html'
    paginate_by = settings.PAGINATE_BY_VIEWS
    context_object_name = 'tweets'
    page_kwarg = 'page'
    object = None

    def get_object(self):
        if self.object is None:
            # Return all TwitterPosts for Campaign designated by campaignId
            self.object = GetTwitterPostsForCampaign(self.campaignId)

        return self.object

    def paginate_queryset(self, object, store, page_size):
        paginator = TweetPaginator(
            object, store, page_size
        )

        page = self.request.GET.get(self.page_kwarg) or 1

        try:
            page_number = int(page)
        except ValueError:
            if page == 'last':
                page_number = paginator.num_pages
            elif page == 'first':
                page_number = 1
            else:
                raise Http404(_('Page is not found.'))
        try:
            page = paginator.page(page_number)
            return (paginator, page, page.object_list, page.has_other_pages())
        except InvalidPage as e:
            raise Http404(
                _('Invalid page (%(page_number)s): %(message)s') % {
                    'page_number': page_number,
                    'message': str(e)
                }
            )

    def get_context_data(self, **kwargs):
        object = self.get_object()
        page_size = self.paginate_by
        context_object_name = self.context_object_name

        if page_size:
            paginator, page, queryset, is_paginated = self.paginate_queryset(
                object, self.kwargs['store'], page_size
            )
            context = {
                'paginator': paginator,
                'page_obj': page,
                'is_paginated': is_paginated,
                'object_list': queryset
            }
        else:
            raise InvalidPage

        if context_object_name is not None:
            context[context_object_name] = queryset

        context.update(kwargs)

        context['campaign'] = self.campaign

        return super(TweetStoreListView, self).get_context_data(**context)

1 个答案:

答案 0 :(得分:0)

如果您发布了完整的回溯,那将会更容易,这将显示错误发生的确切位置。

然而,它可能发生在你的视图的get_object方法中,其第三行应该是:

self.object = GetTwitterPostsForCampaign(self.kwargs['campaignId'])