Django“找不到页面(404)”

时间:2013-11-05 23:38:46

标签: python django

我认为我做的一切都是正确的,但我不知道为什么它不起作用。当我尝试访问某个页面时,我收到错误“找不到页面(404)”。

所以,这个所有产品页面的工作原理是127.0.0.1:8000/products/,但是当我尝试访问单个产品页面127.0.0.1:8000/products/audi时,我收到的错误是找不到...

那么,也许你这里有什么不对? 谢谢。

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.

主要项目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'),
)

产品应用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

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

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

2 个答案:

答案 0 :(得分:3)

您的网址格式在开头有一个额外的$,因此它永远不会匹配任何内容:

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

这应该是:

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

这仍然需要一个斜杠,这是正常的模式。经过更正后,您的网址应为/products/audi/。您没有显示创建该URL的上下文,但这是为什么最好使用Django的url reversal来构建URL的一个例子。在Python代码中看起来像这样(例如,可能在模型上的get_absolute_url方法中:

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

或者像这样,在模板中:

Django 1.5:

{% url 'single_product' someproduct.slug %}

Django 1.4及更早版本:

{% url single_product someproduct.slug %}

答案 1 :(得分:2)

你有一个错字:

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

$之后注意这个^,在regexp中它表示字符串结束。替换为

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