Reverse for 'profile' with arguments '(None,)' not found. 1 pattern(s) tried: ['profile/(?P<user_id>[0-9]+)\\Z']

<a class="nav-link" href="{% url 'profile' user.id %}">

This takes me back to the error i get on the path (http://127.0.0.1:8000/profile/4) but works when I’m at the index page (http://127.0.0.1:8000)

Reverse for ‘profile’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘profile/(?P<user_id>[0-9]+)\Z’]

This is my current view


def profile(request, user_id):
    profile_user = User.objects.get(pk = user_id)
    # user_profile = Profile.objects.get(user=user_object)
    profile_post = NewTweet.objects.filter(user = user_id)
    post_count = len(profile_post)
    follower = request.user.username
    user = user_id

    if Followers.objects.filter(follower=follower, user=user).first():
        button_text = "Unfollow"
    else:
        button_text = "Follow"

    context = {
        "profile_user": profile_user,
        "post_count": post_count,
        "profile_post": profile_post,
        "button_text": button_text,
        "user": user,
    }

    return render(request, "network/profile.html", context)

The {{ user.id }} still shows as 4 when i use it in the template

Are we looking at two different views and templates here? I’m getting confused.

Are you saying you’re getting this error when you go to the page /profile/4? If so, then that is the view and template we need to see here.

This is my current views.py

Profile template section 1

<li class="nav-item">
  <a class="nav-link" href="{% url 'profile' user %}">
  <img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
  Profile
 </a>
 <!-- {{ user.id }}  THIS IS TO SEE THE ID SHOWING-->
</li>

