首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >派生django-oscar视图显示错误

派生django-oscar视图显示错误
EN

Stack Overflow用户
提问于 2020-04-14 21:55:39
回答 1查看 83关注 0票数 0

我派生了客户应用程序,以便在http://oscar/accounts/...(example中添加一个选项卡来编辑/显示目录视图(Dashboard>Catalogue)

我使用该视图的错误是

代码语言:javascript
复制
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我使用具有相同方法的视图进行付款结账,但这一次遇到了错误。

代码语言:javascript
复制
# yourappsfolder.customer.apps.py
import oscar.apps.customer.apps as apps
from oscar.apps.dashboard.catalogue import apps as dapps
from django.views import generic
from django.conf.urls import url
from oscar.core.loading import get_class
from .views import ProductListView

class CustomerConfig(apps.CustomerConfig):
    name = 'yourappsfolder.customer'

    def ready(self):
        super().ready()
        self.extra_view =ProductListView

    def get_urls(self):
        urls = super().get_urls()
        urls += [
            url(r'products/',self.extra_view.as_view(),name='Products'),
        ]
        return self.post_process_urls(urls)

这是我从oscar.apps.dashboard.catalogue复制的视图

代码语言:javascript
复制
# yourappsfolder.customer.views

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from django.views import generic 

from oscar.apps.dashboard.catalogue.views import ProductListView as UserProductListView


class ProductListView(UserProductListView):

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['form'] = self.form
        ctx['productclass_form'] = self.productclass_form_class()
        return ctx

    def get_description(self, form):
        if form.is_valid() and any(form.cleaned_data.values()):
            return _('Product search results')
        return _('Products')

    def get_table(self, **kwargs):
        if 'recently_edited' in self.request.GET:
            kwargs.update(dict(orderable=False))

        table = super().get_table(**kwargs)
        table.caption = self.get_description(self.form)
        return table

    def get_table_pagination(self, table):
        return dict(per_page=20)

    def filter_queryset(self, queryset):
        """
        Apply any filters to restrict the products that appear on the list
        """
        return filter_products(queryset, self.request.user)

    def get_queryset(self):
        """
        Build the queryset for this list
        """
        queryset = Product.objects.browsable_dashboard().base_queryset()
        queryset = self.filter_queryset(queryset)
        queryset = self.apply_search(queryset)
        return queryset

    def apply_search(self, queryset):
        """
        Search through the filtered queryset.

        We must make sure that we don't return search results that the user is not allowed
        to see (see filter_queryset).
        """
        self.form = self.form_class(self.request.GET)

        if not self.form.is_valid():
            return queryset

        data = self.form.cleaned_data

        if data.get('upc'):
            # Filter the queryset by upc
            # For usability reasons, we first look at exact matches and only return
            # them if there are any. Otherwise we return all results
            # that contain the UPC.

            # Look up all matches (child products, products not allowed to access) ...
            matches_upc = Product.objects.filter(upc__iexact=data['upc'])

            # ... and use that to pick all standalone or parent products that the user is
            # allowed to access.
            qs_match = queryset.filter(
                Q(id__in=matches_upc.values('id')) | Q(id__in=matches_upc.values('parent_id')))

            if qs_match.exists():
                # If there's a direct UPC match, return just that.
                queryset = qs_match
            else:
                # No direct UPC match. Let's try the same with an icontains search.
                matches_upc = Product.objects.filter(upc__icontains=data['upc'])
                queryset = queryset.filter(
                    Q(id__in=matches_upc.values('id')) | Q(id__in=matches_upc.values('parent_id')))

        if data.get('title'):
            queryset = queryset.filter(title__icontains=data['title'])

        return queryset
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-15 18:53:59

您有一个循环导入-将列表视图的导入移动到应用程序配置的ready()方法中:

代码语言:javascript
复制
class CustomerConfig(apps.CustomerConfig):
    name = 'yourappsfolder.customer'

    def ready(self):
        super().ready()
        from .views import ProductListView
        self.extra_view =ProductListView
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61209435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档