登录后如何重定向到用户个人资料页面? [英] How to redirect to user profile page after log in?
问题描述
我正在寻找一种登录后重定向到用户个人资料页面的方法.我面临的问题是我可以弄清楚如何将user_id传递到url中以完成重定向.
I'm looking for a way to redirect to the user profile page after log in. The problem I'm facing is that I can figure out how to pass the user_id into the url to get the redirection to complete.
我知道这是一个常见的问题,我尝试遵循从其他人的问题中发现的许多建议解决方案,但是没有一个解决方案可以帮助我找到解决方案.正如我在评论中指出的那样,在URL路径中包含user_id的原因是由于该应用的性质,该应用允许用户查看彼此的个人资料页面.
I understand that this is a common issue and I've tried following many suggested solutions I found from other people's issues, however none have allowed me to find a solution. As I pointed out in the comments, the reason for having a user_id in the url path is due to the nature of the app which allows users to view each other's profile page.
<div class='registration'>
<form method="post" action="{% url 'auth_login' %}">{% csrf_token %}
<p> {% trans form.username.label_tag %} {{ form.username }}</p>
<p> {% trans form.password.label_tag %} {{ 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_REDIRECT_URL ='/profile/')-这给了我404错误.-(LOGIN_REDIRECT_URL ='/profile/')-这给了我一个NoReverseMatch错误.
- input class ='login-button'type ="hidden" name ="next" value ="{%网址配置文件request.user_id%}"
- 我已经尝试过进行登录,但这也不起作用.
对此我已经停留了一段时间,将不胜感激!
I've been stuck for a while on this and any help would be greatly appreciated!
推荐答案
简单地调用相同的函数,然后将 user_id
设置为 request.user.id
(如果是)没有通过
Simply call the same function and then set the user_id
to request.user.id
if it is not passed
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
这篇关于登录后如何重定向到用户个人资料页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!