我如何在Django中用用户名声明用户个人资料?

问题描述 投票:0回答:2

我是Django新手,还在学习。目前,我正在尝试开发Blog网站。最近几天,我遇到这种错误,无法解决。

我的代码:

base.html

<nav>
    <ul>
        <li><a href="{% url 'account:profile' username=user.username %}">Profile</a></li>
    </ul>
</nav>

urls.py

path('profile/<username>/', views.profile, name='profile'),

views.py

def profile(request, username):
    user = User.objects.get(username=username)
    editable = False
    if user.is_authenticated and request.user == user:
        editable = True
    return render(request, 'Account/profile.html', {'user':user, 'editable':editable})

profile.html

<!--User Profile-->
<div class="row info">
    <div class="col-sm-4 image">
        {% if user.user_profile %}
        <img src="/media/{{ user.user_profile.profile_pic }}" alt="{{ user.username }}" title="{{ user.username }}" class="rounded-circle">

        {% else %}
        <img src="/media/profile_pics/avatar.png" title="Add Profile Photo" class="rounded-circle">

        {% endif %}
    </div>

    <div class="col-sm-8 text">
        <h2>{{ user.username }}</h2>
        <h6 class="post-text"><strong>{{ user.blog_author.count }}</strong> Blogs</h6>
        <div class="full-name">
            <h4>{{ user.first_name }} {{ user.last_name }}</h4>
        </div>
        <br>
        {% if editable %}
        <a href="{% url 'account:edit_profile' %}" class="btn btn-light btn-md">Edit Profile</a> <a
                href="{% url 'account:logout' %}" class="btn btn-danger btn-md logout">Logout</a>
        {% endif %}
    </div>
</div>

当用户登录时,它可以完美运行。但是,当用户注销时,它显示这种类型的错误。

NoReverseMatch at /
Reverse for 'profile' with keyword arguments '{'username': ''}' not found. 1 pattern(s) tried: ['account/profile/(?P<username>[^/]+)/$']

我想为所有用户使用一个模板。

谢谢。

python django profile username usernametoken
2个回答
0
投票

我认为您注销时。该网址将为localhost:8000 / profile //


0
投票

在settings.py中,您必须找到LOGOUT_REDIRECT_URL并放入="yoururl"

© www.soinside.com 2019 - 2024. All rights reserved.