社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

django 2.0的django oscar url?

newbee • 4 年前 • 1071 次点击  

Django Oscar显然已经更新为Django 2.0。我是django的新手,我不确定如何更新 Oscar Tutorial 以下内容:

from django.conf.urls import include, url
from django.contrib import admin
from oscar.app import application

urlpatterns = [
    url(r'^i18n/', include('django.conf.urls.i18n')),

    # The Django admin is not officially supported; expect breakage.
    # Nonetheless, it's often useful for debugging.
    url(r'^admin/', include(admin.site.urls)),

    url(r'', include(application.urls)),
]

这是当前可用的URL:

urlpatterns = [
    path('admin/', admin.site.urls),
]

那么,这是否意味着我要将django oscar url更改为?以下内容:

 path(r'^i18n/', include('django.conf.urls.i18n')),
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46457
 
1071 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Mobasshir Bhuiyan
Reply   •   1 楼
Mobasshir Bhuiyan    4 年前

Django Oscar总是将他们的URL放在应用程序目录的app s.py文件中,然后将该URL包含到项目级的url.py文件中,这是Django Oscar的设计决策。但我建议您使用path()而不是url(),因为这有助于避免复杂性。

##django-oscar/src/oscar/apps/basket/apps.py    
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from django.utils.translation import gettext_lazy as _

from oscar.core.application import OscarConfig
from oscar.core.loading import get_class


class BasketConfig(OscarConfig):
    label = 'basket'
    name = 'oscar.apps.basket'
    verbose_name = _('Basket')

namespace = 'basket'

def ready(self):
    self.summary_view = get_class('basket.views', 'BasketView')
    self.saved_view = get_class('basket.views', 'SavedView')
    self.add_view = get_class('basket.views', 'BasketAddView')
    self.add_voucher_view = get_class('basket.views', 'VoucherAddView')
    self.remove_voucher_view = get_class('basket.views', 'VoucherRemoveView')

def get_urls(self):
    urls = [
        url(r'^$', self.summary_view.as_view(), name='summary'),
        url(r'^add/(?P<pk>\d+)/$', self.add_view.as_view(), name='add'),
        url(r'^vouchers/add/$', self.add_voucher_view.as_view(),
            name='vouchers-add'),
        url(r'^vouchers/(?P<pk>\d+)/remove/$',
            self.remove_voucher_view.as_view(), name='vouchers-remove'),
        url(r'^saved/$', login_required(self.saved_view.as_view()),
            name='saved'),
    ]
    return self.post_process_urls(urls)

然后由项目级config.py文件导入

##django-oscar/src/oscar/config.py
# flake8: noqa, because URL syntax is more readable with long lines

from django.apps import apps
from django.conf import settings
from django.conf.urls import url
from django.urls import reverse_lazy
from django.views.generic.base import RedirectView

from oscar.core.application import OscarConfig
from oscar.core.loading import get_class


class Shop(OscarConfig):
    name = 'oscar'

    def ready(self):
        from django.contrib.auth.forms import SetPasswordForm

        self.catalogue_app = apps.get_app_config('catalogue')
        self.customer_app = apps.get_app_config('customer')
        self.basket_app = apps.get_app_config('basket')
        self.checkout_app = apps.get_app_config('checkout')
        self.search_app = apps.get_app_config('search')
        self.dashboard_app = apps.get_app_config('dashboard')
        self.offer_app = apps.get_app_config('offer')

        self.password_reset_form = get_class('customer.forms', 'PasswordResetForm')
        self.set_password_form = SetPasswordForm

    def get_urls(self):
        from django.contrib.auth import views as auth_views

        from oscar.views.decorators import login_forbidden

        urls = [
            url(r'^$', RedirectView.as_view(url=reverse_lazy('catalogue:index')), name='home'),
            url(r'^catalogue/', self.catalogue_app.urls),
            url(r'^basket/', self.basket_app.urls),
            url(r'^checkout/', self.checkout_app.urls),
            url(r'^accounts/', self.customer_app.urls),
            url(r'^search/', self.search_app.urls),
            url(r'^dashboard/', self.dashboard_app.urls),
            url(r'^offers/', self.offer_app.urls),

            # Password reset - as we're using Django's default view functions,
            # we can't namespace these urls as that prevents
            # the reverse function from working.
            url(r'^password-reset/$',
                login_forbidden(
                    auth_views.PasswordResetView.as_view(
                        form_class=self.password_reset_form,
                        success_url=reverse_lazy('password-reset-done'),
                        template_name='oscar/registration/password_reset_form.html'
                    )
                ),
                name='password-reset'),
            url(r'^password-reset/done/$',
                login_forbidden(auth_views.PasswordResetDoneView.as_view(
                    template_name='oscar/registration/password_reset_done.html'
                )),
                name='password-reset-done'),
            url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
                login_forbidden(
                    auth_views.PasswordResetConfirmView.as_view(
                        form_class=self.set_password_form,
                        success_url=reverse_lazy('password-reset-complete'),
                        template_name='oscar/registration/password_reset_confirm.html'
                    )
                ),
                name='password-reset-confirm'),
            url(r'^password-reset/complete/$',
                login_forbidden(auth_views.PasswordResetCompleteView.as_view(
                    template_name='oscar/registration/password_reset_complete.html'
                )),
                name='password-reset-complete'),
        ]
        return urls

奥斯卡的想法是将每个应用程序模块化。这就是为什么它会将app的所有url存储到app s.py的每个app文件夹中,并将其包含到项目级config.py文件中。

solarissmoke
Reply   •   2 楼
solarissmoke    5 年前

由于某些原因,读文档的文档已经过时了。 most recent version 在为django 2提供配置的github上。

使用 path 您需要删除url中的正则表达式语法。使用 include() 也已删除直接传递的URL配置,因此您将得到:

from django.urls import include, path
from django.contrib import admin
from oscar.app import application

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
    path('admin/', admin.site.urls),
    path('', application.urls),
]