pythondjangodjango-urlsdjango-comments

Django-Fluent-Comments - NoReverseMatch at /rohit/post/new/


i tried every thing but i donot know how to remove this error. need help guys. i am using django-fluent-comments for comments and when i put this line

{% render_comment_form for object %}

to get form for comments. i got this error:

NoReverseMatch at /rohit/post/new/

Reverse for 'comments-post-comment' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P\w+)/post/(?P[\w-]+)/comments/post/$']

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/rohit/post/new/

Django Version: 1.10.2
Python Version: 3.4.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'pagination',
 'postman',
 'pinax.notifications',
 'fluent_comments',
 'crispy_forms',
 'django_comments',
 'mailer',
 'student',
 'teacher',
 'classroom',
 'bootstrapform',
 'posts',
 'background']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'pagination.middleware.PaginationMiddleware']


Template error:
In template C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\fluent_comments\templates\comments\form.html, error at line 7
   Reverse for 'comments-post-comment' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P<username>\\w+)/post/(?P<slug>[\\w-]+)/comments/post/$']   1 : {% load comments i18n crispy_forms_tags fluent_comments_tags %}
   2 : 
   3 : {% if not form.target_object|comments_are_open %}
   4 :     <p>{% trans "Comments are closed." %}</p>
   5 : {% else %}
   6 :   {% if not form.helper or not form.helper.form_tag %}
   7 :     <form id="comment-form-{{ form.target_object.pk }}" data-object-id="{{ form.target_object.pk }}" action=" {% comment_form_target %} " method="post" class="{% block form_class %}{{ form.helper.form_class|default:'js-comments-form comments-form form-horizontal' }}{% endblock %}"
   8 :           data-ajax-action="{% url 'comments-post-comment-ajax' %}">
   9 :       {% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
   10 :   {% endif %}
   11 : 
   12 :       {% block comment_form %}
   13 :         {% block form_fields %}
   14 :           {% crispy form %}
   15 :         {% endblock %}
   16 : 
   17 :         {% block form_actions %}


Traceback:

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
  249.             response = self._get_response(request)

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

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

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "C:\Users\rohit\Desktop\GBPEC\portal\GBPEC_PORTAL\classroom\views.py" in post_full
  230.  return render(request, "post_detail.html", context)

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

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

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  208.                     return self._render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django_comments\templatetags\comments.py" in render
  191.             formstr = render_to_string(template_search_list, context_dict)

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

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  208.                     return self._render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\defaulttags.py" in render
  315.                 return nodelist.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\defaulttags.py" in render
  315.                 return nodelist.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django\template\library.py" in render
  203.         output = self.func(*resolved_args, **resolved_kwargs)

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django_comments\templatetags\comments.py" in comment_form_target
  339.     return django_comments.get_form_target()

File "C:\Users\rohit\Desktop\GBPEC\portal\lib\site-packages\django_comments\__init__.py" in get_form_target
  74.         return reverse("comments-post-comment")

File "C:\Users\rohit\Desktop\GBPEC\portal\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\rohit\Desktop\GBPEC\portal\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  392.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /rohit/post/new/
Exception Value: Reverse for 'comments-post-comment' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P<username>\\w+)/post/(?P<slug>[\\w-]+)/comments/post/$']

i have used this for comment url:

url(r'^(?P<username>\w+)/post/(?P<slug>[\w-]+)/comments/', include('fluent_comments.urls')),

My Template:

<div class="mdl-card__title" style="margin: 0 auto;">
                          <h2 class="mdl-card__title-text">{{ post.title }}</h2>
                        </div>
                        <div class="mdl-card__supporting-text" style="margin:0 auto;">
                         <p> Content : {{ post.content|linebreaksbr }}</p>
                         <p>
                         {% if file %}
                         Files : 
                         {% for f in file %}
                         <a href="{{ f.file.url }}">{{ f.filename }}</a>
             <br>
                         {% endfor %}
                        {% endif %}
                         </p>
                         <p>
                        {{ post.post_type }}
                        </p>
                        {{ post.timestamp|timesince }} ago by {{ post.user }}
                        </div>
             {% if author %}
                        <div class="mdl-card__actions mdl-card--border">
                          <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect mdl-button--colored" data-upgraded=",MaterialButton,MaterialRipple"  href="/{{ request.user.username }}/post/{{ post.slug }}/edit/">Edit Post</a>
                          <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect mdl-button--accent" data-upgraded=",MaterialButton,MaterialRipple"  href="/{{ request.user.username }}/post/{{ post.slug }}/delete/">Delete Post</a>
                        </div>
              {% endif %}
              
                    </div>
          {% render_comment_list for object %}
          {% render_comment_form for object %}
                </div>

            </div>
        </div>

Solution

  • The error occurs as a result of the url you have specified. Any named url in the fluent_comments.urls you have included in your ^(?P<username>\w+)/post/(?P<slug>[\w-]+)/comments/ will need to be passed a username and slug objects.

    When django-fluent-comments try to render {% render_comment_form for object %} it will use a reverse url tag, but it wasn't passed a username or slug objects.

    Instead, you can either do what the django-fluent-comments authors have suggested and add the following to your url.py

    urlpatterns += patterns('',
        url(r'^blog/comments/', include('fluent_comments.urls')),
    )
    

    or remove the username and slug objects.

    If you really want to add them, than you can override the templates in django-fluent-comments such that every url tag that uses a url name is passed a username and slug objects.

    Check out the Django Urls section for more information.