在Django中执行用户个人资料 [英] Doing User Profiles In Django
问题描述
我正在尝试在我的网站中设置用户个人资料,所以我们有:
I'm trying to set up user profiles in my site so we have:
www.example.com/someuser
www.example.com/someuser
www.example.com/anotheruser2
www.example.com/anotheruser2
在我的urls.py
in my urls.py
url(r'^(?P<profile>[0-9A-Fa-f]{1,36})/', 'site.views.profile),
和我的观点:
def profile(request, profile):
... do something ...
有两个问题:
- 这是正确的方法还是有更好的方法?
- 我应如何处理其他网址,例如"aboutus"等.
对于第2点,我会这样做:
For Point 2, I would do:
url(r'^aboutus/', 'site.views.aboutus'),
url(r'^(?P<profile>[0-9A-Fa-f]{1,36})/', 'site.views.profile),
因此,现在,配置文件将成为网站上其他所有内容的统包,我必须检查有效的配置文件,如果找不到密码,则抛出404.
So now profile will be the catchall for everything else in the site, and I have to check for a valid profile then throw a 404 if a password is not found.
再次,有更好的方法吗?
Again, is there a better way to do this?
推荐答案
使用 site.views.profile
作为包包不是一个好主意.责任分工不是很好,这不应该是它的工作.像这样的事情怎么样:
It's not a good idea to use site.views.profile
as a catchall. It's not good separation of responsibilities, it shouldn't be its job. How about something like this instead:
url(r'^profile/$', 'site.views.profile_self'),
url(r'^profile/(?P<profile_name>[0-9A-Fa-f]{1,36})/$', 'site.views.profile'),
url(r'^aboutus/', 'site.views.aboutus'),
对于总体而言,请使用自定义的404页面,或者您可以让服务器引发404错误.
For the catchall, use a custom 404 page, or you could let the server raise a 404 error.
这篇关于在Django中执行用户个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!