没有找到参数的Django NoReverseMatch异常

问题描述 投票:0回答:1

我的应用程序的基本结构是一个有评论的博客。

forms.朋友

class CommForm(forms.ModelForm):

    class Meta:
        model = Comm_Item
        fields = ('comm_type', 'title', 'descr',)
        labels = {
            'comm_type': ('Type of Communication'),
            'title': ('Title'),
            'descr': ('Details'),
        }

class comment_form(forms.ModelForm):

    class Meta:
        model = comment
        fields = {'content',}
        labels = {
            'content':('Comment'),
        }

models.朋友

class Comm_Item(models.Model):
    comm_types = [('Inventory','Inventory'),('Chemo','Chemo'),('Operation', 'Operation'), ('HomeIV','HOMEIV'),]
    title = models.CharField(max_length = 200, unique = True, help_text = "<br/>This could be drug name, or shift name, refer to guidelines of use.")
    comm_type = models.CharField(max_length=10, choices=comm_types, default = 'OPS')
    descr = models.TextField(help_text="<br/>Be as DESCRIPTIVE as possible [WHO, WHAT, WHEN, WHERE]")
    date = models.DateField(auto_now=True)
    visible = models.BooleanField(default=True)

class comment(models.Model):
    content = models.TextField()
    date_time = models.DateField(auto_now=True)
    link_to_comm = models.ForeignKey('staffcom.Comm_Item', 
        related_name = "comments" ,on_delete = models.CASCADE)
    username = models.ForeignKey(User, default = "")

URLs.朋友

# methods to access details of a post
    url(r'^detail/(?P<pk>\d+)/$', staff_view.DetailPostView.as_view(), name = 'details'),
    # methods to add objects for posts(communication) and comments
    url(r'^new_comm/$', staff_view.form_view, name ='add_communication'),
    url(r'^detail/(?P<pk>\d+)/comment/new$', staff_view.comment_view, name='add_comment'),
]

detail.html

<div class='container'>
    <h2>{{ post.title }}</h2>
    <h3><span class="label label-info">{{post.date}}</span></h3>
    <br>
    <div class ='well'><h5>{{ post.descr }}</h5></div>
    <a href="{% url 'add_comment' pk=post.id %}">Add A Comment</a>

views.朋友

class DetailPostView(DetailView):
    context_object_name = 'post'
    template_name = 'staffcom/detail.html'
    model = Comm_Item
    def get_context_data(self, **kwargs):
        post_pk = self.kwargs['pk']
        context = super(DetailPostView, self).get_context_data(**kwargs)
        #context['exp'] = Drug.objects.get(pk = drug_Pk).expiration_dates.all().order_by('expirationDate')
        return context

    def comment_view(request,pk):
        comm_form = comment_form()
        print(request.POST.get('pk'))
        if request.method == 'POST':
            form = comment_form(request.POST)
            if form.is_valid():
                usn = request.user.get_username()
                print(usn)
                new_com = comment()
                new_com.content = form.cleaned_data.get('content')
                print(new_com.content)
                new_com.link_to_comm = Comm_Item.objects.get(pk = request.POST.get('pk'))
                new_com.username = User.objects.get(username = usn)
                new_com.save(commit=True)
                return redirect('details',pk = self.kwargs ['pk'])

        return render(request, 'staffcom/comment.html', {'form': comm_form})

所以我想要做的是通过帖子的详细信息页面添加评论发布(Comm_Item)。详细信息页面正常工作,但是当我单击链接添加注释时,注释表单不会呈现。看来get请求没有得到满足。

Reverse for 'add_comment' with no arguments not found. 1 pattern(s) tried: ['staffcom/detail/(?P<pk>\\d+)/comment/new$']

如果我为request.method ==“获取异常而不是将其留给要解析的函数的剩余部分,它会有帮助吗?

这是完整的痕迹:

    Traceback:

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\hiren\Desktop\Python Programs\django projects\blog\RWC Comm Board\Communication_Board\staffcom\views.py" in comment_view
  72.       return render(request, 'staffcom/comment.html', {'form':comm_form, 'post':post_object})

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\loader.py" in render_to_string
  68.     return template.render(context, request)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render
  207.                     return self._render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\test\utils.py" in instrumented_test_render
  107.     return self.nodelist.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\test\utils.py" in instrumented_test_render
  107.     return self.nodelist.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\template\defaulttags.py" in render
  458.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Users\hiren\Anaconda2\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  497.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /staffcom/detail/14/comment/new/
Exception Value: Reverse for 'add_comment' with no arguments not found. 1 pattern(s) tried: ['staffcom/detail/(?P<pk>\\d+)/comment/new/$']
python django
1个回答
0
投票

修复它,问题出在comment.html模板中:url没有传递任何参数(post.pk),这就是它无法正常工作的原因。对不起大家!这是一个愚蠢的错误,谢谢!

{% block content %}
<div class='container'>
    <form action="{% url 'comment_new' %}" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input class ="btn btn-info btn-lg" type="submit" value="Submit"/>
</div>
{% endblock %}
© www.soinside.com 2019 - 2024. All rights reserved.