简体   繁体   English

尝试查看产品详细信息页面时 Django 3 中的 URL 问题

[英]Issues with URLs in Django 3 when trying to view products detail page

New to Django and im trying to simply get to my products detail page using the following code in my URLs.py: Django 的新手,我试图使用我的 URLs.py 中的以下代码简单地访问我的产品详细信息页面:

urls.py:网址.py:

from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from products import views
import carts
from carts.views import CartView
from django.urls import path


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('products/', views.all, name='products'),
    path('s/', views.search, name='search'),
    path('products/<slug:slug>', views.single, name='single_product'), <----This is the page I want to get to
    path('cart/', carts.views.CartView, name='cart'),
]
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

If its any help, this is the link I use in my template to get to the product display page <a href="{{ product.get_absolute_url }}" class="btn btn-primary">View</a>如果有帮助,这是我在模板中使用的链接,用于访问产品显示页面<a href="{{ product.get_absolute_url }}" class="btn btn-primary">View</a>

And here is my view:这是我的观点:

def single(request, slug):
    try:
        product = Product.objects.get(slug=slug)
        images = ProductImage.objects.filter(product=product)
        context = {'product': product, "images": images}
        template = 'products/single.html'
        return render(request, template, context)
    except:
        raise Http404

But when trying to access this page I get the following 404 Error:但是当尝试访问此页面时,我收到以下 404 错误: 在此处输入图像描述

Maybe a fresh pair of eyes can help me spot the mistake I'm making?也许一双新鲜的眼睛可以帮助我发现我正在犯的错误?

If any additional info is needed please let me know, I'll be happy to provide.如果需要任何其他信息,请告诉我,我很乐意提供。 Thanks a million in advance!提前一百万谢谢!

First try replacing:首先尝试替换:

    path('products/<slug:slug>', views.single, name='single_product'),

with

    path('products/<slug:slug>/', views.single, name='single_product'),

Django automatically appends a slash (/) to the end of urls by default and the image you showed us shows an url ending on a slash while your defined path does not contain one. Django 默认情况下会自动在 url 的末尾附加一个斜杠 (/),您向我们展示的图像显示了一个 url 以斜杠结尾,而您定义的路径不包含斜杠。 I just tried to reproduce it, and this made it work.我只是试图重现它,这使它工作。

And then if it still doesn't work, double check if you are using the correct slug to find your Product using the shell as you are raising a 404 whenever an exception occurs.然后如果它仍然不起作用,请仔细检查您是否使用正确的 slug 使用 shell 找到您的产品,因为每当发生异常时您都会引发 404。 You might even consider removing the try/except to check if it's actually raising a different exception.您甚至可以考虑删除 try/except 以检查它是否真的引发了不同的异常。

Maybe the last slash ('/') is the problem, try the url without it: http://127.0.0.1/products/product-one也许最后一个斜杠('/')是问题所在,试试没有它的 url: http://127.0.0.1/products/product-one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM