Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Question: I get this traceback when browsing to <https://monajalal.pythonanywhere.com/>
and the complete code can be found here
<https://github.com/monajalal/FirstDjangoApp> :

    
    
    NoReverseMatch at /
    Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    Request Method: GET
    Request URL:    https://monajalal.pythonanywhere.com/
    Django Version: 1.9.8
    Exception Type: NoReverseMatch
    Exception Value:    
    Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    Exception Location: /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508
    Python Executable:  /usr/local/bin/uwsgi
    Python Version: 3.5.1
    Python Path:    
    ['/var/www',
     '.',
     '',
     '/var/www',
     '/home/monajalal/FirstDjangoApp/myvenv/lib/python3.5',
     '/home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/plat-x86_64-linux-gnu',
     '/home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/lib-dynload',
     '/usr/lib/python3.5',
     '/usr/lib/python3.5/plat-x86_64-linux-gnu',
     '/home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages',
     '/home/monajalal/FirstDjangoApp']
    Server time:    Mon, 18 Jul 2016 20:31:01 -0500
    Error during template rendering
    
    In template /home/monajalal/FirstDjangoApp/blog/templates/blog/post_list.html, error at line 0
    Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    1   {% extends 'blog/base.html' %}
    2   
    3   {% block content %}
    4       {% for post in posts %}
    5           <div class="post">
    6               <div class="date">
    7                   {{ post.published_date }}
    8               </div>
    9               <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
    10              <p>{{ post.text|linebreaksbr }}</p>
    Traceback Switch to copy-and-paste view
    
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/defaulttags.py in render
                                  current_app=current_app) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py in reverse
        return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py in _reverse_with_prefix
                                 (lookup_view_s, args, kwargs, len(patterns), patterns)) ...
    ▶ Local vars
    During handling of the above exception (Reverse for 'mysite.post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []), another exception occurred:
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py in get_response
                        response = self.process_exception_by_middleware(e, request) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py in get_response
                        response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/blog/views.py in post_list
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/shortcuts.py in render
                template_name, context, request=request, using=using) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/loader.py in render_to_string
            return template.render(context, request) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/backends/django.py in render
                return self.template.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render
                        return self._render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in _render
            return self.nodelist.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render
                    bit = node.render_annotated(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render_annotated
                return self.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/loader_tags.py in render
            return compiled_parent._render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in _render
            return self.nodelist.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render
                    bit = node.render_annotated(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render_annotated
                return self.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/defaulttags.py in render
                    return nodelist.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render
                    bit = node.render_annotated(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/base.py in render_annotated
                return self.render(context) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/defaulttags.py in render
                            six.reraise(*exc_info) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/utils/six.py in reraise
            raise value ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/template/defaulttags.py in render
                url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py in reverse
        return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) ...
    ▶ Local vars
    /home/monajalal/FirstDjangoApp/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py in _reverse_with_prefix
                                 (lookup_view_s, args, kwargs, len(patterns), patterns)) ...
    ▶ Local vars
    

I have this in views.py:

    
    
    from django.shortcuts import render, get_object_or_404
    from django.utils import timezone
    from .models import Post
    from .forms import PostForm
    
    # Create your views here.
    def post_list(request):
        posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
        return render(request, 'blog/post_list.html', {'posts': posts})
    
    def post_detail(request, pk):
        post = get_object_or_404(Post, pk=pk)
        return render(request, 'blog/post_detail.html', {'post': post})
    
    def post_new(request):
        if request.method == "POST":
            form = PostForm(request.POST)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.published_date = timezone.now()
                post.save()
                return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm()
        return render(request, 'blog/post_edit.html', {'form': form})
    
    def post_edit(request, pk):
        post = get_object_or_404(Post, pk=pk)
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.published_date = timezone.now()
                post.save()
                return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
        return render(request, 'blog/post_edit.html', {'form': form})
    

And the following in urls.py in blog folder:

    
    
    from django.conf.urls import url
    from django.core.urlresolvers import reverse  
    from . import views
    
    urlpatterns = [
        url(r'^$', views.post_list, name='post_list'),
        url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
        url(r'^post/new/$', views.post_new, name='post_new'),
        url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
    ]
    

Can you please hint what the problem is?


Answer: Either you need to specify the app name in the url namespace:

See this -> <https://docs.djangoproject.com/en/1.9/topics/http/urls/#url-
namespaces>

Or else you need to write your URLs in your html templates like this:

    
    
        <h1><a href="{% url 'app_name:post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
    

i.e., you'll have to specify the app name in the url in html while reversing,
if you have not specified the app name in your namespace for URLs.

Hope this will resolve the issue.

