Django urls.py。未按预期工作

Posted

技术标签:

【中文标题】Django urls.py。未按预期工作【英文标题】:Django urls.py. Not working as intended 【发布时间】:2015-07-19 06:30:19 【问题描述】:

我的 urls.py 中有这个网址

urlpatterns = [
    # Examples:
    url(r'^$', 'products.views.home', name='home'),
    url(r'^s/$', 'products.views.search', name='search'),
    url(r'^products/$', 'products.views.all', name='products'),
    url(r'^products/(?P<slug>[\w-]+)/$', 'products.views.single', name='single_product'),
    url(r'^cart/(?P<id>\d+)/$', 'carts.views.remove_from_cart', name='remove_from_cart'),
    url(r'^cart/(?P<slug>[\w-]+)/$', 'carts.views.add_to_cart', name='add_to_cart'),
    url(r'^cart/$', 'carts.views.view', name='cart'),
    url(r'^checkout/$', 'orders.views.checkout', name='checkout'),
    url(r'^orders/$', 'orders.views.orders', name='user_orders'),
    url(r'^ajax/dismiss_marketing_message/$', 'marketing.views.dismiss_marketing_message', name='dismiss_marketing_message'),
    url(r'^ajax/email_signup/$', 'marketing.views.email_signup', name='ajax_email_signup'),
    url(r'^ajax/add_user_address/$', 'accounts.views.add_user_address', name='ajax_add_user_address'),

    #footerpages
    url(r'^about/$', 'footerpages.views.about_us', name='about_us'),
    url(r'^faq/$', 'footerpages.views.faq', name='faq'),
    url(r'^privacy/$', 'footerpages.views.privacy', name='privacy'),
    url(r'^terms/$', 'footerpages.views.terms', name='terms'),

    #contact us
    url(r'^contact/$', 'contact.views.contact', name='contact'),

    # url(r'^blog/', include('blog.urls')),
    #(?P<all_items>.*)
    #(?P<id>\d+)
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/logout/$', 'accounts.views.logout_view', name='auth_logout'),
    url(r'^accounts/login/$', 'accounts.views.login_view', name='auth_login'),
    url(r'^accounts/register/$', 'accounts.views.registration_view', name='auth_register'),
    # url(r'^accounts/profile/$', 'accounts.views.profile', name='profile'),
    url(r'^accounts/profile/edit/$', 'accounts.views.profile_edit', name='profile_edit'),
    url(r'^accounts/packages/$', 'accounts.views.packages', name='packages'),
    url(r'^accounts/packages/edit/(?P<variation_id>\d+)/$', 'accounts.views.packages_edit', name='packages_edit'),
    url(r'^accounts/packages/delete/(?P<variation_id>\d+)/$', 'accounts.views.packages_delete', name='packages_delete'),
    url(r'^accounts/packages/add/$', 'accounts.views.packages_add', name='packages_add'),
    url(r'^accounts/address/add/$', 'accounts.views.add_user_address', name='add_user_address'),
    url(r'^accounts/activate/(?P<activation_key>\w+)/$', 'accounts.views.activation_view', name='activation_view'),
]

然后我变了

url(r'^products/(?P<slug>[\w-]+)/$', 'products.views.single', name='single_product'),

收件人:

url(r'^(?P<slug>[\w-]+)/$', 'products.views.single', name='single_product'),

因此,“single_product”产品网址可以正常工作,并且会在 slug 中传递。我什至可以加载帐户/页面。但是其他网址,例如 about、faq、cart、checkout,都给了我一个找不到页面的错误。我无法事件访问http://127.0.0.1:8000/admin/。它给了我一个找不到页面 (404)。

现在,如果我将 urls.py 更改为 /xyz/admin/,那么 http://127.0.0.1:8000/xyz/admin/ 可以工作。 xyz/cart 或 xyz/faq 也是如此。

我想保留

url(r'^(?P<slug>[\w-]+)/$', 'products.views.single', name='single_product'),

我能做什么?

如果有任何影响,只需包括 carts.views.view。

def view(request):
try:
    the_id = request.session['cart_id']
    cart = Cart.objects.get(id=the_id)
except:
    the_id = None       
if the_id:
    new_total = 0.00
    for item in cart.cartitem_set.all():
        pass
        for variation in item.variations.all():
            line_total = (float(variation.price_per_guest) * item.quantity) + (float(variation.extra_km_charge) * item.distance) + (float(variation.extra_hours_charge) * item.hour)
            new_total += line_total
            # subtotal_guests = variation.price_per_guest * item.quantity
            # subtotal_km_charges = variation.extra_km_charge * item.distance
            # subtotal_hours_charges = variation.extra_hours_charge * item.hour
    request.session['items_total'] = cart.cartitem_set.count()
    cart.total = new_total
    cart.save()
    context =  "cart": cart,
                # "subtotal_guests": subtotal_guests,
                # "subtotal_km_charges": subtotal_km_charges,
                # "subtotal_hours_charges": subtotal_hours_charges,
                
else:
    empty_message = "Your Cart is Empty, please keep shopping."
    context = "empty": True, "empty_message": empty_message 

template = "cart/view.html"
return render(request, template, context)

【问题讨论】:

【参考方案1】:

所有工作都按预期工作 - about/^([\w-]+)/$ url 完美匹配,因此此 url 调用 products.views.single 视图。同样的问题对faq/cart/checkout/admin/ 页面有效——它们都被这个正则表达式捕获了..

single_product 网址移动到urlpatterns 的最底部。它应该是列表中的最后一条记录:

urlpatterns = patterns('',

    ... # all your urls except the `single_product`

    url(r'^(?P<slug>[\w-]+)/$', 'products.views.single', name='single_product')
)

【讨论】:

以上是关于Django urls.py。未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章

Django之Urls.py模块

Django 忽略对 urls.py 的更新

刷新 django 中的 urls.py 缓存

如果条件在urls.py - django

Django中关于URL配置文件urls.py的理解

django 在 urls.py 中重定向