在Django中如何使用这样的URL? [英] How to have a URL like this in Django?

查看:129
本文介绍了在Django中如何使用这样的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Django中使用 example.com/category/catename-operation / 之类的URL?

How can I have URLs like example.com/category/catename-operation/ in Django?

另外在某些情况下,用户输入一个空格分隔的类别,该怎么处理呢?
例如,如果用户将类别输入为我的家,则此类别的URL将成为
example.com/my home / which

Also in some cases the user enters a space separated category, how can I handle that? E.g if the user enters the category as "my home", then the URL for this category will become example.com/my home/ which is not a valid URL.

如何处理这些事情?

推荐答案

如果您想保持网址漂亮,例如,当用户输入我的类别时,您可以在URL中使用我的类别而不是我的%20category。我建议您查看SlugField( http://docs.djangoproject.com / en / dev / ref / models / fields /#slugfield ),并使用ModelAdmin的prepopulated_fields属性预先填充该slugfield。

If you want to keep your URLs pretty, for example when a user enters "my category" you could have "my-category" instead of "my%20category" in the URL. I suggest you look into SlugField (http://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield) and prepopulating that slugfield using ModelAdmin's prepopulated_fields attribute.

这篇关于在Django中如何使用这样的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