When I use the above template with this “{% url ‘profile’ user %}”, my index page(http://127.0.0.1:8000/) gives me this error - Reverse for ‘profile’ with arguments ‘(<SimpleLazyObject: <User: Noble>>,)’ not found. 1 pattern(s) tried: [‘profile/(?P<user_id>[0-9]+)\Z’]
However, my profile page (http://127.0.0.1:8000/profile/4) shows without any error.

So with the above solution, I can access the profile page but not the index page

Profile template section 2

<li class="nav-item">
 <a class="nav-link" href="{% url 'profile' user.id %}">
 <img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
 Profile
 </a>
 <!-- {{ user.id }}  THIS IS TO SEE THE ID SHOWING  AND IT SHOWS 4-->
</li>

When I use the above template with this “{% url ‘profile’ user.id %}”, my index page(http://127.0.0.1:8000/) shows without any error.

However, my profile page (http://127.0.0.1:8000/profile/4) gives me this error - Reverse for ‘profile’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘profile/(?P<user_id>[0-9]+)\Z’] - even though the {{ user.id }} on the index page shows the id - 4

So with the above solution, I can access the index page but not the profile page

Are you saying you’re trying to use the same template for both views?

No, I have an index template that my Index view references but I don’t know why this {{user or user.id}} is affecting it.

LAYOUT.HTML - This layout page has nav links that takes me to index and profile

<li class="nav-item">
  <a class="nav-link text-decoration-none" href="{% url 'index' %}">
 <img src= {% static 'network/images/all-post.svg' %} alt="All post" class="left-sidebar-menu-icon">
                                        All Post
  </a>
</li>
 <li class="nav-item">
   <a class="nav-link" href="{% url 'profile' user.id %}">
   <img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
   Profile
 </a>
 {{ user.id }}
</li>

INDEX VIEW

def index(request):
    all_post = NewTweet.objects.all()
    context = {
        "all_post": all_post,
    }
    return render(request, "network/index.html", context)

INDEX.HTML

{% extends "network/layout.html" %}

{% block body %}
                <div class="col-5">
                    <div class="main-page-header mt-3 sticky-top">Home</div>
                    <div class="main-page-input">
                        <!-- <form class="d-flex flex-column"> -->
                            <div class="row">
                                <div class="col-2 main-page-header-profile-picture">
                                    <img src="https://i.pinimg.com/originals/a6/58/32/a65832155622ac173337874f02b218fb.png" class="main-page-header-profile-picture" alt="profile-picture"/>
                                </div>
                                <div class="col-10">
                                    <div class="main-page-input-box">
                                        <form method="POST" action="new_post">
                                        {% csrf_token %}
                                            {{ form.as_p }}
                                            <div>
                                                <textarea name="caption" id="tweet-box" class="form-control main-page-tweet-box text-wrap" cols="30" rows="10" placeholder="What's happening">
                                                </textarea>
                                                    <!-- <input type="text" class="form-control main-page-tweet-box text-wrap" id="exampleFormControlInput1" placeholder="What's happening"> -->
                                            </div>
                                                <div class="main-page-input-privacy pb-2 mt-3">
                                                    <i class="fas fa-globe-americas main-page-input-privacy-icon"></i>
                                                    <span class="ain-page-input-privacy-text ml-2">Everyone can reply</span>
                                                </div>
                                            </div>
                                            <div class="main-page-input-icons mt-3">
                                                <ul class="main-page-input-icons-ul">
                                                    <li><i class="fa-solid fa-image"></i></li>
                                                    <li><i class="fa-solid fa-square-poll-horizontal"></i></li>
                                                    <li><i class="fa-solid fa-face-grin"></i></li>
                                                    <li><i class="fa-regular fa-calendar"></i></li>
                                                    <li><i class="fa-solid fa-location-dot"></i></li>
                                                </ul>
                                                <button type="submit" class="btn btn-primary rounded-pill px-4 main-page-tweet-button"><strong>Tweet</strong></button>
                                            </div>
                                        </form>
                                    
                                </div>
                            </div>
                    <!-- </form> -->
                    </div>
                    <div class="tweet-feed">
                        {% for post in all_post%}
                            <div class="tweet">
                                <img src="http://placeimg.com/140/140/people" alt="author profile picture" class="tweet-author-image"/>
                                <div class="tweet-feed-content">
                                    <div class="tweet-header">
                                        <a href="#" class="tweet-author-username">{{post.user}}</a>
                                        <div class="tweet-author-handle">@ {{post.user}</div>
                                        <div class="tweet-dot">.</div>
                                        <div class="tweeted-time">{{post.created_at}}</div>
                                    </div>
                                    <div class="tweet-content">
                                        <div class="tweet-created-content">{{post.caption}}</div>
                                    </div>
                                    <div class="engagement-icons">
                                        <ul class="engagement-icons-ul mt-1">
                                            <li><i class="fa-regular fa-comment"></i></li>
                                            <li><i class="fa-solid fa-retweet"></i></li>
                                            <li><i class="fa-regular fa-heart"></i></li>
                                            <li><i class="fa-solid fa-arrow-up-from-bracket"></i></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                            <div class="tweet">
                                <img src="http://placeimg.com/140/140/people" alt="author profile picture" class="tweet-author-image"/>
                                <div class="tweet-feed-content">
                                    <div class="tweet-header">
                                        <a href="#" class="tweet-author-username">Nick Huber</a>
                                        <div class="tweet-author-handle">@sweatystartup</div>
                                        <div class="tweet-dot">.</div>
                                        <div class="tweeted-time">8h</div>
                                    </div>
                                    <div class="tweet-content">
                                        <div class="tweet-created-content">It is a wonderful day</div>
                                    </div>
                                    <div class="engagement-icons">
                                        <ul class="engagement-icons-ul mt-1">
                                            <li><i class="fa-regular fa-comment"></i></li>
                                            <li><i class="fa-solid fa-retweet"></i></li>
                                            <li><i class="fa-regular fa-heart"></i></li>
                                            <li><i class="fa-solid fa-arrow-up-from-bracket"></i></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="tweet">
                                <img src="http://placeimg.com/140/140/animals" alt="author profile picture" class="tweet-author-image"/>
                                <div class="tweet-feed-content">
                                    <div class="tweet-header">
                                        <a href="#" class="tweet-author-username">Randall</a>
                                        <div class="tweet-author-handle">@RandallKanna</div>
                                        <div class="tweeted-time"><span class="tweet-dot">.</span> 18h</div>
                                    </div>
                                    <div class="tweet-content">
                                        <div class="tweet-created-content">Hey friends</div>
                                        <img src="http://placeimg.com/300/300/tech" class="tweeted-image" alt="tweeter feed"/>
                                    </div>    
                                    <div class="engagement-icons">
                                        <ul class="engagement-icons-ul m-1">
                                            <li><i class="fa-regular fa-comment"></i></li>
                                            <li><i class="fa-solid fa-retweet"></i></li>
                                            <li><i class="fa-regular fa-heart"></i></li>
                                            <li><i class="fa-solid fa-arrow-up-from-bracket"></i></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
{% endblock %}

It appears that both index.html and profile.html may extend layout.html, is that correct?

If so, since layout.html has this:

then it (layout.html) is expecting that user is an object and not an id.

That means you then need to change your profile view.

In that view you have:

But, since layout.html is expecting the User object and not just the id field for that User object, it should be user = profile_user.

Now, it gets a little more complex than this, because you likely have django.contrib.auth.context_processors.auth in the TEMPLATES setting. This context processor injects the current user into the context under the name user. Your redefinition of user in the context could interfere with other default behavior.

Therefore, what I also recommend is that you don’t use the name user in your view to pass a User object that isn’t the requesting user to the template.

I’d suggest you actually remove that line completely and the "user": user line from the context.

Then, in your template, when you need to create the url reference to that profile, you use {% url 'profile' profile_user.id %}

But you want to be sure you’re clear in your template when you want to reference the current user (user) and when you want to refer to a different user (profile_user)

You don’t need to explicitly include user in the context if you have django.contrib.auth.context_processors.auth in your context_processors in the TEMPLATES section of your settings.

That would not cause user.id to show “None”. In fact, it wouldn’t show anything at all.

This is correct, I would edit using your suggestion and provide feedback, thanks

I made the following edit based on your suggestion

VIEWS.PY

def profile(request, user_id):
    profile_user = User.objects.get(pk = user_id)
    # user_profile = Profile.objects.get(user=user_object)
    profile_post = NewTweet.objects.filter(user = user_id)
    post_count = len(profile_post)
    follower = request.user.username

    if Followers.objects.filter(follower=follower).first():
        button_text = "Unfollow"
    else:
        button_text = "Follow"

    context = {
        "profile_user": profile_user,
        "post_count": post_count,
        "profile_post": profile_post,
        "button_text": button_text,
    }

    return render(request, "network/profile.html", context)

LAYOUT.HTML

<a class="nav-link" href="{% url 'profile' profile_user.id %}">
  <img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
                                        Profile
</a>

These shows in url path - http://127.0.0.1:8000/profile/4

But still getting - Reverse for ‘profile’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘profile/(?P<user_id>[0-9]+)\Z’] at the url path - http://127.0.0.1:8000/

I also tried altering this - but i get same error
<a class="nav-link" href="{% url 'profile' profile_user %}">

Which user are you trying to set a link for in your layout.html?

You posted earlier for layout.html:

What person is this link supposed to link you to? The person currently logged in? If so, then this reference would remain as user.id.

That’s the point I was trying to get to in my previous post:

1 Like

This is for the currently logged in user.

Eureka! This is working.

Thank you so much, I always appreciate your help.

Thank you, I was able to resolve this below @ plopidou and @leandrodesouzadev