在Django中创建用户配置文件

在Django中创建用户配置文件,django,django-views,Django,Django Views,我正在尝试在我的站点中设置用户配置文件,以便: www.example.com/someuser www.example.com/anotheruser2 在myurl.py中 url(r'^(?P<profile>[0-9A-Fa-f]{1,36})/', 'site.views.profile), 有两个问题: 这是正确的方法还是有更好的方法? 我应该如何处理其他URL,如aboutus等。 关于第2点,我想: url(r'^aboutus/', 'site.vie

我正在尝试在我的站点中设置用户配置文件,以便:

www.example.com/someuser

www.example.com/anotheruser2

在myurl.py中

url(r'^(?P<profile>[0-9A-Fa-f]{1,36})/',    'site.views.profile),
有两个问题:

这是正确的方法还是有更好的方法? 我应该如何处理其他URL,如aboutus等。 关于第2点,我想:

url(r'^aboutus/',    'site.views.aboutus'),
url(r'^(?P<profile>[0-9A-Fa-f]{1,36})/',    'site.views.profile),
因此,现在的个人资料将是所有其他在网站上,我必须检查一个有效的个人资料,然后抛出一个404,如果没有找到密码

同样,有更好的方法吗?

accounts/models.py

accounts/url.py

accounts/views.py

这样,只有登录的用户才能看到自己的个人资料

至于第二点,这有什么问题

url(r'^about_us/$', 'site.views.about_us'),
-更新-

啊,好的。那么,你是对的。但是为什么不用用户名呢

accounts/url.py


将site.views.profile用作catchall不是一个好主意。责任分离不好,不应该是它的工作。不如换成这样:

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'),

对于catchall,使用自定义404页面,或者您可以让服务器引发404错误。

我看到的问题是,这个解决方案需要设置www.example.com/profile/someuser。要求是www.example.com/someuser如果这是一个硬要求,那么除了按照您的建议执行之外,您别无选择。但是它的设计很差,因为它会妨碍你正确地划分责任。我认为这个解决方案的问题是我必须设置www.example.com/profile/someuser。要求是www.example.com/someuser
url(r'^profile/$', 'site.views.profile'),
from django.contrib.auth.decorators import login_required

@login_required
def profile(request):
    # get current logged user profile
    profile = request.user.get_profile()
url(r'^about_us/$', 'site.views.about_us'),
url(r'^(?P<username>[-\w]+)/$', 'site.views.profile'),
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'),