简体   繁体   English

Django“找不到页面(404)”

[英]Django “Page not found (404)”

I think I done everything correct but I don't know why it doesn't work. 我想我所做的一切都正确,但是我不知道为什么它不起作用。 When I try to reach a page I get error "Page not found (404)". 当我尝试访问页面时,出现错误“找不到页面(404)”。

So, this all products page works 127.0.0.1:8000/products/ but when I try to visit single product page 127.0.0.1:8000/products/audi I get error that it's not found... 因此,此所有产品页面均适用于127.0.0.1:8000/products/,但是当我尝试访问单个产品页面127.0.0.1:8000/products/audi时,我得到未找到的错误...

So, maybe you what's wrong here? 所以,也许您这是怎么了? Thank you. 谢谢。

Page not found (404)
Request Method: GET
Request URL:    http://link/products/audi
Using the URLconf defined in ecommerce.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^media/(?P<path>.*)$
^admin/doc/
^admin/
^products/ ^$ [name='products']
^products/ ^$(?P<slug>.*)/$
^contact/ [name='contact_us']
The current URL, products/audi, didn't match any of these.

Main project urls.py: 主项目urls.py:

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.STATIC_ROOT
        }),
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT
        }),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^products/', include('products.urls')),
    url(r'^contact/', 'contact.views.contact_us', name='contact_us'),
)

Products app urls.py: 产品应用urls.py:

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


urlpatterns = patterns('products.views',
    url(r'^$', 'all_products', name='products'),
    url(r'^$(?P<slug>.*)/$', 'single_product'),
)

views.py views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, RequestContext, get_object_or_404

from .models import Product

def all_products(request):
    products = Product.objects.filter(active=True)
    return render_to_response('products/all.html', locals(), context_instance=RequestContext(request))


def single_product(request, slug):
    product = get_object_or_404(Product, slug=slug)
    return render_to_response('products/single.html', locals(), context_instance=RequestContext(request))

models.py models.py

from django.db import models

# Create your models here.
class Product(models.Model):
    title = models.CharField(max_length=220)
    description = models.CharField(max_length=3000, null=True, blank=True)
    price = models.DecimalField(max_digits=1000, decimal_places=2, null=True, blank=True)
    slug = models.SlugField()
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __unicode__(self):
        return self.title
    class Meta:
        ordering = ['title',]


class ProductImage(models.Model):
    product = models.ForeignKey(Product)
    description = models.CharField(max_length=3000, null=True, blank=True)
    image = models.ImageField(upload_to='product/images/')
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __unicode__(self):
        return self.image

Your URL pattern has an extra $ at the beginning, so it can never match anything: 您的网址格式开头有一个额外的$ ,因此它永远无法匹配任何内容:

url(r'^$(?P<slug>.*)/$', 'single_product')

This should be: 应该是:

url(r'^(?P<slug>.*)/$', 'single_product')

This still requires a trailing slash, which is the normal pattern. 这仍然需要尾随斜杠,这是正常模式。 With that corrected, your URL should be /products/audi/ . 纠正该错误后,您的URL应为/products/audi/ You don't show the context in which that URL is created, but this is one example of why it's a good idea to use Django's url reversal to build URLs if at all possible. 您没有显示创建该URL的上下文,但这是一个示例,说明为什么尽可能使用Django的url反向来构建URL是一个好主意。 That would look something like this, in Python code (for instance, possibly in a get_absolute_url method on the model: 在Python代码中(例如,可能在模型的get_absolute_url方法中)如下所示:

reverse('single_product', kwargs={'slug': someproduct.slug})

Or like this, in a template: 或者像这样,在模板中:

Django 1.5: Django 1.5:

{% url 'single_product' someproduct.slug %}

Django 1.4 and earlier: Django 1.4及更早版本:

{% url single_product someproduct.slug %}

You have a typo: 您有错字:

url(r'^$(?P<slug>.*)/$', 'single_product'),

Note this $ just after ^ , in regexp it states for a string end. 请注意,此$恰好位于^后面,表示字符串结尾。 Replace with 用。。。来代替

url(r'^(?P<slug>.*)/$', 'single_product'),

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

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