Django.fun

No reverse match error. Reverse for 'user-profile' with arguments '('',)' not found

I have created a messaging function on my website where logged in and guest users can send messages to other users. I have created an inbox for the logged in users to store these messages. When opening up a message from a guest (someone who doesn't have an account) I get an error: Reverse for 'user-profile' with arguments '('',)' not found.

Based on my digging on the web I think this may be due to the fact that the guest user doesnt have a name attached? Could this be it and how would I fix it?

Views.py:

def createMessage(request,pk):
recipient = Profile.objects.get(id=pk)
form = MessageForm()

try:
    sender = request.user.profile
except:
    sender = None 

if request.method == 'POST':
    form = MessageForm(request.POST)
    if form.is_valid():
        message = form.save(commit=False)
        message.sender = sender
        message.recipient = recipient

        if sender:
            message.name = sender.name
            message.email = sender.email
        message.save()

        messages.success(request, 'Your message was successfully sent!')
        return redirect('user-profile', pk=recipient.id)

context = {'recipient': recipient, 'form': form}
return render(request, 'users/message_form.html', context)

Models.py:

class Message(models.Model):
sender = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True)
recipient = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True, related_name="messages")
name = models.CharField(max_length=200, null=True, blank=True)
email = models.EmailField(max_length=200, null=True, blank=True)
subject = models.CharField(max_length=200, null=True, blank=True)
body = models.TextField()
is_read = models.BooleanField(default=False, null=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__ (self):
    return self.subject

class Meta:
    ordering = ['is_read', '-created']

Message.html:

<a href="{% url 'inbox' %}">
          <h2>
            <span class="badge bg-primary"> <i class="fas fa-arrow-circle-left"></i></span>
          </h2>
        </a>
          <h3 class="text-center">{{message.subject}}</h3>
          <a class="text-decoration-none" href="{% url 'user-profile' message.sender.id %}">{{message.name}}</a>
          <p>{{message.created}}</p>
          <div>
            {{message.body|linebreaksbr}}

          </div>

Answers: 0