- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近一直在创建 Hackernews 类型的克隆,并在添加更具可读性的 URL 时遇到了一些麻烦。根据我读到的内容,我相信它被称为 Slug。
基本上,我的网址读起来类似于:localhost/1/、localhost/2/,它们取自类别模型。我不想读取/1/、/2/等,而是更喜欢它采用 Category_name 字段并填充 url。例如 localhost/engineering、localhost/science。
这是我的类别模型:
models.py
class Category(models.Model):
category_name = models.CharField(max_length = 50)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
def __unicode__(self):
return self.category_name
class Meta:
verbose_name_plural = "categories"
这是我当前的 url.py:
url.py
url(r'^(?P<category_id>[0-9]+)/$', 'stories.views.category'),
和我的观点.py:
views.py
def category(request, category_id=1):
template = 'stories/category.html'
category = Category.objects.get(id = category_id)
return render_to_response(template, {
'category': category
})
我应该如何实现这样的事情?我读过有关 Slugify 的内容,但永远无法让它正常工作。谢谢。
最佳答案
你的 urls.py 应该是,
url(r'^(?P<category_name>\w+)/$', 'stories.views.category'),
而views.py应该是这样的,
def category(request, category_name):
template = 'stories/category.html'
category = Category.objects.get(category_name = category_name)
return render_to_response(template, {
'category': category
})
如果您不使用 slugified Category_name,上述内容可能会起作用。如果这样做,请在模型中使用 slug 字段,如this stack overflow answer所述。
关于python - 如何将我的网址转换为可读的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824655/
我正在通过 NodeSchool.io 练习学习 React 和 Express 框架。 我想将所有练习文件存储在具有多个页面的单个应用程序中,例如 索引 索引2 索引3 索引4 .... local
从这里:http://developer.android.com/reference/android/os/AsyncTask.html doInBackground(URL... urls) onP
我最近收到了一封电子邮件,其中包含以下内容(请勿点击!): UNS 这是原始电子邮件的链接:https://gist.github.com/anonymous/16963a230cab0a3a1bc
在 android 中,可以单击带有 URL 的 TextView 以在网络中打开 URL,方法是: android:autoLink="web" 我想做的是捕获这次点击,如果这个 TextView
我在我的网站上以 mysite.anotherdomain.org 的形式实现 Facebook 登录。我在 JavaScript SDK 的文档中做了所有解释,但由于我遇到了一些问题,我想知道错误是
我在 window.location.href 中有响应网址,我需要其中的 error、error_description 和 state 的值 http://localhost:4200/#erro
我正在创建无限加载,意味着当用户到达页面底部/特定 div 时会加载新页面。目前我有这个代码可以在点击时加载新页面。 $("#about").click(function(){ // load
当我们在谷歌引擎中搜索时,它也会显示热门网站标签或链接。就像我们搜索“bing”或“net beans”时一样。 问:它如何显示这些链接。我们是否必须告诉它显示这些链接。 问:它是否与 sitemap
我想从我的网址中获取我的产品。例如: http://www.website.com/product-category/iphone 我想获取 iphone,这对我的代码来说没问题,但我有一个下拉菜单来
我对 Pythonanywhere 完全陌生,我不知道为什么静态文件没有加载...这是我存储 css 和图像的路径,即 static/images/wikiLang.png 等 /static/adm
我正在使用这个正则表达式来验证 youtube 网址。 ^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$ 它很好用。 但我有这个
我刚刚在 gist.github 上传了一个我正在处理的小编码项目,因为它似乎是一次上传几个类的好方法。 我想将某人与我的“要点”联系起来,并在角落里写着: Public Clone URL: git
我正在使用 jQuery 验证引擎来解析我的表单数据: https://github.com/posabsolute/jQuery-Validation-Engine 验证 Twitter URL 的
我有一个 Django 应用程序,它可以在 localhost 上正常工作。即使对于 utf-8 URL 路径也是如此。但是当我在生产中使用它时,它给了我一个错误: 2019-09-01 14:32:
我已经安装了Laravel并开始尝试编写一个应用程序。我在/ app所在的目录中为 Assets 创建了一些目录。但是,当我尝试访问本地主机中的图像时,例如:http://localhost/asse
我们正在寻找一种方法来检查一长串 YouTube 网址,以查找目前私有(private)、已删除或不再可用的视频。我们可以检查状态,但即使视频不再公开可用,URL 也会返回 200。例如这两个: ht
我在 YouTube 上有现场事件,我想在我的网站上播放它。我想将我的事件设为私有(private),获取它的 RTMP 广播 URL 并将其粘贴到我的网站上,在 JWPlayer 中。 那可能吗?
当我在谷歌上搜索我的域时,它会显示我网站上的几个 https 网址,因为谷歌喜欢 https,但出于特殊原因我不想索引 https/ssl 版本。 如何避免这种情况,全世界都只通过 htaccess
我想获取在 Salesforce.com 授权期间作为回调收到的当前 URL。 url 中的数据位于片段部分。 最佳答案 您可以使用 $_SERVER['HTTP_HOST'] 和 $_SERVER[
我正在使用 ionic 创建一个应用程序,其中我使用 iframe 显示 URL。 这是 HTML 代码: 这是 Angular js: $scope.iframeHeight = windo
我是一名优秀的程序员,十分优秀!