如何在登录后重定向至用户个人资料页?

2024-06-02 06:29:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法,重定向到用户配置文件页后登录。我面临的问题是,我可以找出如何将用户id传递到url中,以完成重定向。你知道吗

我知道这是一个常见的问题,我试着从其他人的问题中找到许多建议的解决方案,但是没有一个能让我找到解决方案。正如我在评论中指出的,在url路径中有一个用户id的原因是由于应用程序的性质允许用户查看彼此的个人资料页面。你知道吗

<div class='registration'>
    <form method="post" action="{% url 'auth_login' %}">{% csrf_token %}

        <p> &nbsp;{% trans form.username.label_tag %} &nbsp;{{ form.username }}</p>

        <p> &nbsp; {% trans form.password.label_tag %} &nbsp; {{ form.password }}</p>

       <p>{% blocktrans %}<a href="{{ auth_pwd_reset_url }}">Forgot</a> your password? </br>
        <a href="{{ register_url }}">Need an account</a>?{% endblocktrans %}</p>

    <input class='login-button' type="submit" value="{% trans 'login' %}" />
    <input class= 'login-button'type="hidden" name="next" value="{{ next }}" />
    </form>
</div>
LOGIN_REDIRECT_URL = '/'
    path('profile/<int:user_id>', core_views.user_profile, name='profile'),

我试过的东西

  • (LOGIN\u REDIRECT\u URL='/profile/')-这给了我一个404错误。 -(LOGIN\u REDIRECT\u URL='/profile/')-这给了我一个NoReverseMatch错误。你知道吗
  • input class='login-button'type=“hidden”name=“next”value=“{%url配置文件请求用户id%}““
  • 我试过在视图中登录,但也没用。你知道吗

我已经被困了一段时间,任何帮助将不胜感激!你知道吗


Tags: 用户nameformidurltransinputvalue
1条回答
网友
1楼 · 发布于 2024-06-02 06:29:23

只需调用相同的函数,然后将user_id设置为request.user.id(如果未传递)

path('profile/', core_views.user_profile, name='profile'),
path('profile/<int:user_id>/', core_views.user_profile, name='profile-view'),

def user_profile(request, user_id=None):
    if user_id == None: user_id = request.user.id
    ... now continue as before

相关问题 更多 >