Home > Mobile >  Django NoReverseMatch at /project/360c2471-44ff-4d8c-b536-c9da3448c002/ Reverse for 'conversati
Django NoReverseMatch at /project/360c2471-44ff-4d8c-b536-c9da3448c002/ Reverse for 'conversati

Time:08-31

Hi I am currently facing a problem in redirecting the user to my directs-app. The NewConversation in directs/views.py starts a new conversation. It can be clicked in the profile-page user-profile.html of the users-app. Now I wanna do the same in my single-project.html in my projects-app. But I am getting the error above. Thanks for your help!

directs/views.py

def NewConversation(request, username):
    from_user = request.user
    body = ''
    try:
        to_user = User.objects.get(username=username)
    except Exception as e:
        return redirect('search-users')
    if from_user != to_user:
        Message.sender_message(from_user, to_user, body)
    return redirect('message')

directs/urls.py

urlpatterns = [
    path('new/<username>', views.NewConversation, name="conversation"),
]

users/models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    username = models.CharField(max_length=200, blank=True)

users/views.py

def userProfile(request, pk):
    profile = Profile.objects.get(id=pk)
    context = {'profile':profile}
    return render(request, 'users/user-profile.html', context)

users/user-profile.html

          <h2 >{{profile.name}}</h2>
          <img  src="{{profile.profile_image.url}}" />
          <a href="{% url 'conversation' profile.user  %}" >Message</a>

projects/views.py

def project(request, pk):
    projectObj = Project.objects.get(id=pk)
    context = {'project':projectObj}
    return render(request, 'projects/single-project.html', context)

projects/models.py

class Project(models.Model):
    owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

projects/single-project.html

<div  style="margin-top: 20px;">
    <a href="{% url 'conversation' project.owner.user  %}">Send message</a>
</div>

projects.urls.py

urlpatterns = [
    path('project/<str:pk>/', views.project, name='project'),
]

CodePudding user response:

I think you should also post the entire traceback in your post for more coherence.

Let me start with the error:

NoReverseMatch at /project/360c2471-44ff-4d8c-b536-c9da3448c002/ Reverse for 'conversation' with arguments '('',)' not found. 1 pattern(s) tried: ['message/new/(?P<username>[^/] )\\Z']

Your url for conversation is declared as follows:

    path('new/<username>', views.NewConversation, name="conversation"),
]

What you should improve here: First you should declare what type of argument you are expecting. <str:username> I think you are expecting the string.

Now let's look at the error. You are receiving the argument '' which is an empty string. The urlpath conversation is expecting something that is not an empty string. Why though it is an empty string?

{% url 'conversation' profile.user %} and {% url 'conversation' project.owner.user %} profile.user will not be what you are expecting as there is no specific representation given to profile.user that django templates knows how to translate. Instead, you are giving it the entire User object. I suggest you to either put there profile.user.username, or adding the ___str___ method to your Profile Model.

Something like:

def __str__(self):
      return self.username

I also recommend to improve your function based views by telling what kind of request are you expecting (here it is a GET I assume).

Also, have a look here

  • Related