0
我試圖在Django項目中設置Slug網址。我不斷收到404錯誤,無法找到我錯過的東西。我之前做過幾次,但由於某種原因,我必須缺少一些東西。S URL網址在Django中不起作用
models.py
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
# Create your models here.
class Pages(models.Model):
title = models.CharField(max_length=250, default="Add Title Here")
content = models.TextField(default="Add Content Here")
slug = models.SlugField(unique=True)
seo_title = models.CharField(max_length=250, default="Add Title Here")
seo_description = models.CharField(max_length=160, default="Add Seo Description")
active = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False, null=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)
def __unicode__(self):
return self.title
class Meta:
unique_together = ('slug', 'title')
def get_absolute_url(self):
return reverse("single_page", kwargs={"slug": self.slug})
views.py
from django.shortcuts import render, get_object_or_404, Http404
# Create your views here.
from .models import Pages
def Single(request, slug):
try:
page = get_object_or_404(Pages, slug=slug)
context = {'page': page}
template = 'pages/page_detail.html'
return render(request, template, context)
except:
raise Http404
urls.py
from django.conf.urls import url, patterns
from . import views
urlpatterns = [
url(r'^(?P<slug>[\w-]+)/$', views.Single, name='single_page'),
]
你只是在你看來無條件這就提出了一個404,我建議你至少'除例外爲e: '然後記錄異常詳情。目前,你的觀點或模型中的任何錯誤都會拋出404錯誤。也可以看看實際發生的情況。 – vastlysuperiorman
在視圖中收到'slug'的值是多少? – alecxe
和視圖名稱必須小寫! – doniyor