Python/Django:在 URL 中使用 Slug 时遇到问题
Python/Django: Trouble using Slug in URL
我正在使用 Python 2.7 和 Django 1.10(我的第一个)构建应用程序,现在我无法使用 slugs 创建产品详细信息页面的链接。
当我手动输入产品时 URL 它工作正常 (/courses/analise-swot/)。它读取 details.html 页面并使用数据库中的信息适当地构建它。以下是我遇到的错误:
Page not found (404)
Request Method: GET
Request URL:
Using the URLconf defined in simplemooc.urls, Django tried these URL patterns, in this order:
^ ^$ [name='home']
^ ^contato/ [name='contato']
^courses/ ^$ [name='cursos']
^courses/ ^(?P<slug>[A-Za-z0-9_\-]+)/$ [name='details']
^admin/
^media\/(?P<path>.*)$
The current URL, courses/(u'details', (), {u'slug': u'analise-swot'}), didn't match any of these.
这是我的代码:
class 我在 models.py:
中创建 slug
class Course(models.Model):
name = models.CharField('Nome', max_length=100)
slug = models.SlugField('Atalho', max_length=100, unique=True)
get_absolute_url models.py 中的方法:
def get_absolute_url(self):
return ('details', (), {'slug': self.slug})
整个urls.py:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.cursos, name='cursos'),
url(r'^(?P<slug>[A-Za-z0-9_\-]+)/$', views.details, name='details'),
在views.py中的请求:
def details(request, slug):
courses = get_object_or_404(Course, slug=slug)
context = {
'course': courses
}
template_name = 'courses/details.html'
return render(request, template_name, context)
get_absolute_url
方法不应该 return 元组。
Define a get_absolute_url()
method to tell Django how to calculate
the canonical URL for an object. To callers, this method should appear
to return a string that can be used to refer to the object over
HTTP.
像下面这样的会更像:
def get_absolute_url(self):
return '/%s/' % self.slug
我正在使用 Python 2.7 和 Django 1.10(我的第一个)构建应用程序,现在我无法使用 slugs 创建产品详细信息页面的链接。
当我手动输入产品时 URL 它工作正常 (/courses/analise-swot/)。它读取 details.html 页面并使用数据库中的信息适当地构建它。以下是我遇到的错误:
Page not found (404)
Request Method: GET
Request URL:
Using the URLconf defined in simplemooc.urls, Django tried these URL patterns, in this order:
^ ^$ [name='home']
^ ^contato/ [name='contato']
^courses/ ^$ [name='cursos']
^courses/ ^(?P<slug>[A-Za-z0-9_\-]+)/$ [name='details']
^admin/
^media\/(?P<path>.*)$
The current URL, courses/(u'details', (), {u'slug': u'analise-swot'}), didn't match any of these.
这是我的代码:
class 我在 models.py:
中创建 slugclass Course(models.Model):
name = models.CharField('Nome', max_length=100)
slug = models.SlugField('Atalho', max_length=100, unique=True)
get_absolute_url models.py 中的方法:
def get_absolute_url(self):
return ('details', (), {'slug': self.slug})
整个urls.py:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.cursos, name='cursos'),
url(r'^(?P<slug>[A-Za-z0-9_\-]+)/$', views.details, name='details'),
在views.py中的请求:
def details(request, slug):
courses = get_object_or_404(Course, slug=slug)
context = {
'course': courses
}
template_name = 'courses/details.html'
return render(request, template_name, context)
get_absolute_url
方法不应该 return 元组。
Define a
get_absolute_url()
method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.
像下面这样的会更像:
def get_absolute_url(self):
return '/%s/' % self.slug