简体   繁体   中英

I could not figure out why the url pattern cannot be matched in Django

urls.py

tmp_urlpatterns = [
    path('',MsgList.as_view(),name='user_inbox'),
    path('outbox/',MsgOutbox.as_view(),name='user_outbox'),
    path('<int:mid>/',MsgRead.as_view(),name='user_msgread'),
    path('<int:mid>/reply/',MsgReply.as_view(),name='user_msgreply'),
    path('send/<int:mid>/',MsgSend.as_view(),name='user_msgsend'),
]

views.py

class MsgReply(LoginRequiredMixin,CreateView):
    extra_context = {'title':'回覆訊息'}
    model = Message
    fields = ['title','body']

    def get_initial(self):
        self.msg = Message.objects.get(id=self.kwargs['mid'])
        return {
            'title':'Re: '+self.message.title,
            'body':'{}({}) 於 {} 寫道:\n> {}'.format(
                self.msg.sender.username,
                self.msg.sender.first_name,
                self.msg.created,
                '\n> '.join(self.msg.body.split('\n'))
            ),
        }

message_detail.html

<a href="{% url 'user_msgreply' message.id %}" class="btn btn-sm btn-secondary">
        <i class="fas fa-reply"></i> 回覆
</a>

Error:

NoReverseMatch at /user/msg/1/
Reverse for 'user_msgreply' with arguments '('',)' not found. 1 pattern(s) tried: ['user/msg/(?P<mid>[0-9]+)/reply/$']

For the url patterns, I had added the route user_msgreply and when I try to click into the hyperlink, the error message appears and I could not figure out where is the problem since in the html template I had put the parameter like {% url 'user_msgreply' message.id %} . So can anyone help me to find out what I am missing?

try this: {% url 'user_msgreply' request.message.id %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM