如何正确实现example.com/username和example.com/id123?

时间:2016-08-28 12:20:52

标签: django django-urls

我想让用户拥有自定义网址,例如example.com/username,但我不想强制他们这样做,所以在这种情况下我希望有这样的地址:{{1 }}

目前的做法:

example.com/id123

这种方法不灵活。特别是导航\菜单的问题,我想制作一个按钮# root urls.py urlpatterns = [ url(r'^((?P<user_id>[i][d]\d+)|(?P<username>[\w.+-]+))/', include('profiles.urls', namespace='profiles_user')), # profiles/urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.profiles_home, name='profiles_home'), ] # profiles/views.py def profiles_home(request, user_id=None, username=None): if user_id: user_id = int(user_id.replace('id', '')) user = get_object_or_404(UserProfile, pk=user_id) else: user = get_object_or_404(UserProfile, username=username) return render(request, 'profiles/profiles_home.html', {'profile_user': user}) 。对于导航,我使用My page,其中没有内置功能可以使用&#39; double&#39; -link for 1 button,这取决于用户是否具有用户名。 django-sitetree用于将按钮连接到链接的方案是定义sitetree

有人可以建议我如何改进/简化我的网址吗?

2 个答案:

答案 0 :(得分:0)

用于处理导航按钮我通常将id = 0映射到当前用户,因此这里是您的视图

def profiles_home(request, user_id=None, username=None):
    if user_id == 0:
       user = request.user
    else if user_id:
        user_id = int(user_id.replace('id', ''))
        user = get_object_or_404(UserProfile, pk=user_id)
    else:
        user = get_object_or_404(UserProfile, username=username)
    return render(request, 'profiles/profiles_home.html', {'profile_user': user})

答案 1 :(得分:0)

我在url模型中添加了一个新字段UserProfile

url = models.CharField(_('URL address'), max_length=30, unique=True)

以及其他部分:

# main url
urlpatterns = [
    url(r'^admin/', admin.site.urls),

    # My url
    url(r'^(?P<user_url>[\w.-]+)/', include('profiles.urls', namespace='profiles_username')),
]

# profiles.urls (my app's urls)
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.profiles_home, name='profiles_home'),
]

# profiles.views
def profiles_home(request, user_url):
    user = get_object_or_404(UserProfile, url=user_url)

    return render(request, 'profiles/profiles_home.html', {'user_profile_form': user})
创建用户后会生成

url。因为我使用了AllAuth,所以Adaptor看起来像是:{/ p>

from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.utils import user_username, user_email, user_field

class UserProfileAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):

        data = form.cleaned_data
        first_name = data.get('first_name')
        last_name = data.get('last_name')
        email = data.get('email')
        username = data.get('username')
        user_email(user, email)
        user_username(user, username)

        if first_name:
            user_field(user, 'first_name', first_name)
        if last_name:
            user_field(user, 'last_name', last_name)
        if 'password1' in data:
            user.set_password(data["password1"])
        else:
            user.set_unusable_password()
        self.populate_username(request, user)
        if commit:
            user.save()

        # Add user's url to User's model
        user.url = 'id'+str(user.pk)
        user.save()
        return user

还必须为用户个人资料页面实施验证,以禁止用户使用id123456之类的自定义网址,即id + nums,但目前尚未实施