NoReverseMatch at / Reverse for 'user-profile' with arguments '('',)' not found. 1 pattern(s) tried: ['profile/(?P<pk>[^/]+)/$']
Please somebody help please, am new to Django and don't know how to get around this in my code. Am following a tutorial and that builds a chatroom with Django. Everything works fine but then I wanted to modify it and display the posts written by a user on their profile page so that others can see it, but instead i got this error 'NoReverseMatch at / Reverse for 'user-profile' with arguments '('',)' not found. 1 pattern(s) tried: ['profile/(?P[^/]+)/$']
Here is my View file;
from django.shortcuts import render, redirect
from django.http import HttpResponse
#from django.urls import reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login, logout
from django.db.models import Q
from .models import Message, Room, Topic
from .forms import RoomForm
def userProfile(request, pk):
user = User.objects.get(id=pk)
rooms = user.room_set.all()
context = {'user': user, 'rooms': rooms}
return render(request, 'base/profile.html', context)
here is my URL:
from django.urls import path
from . import views
urlpatterns=[
path('', views.home, name="home"),
path('room/<str:pk>/', views.room, name="room"),
path('profile/<str:pk>/', views.userProfile, name='user-profile'),
]
'Error during template rendering' In template C:\Users\Nonesi\Desktop\StudyBudy\base\templates\base\feed_component.html, error at line 9 so here is my template:
<div>
{% for room in rooms %}
<div>
{% if request.user == room.host %}
<a href="{% url 'update-room' room.id %}">Edit</a>
<a href="{% url 'delete-room' room.id %}">Delete</a>
{% endif %}
<a href="{% url 'user-profile' room.host.id %} ">@{{room.host.username}}</a>
<h5>{{room.id}} -- <a href="{% url 'room' room.id %}">{{room.name}}</a></h5>
<small>{{room.topic.name}}</small>
</div>
<hr>
{% endfor %}
</div>
You can try that :
<div>
{% for room in rooms %}
<div>
{% if request.user == room.host %}
<a href="{% url 'update-room' room.id %}">Edit</a>
<a href="{% url 'delete-room' room.id %}">Delete</a>
{% endif %}
{% if room.host.id %}
<a href="{% url 'user-profile' room.host.id %} ">@{{room.host.username}}</a>
{% endif %}
<h5>{{room.id}} -- <a href="{% url 'room' room.id %}">{{room.name}}</a></h5>
<small>{{room.topic.name}}</small>
</div>
<hr>
{% endfor %}
</div>
Show the url to the profile only if the object exist, that way the reverse match error won't be trigger, of course that need to be improve on your backend code as well.