0

I have a Model called Message that I use django-guardian for object level permission. The idea is that both the from_user and to_user in a message instance should have the can_view_message permission. This function is to display a list of messages with the same message thread_id (automatically created when a message was created).

@permission_required_or_403('can_view_message', (Message, 'pk', 'pk'))
def message_thread(request, pk):
    selected_message = Message.objects.get(pk=pk)
    threads = Message.objects.filter(thread_id=selected_message.thread_id)

    context = RequestContext(request, {
        'latest_messages': threads,
    })
    return render(request, 'myapp/messages_thread.html', context)

The url pattern for this request: url(r'^messages/([-_a-z0-9]{1,32})/$', message_views.message_thread),

Here's the Message Model:

class Message(models.Model):
    from_user = models.ForeignKey(User, verbose_name='From', related_name='%(class)s_from_user')
    to_user = models.ForeignKey(User, verbose_name='To', related_name='%(class)s_to_user')
    send_time = models.DateTimeField('Send Time', blank=True)
    msg_content = models.TextField('Message')
    thread_id = models.CharField('Thread', max_length=50, blank=True)

    class Meta:
        permissions = (
            ('can_view_message', 'Can View Message'),
        )

I use post_create signal to assign permission:

def view_message(sender, instance, **kwargs):
    assign_perm('can_view_message', instance.from_user, instance)
    assign_perm('can_view_message', instance.to_user, instance)

post_save.connect(view_message, sender=Message, dispatch_uid="view_message_permission")

However I'm getting the following error but don't really know why:

GuardianError at /myapp/messages/13/
Argument pk was not passed into view function
Request Method: GET
Request URL:    http://127.0.0.1:8000/myapp/messages/13/
Django Version: 1.8.4
Exception Type: GuardianError
Exception Value:    
Argument pk was not passed into view function
Exception Location: /Library/Python/2.7/site-packages/guardian/decorators.py in _wrapped_view, line 111
Python Executable:  /usr/bin/python
Python Version: 2.7.6

Any help will be much appreciated, thank you so much!

2
  • 1
    Update your question with the url pattern for the view. Oct 29, 2015 at 5:03
  • @BurhanKhalid Thank you so much, with your comment I figured out why! I updated the question too, and going to answer it myself. Thank you so much! Oct 29, 2015 at 5:06

1 Answer 1

0

With @Burhan Khalid's comment, I checked my request url, and it was url(r'^messages/([-_a-z0-9]{1,32})/$', message_views.message_thread),

I changed it to url(r'^messages/(?P<pk>[-\w]+)/$', message_views.message_thread), with the pk in the request, now it works perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.