使用参数'()'和关键字参数'{}'找不到'index'

时间:2017-04-05 07:25:42

标签: python django

我在加载索引页面时遇到问题,我已经尝试了两天,但它对我不起作用。

错误:

Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

my views.py:

def index(request):
    invoices = 
    InvoiceHeaders.objects.filter(user=request.user.id).order_by('-
    status', 'number')
    return render(request, 'invoices/index.html' ,
                          {'invoices' : invoices,
                          'current_site' : request.META['HTTP_HOST'],
                          'current_path' : request.META['PATH_INFO'],
                          'r' : request.path , })

我的发票网址:

from django.conf.urls import url, include
from django.conf import settings


from . import views 

app_name = 'invoices'

urlpatterns = [

    url(r'^$', views.index,  name='index'),
    url(r'^new/$', views.new,  name='new'),  

]

我的基本网址:

urlpatterns = [

    url(r'^invoices/',  include('invoices.urls',  
    namespace='invoices')),


   ]

invoices.html

{% extends "base.html" %}
{% load i18n %}
{% block title %}{%trans "Invoices" %}{% endblock %}


{% block content %}

<table>
    {% for invoiceheader in invoices %}
        <tr>
            <td> <label>{%trans "Number" %}:</label>
                {% if invoiceheader.number %}
                {{ invoiceheader.number }}
                {% else %}
                -
                {% endif %}
            </td>
            <td> <label>{%trans "Date Issue" %}:</label>
            {% if invoiceheader.dateissue %}
                {{ invoiceheader.dateissue }}
            {% else %}
                -
            {% endif %}
            </td>
            <td> <label>{%trans "Custumer" %}:</label>{{ 
    invoiceheader.customer_name }}</label> </td>
            <td> <a href="{% url 'invoices:status' 
    invoiceheader.id%}">{%trans "Status" %}</a> {{ 
    invoiceheader.get_status_display }} </td>

            {% if invoiceheader.status == 'I' or invoiceheader.status 
    == 'P' %}
                <td> <label class="getitinfo" findrandomnumber="{{ 
    invoiceheader.findrandomnumber }}" number="{{ invoiceheader.number 
    }}"> 
    {%trans "Get It" %} </label> </td>
            {% else %}
                <td>&nbsp;</td>
            {% endif %}

            {% if invoiceheader.status == 'D' %}
                <td> <a href="{% url 'invoices:update' 
invoiceheader.id%}"> {%trans "Update" %} </a> </td>
                <td> <a href="{% url 'invoices:pdfhtml' 
invoiceheader.id%}">{%trans "pdf" %} </a> </td>
                <td> <a href="{% url 'invoices:delete' 
 invoiceheader.id%}">{%trans "delete" %} </a> </td>
            {% else %}
                <td>&nbsp;</td>
                <td> <label href="{% url 'invoices:download' 
 invoiceheader.id%}">{%trans "pdf" %}</label></td>
                <td>&nbsp;</td>
            {% endif %}

            {% if debug %}
                <td> {{ invoiceheader.id }} </td>
                <!--td> {{ invoiceheader.findrandomnumber }} </td-->
            {% endif %}

            <td> <label>{{ invoiceheader.status }} </label> </td>
         </tr>
    {% endfor %}
  </table>

  {% endblock %}

我的base.html

{% load i18n %}

<body>

<div id="wrapper">
    <div id='header'>
        <div id='logo'>
        <h1> 
            <a href="{% url 'invoices:index' %}">
                <span> web </span> {%trans 'Invoices' %}
            </a>
        </h1>
    </div>
    <div class="clear"></div>
    <div id="menu">
        <ul>


            {% if path == 'invoices' %}
                <li class="current_page_item"> <a href="{% url 
 'invoices:index' %}"> {%trans 'Invoices' %}</a></li>
            {% else %}
                <li> <a href="{% url 'invoices:index' %}"> {%trans 
'Invoices' %}</a></li>
            {% endif %}      

        </ul>
    </div>
</div>

  <div class='clear'> </div>
  {% if error %}
  <div id='error'>
  {{ error }}
 </div>
 {% endif %}
 <div class='clear'> </div>

 <div id="content">

    {% block content %}{% endblock %}
  </div>
</body>
</html>

回溯:

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/core/handlers/exception.py" in inner
39.             response = get_response(request)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in _legacy_get_response
249.             response = self._get_response(request)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in _get_response
187.                 response = 
self.process_exception_by_middleware(e, request)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in _get_response
185.                 response = wrapped_callback(request, 
*callback_args, **callback_kwargs)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/contrib/auth/decorators.py" in _wrapped_view
23.                 return view_func(request, *args, **kwargs)

File "/home/wdalnor/Documents/go/oweb/invoices/views.py" in index
70.                               'r' : request.path , })

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/shortcuts.py" in render
30.     content = loader.render_to_string(template_name, context, 
request, using=using)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/loader.py" in render_to_string
 68.     return template.render(context, request)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/backends/django.py" in render
66.             return self.template.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render
208.                     return self._render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in _render
199.         return self.nodelist.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render
994.                 bit = node.render_annotated(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render_annotated
961.             return self.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/loader_tags.py" in render
174.         return compiled_parent._render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in _render
199.         return self.nodelist.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render
994.                 bit = node.render_annotated(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render_annotated
961.             return self.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/defaulttags.py" in render
315.                 return nodelist.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render
994.                 bit = node.render_annotated(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/base.py" in render_annotated
 961.             return self.render(context)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/template/defaulttags.py" in render
439.             url = reverse(view_name, args=args, kwargs=kwargs, 
current_app=current_app)

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/urls/base.py" in reverse
91.     return 
force_text(iri_to_uri(resolver._reverse_with_prefix(view, 
prefix, *args, **kwargs)))

File "/home/wdalnor/Documents/go/local/lib/python2.7/site-
packages/django/urls/resolvers.py" in _reverse_with_prefix
392.             (lookup_view_s, args, kwargs, len(patterns), 
patterns)

Exception Type: NoReverseMatch at /invoices
Exception Value: Reverse for 'index' with arguments '()' and keyword 
arguments '{}' not found. 0 pattern(s) tried: []

我的发票/ index.html

{% extends "base.html" %}
{% load i18n %}
{% block title %}{%trans "Invoices" %}{% endblock %}

{% block js %}
$('div#getit').hide();

$('#closegetit').click(function(){
$('div#getit').hide('slow');
});


$("label.getitinfo").click(function(){
$('div#getit').slideDown('slow');
$('#getitclass').html("{%trans "link to invoice #" %} " +  
$(this).attr('number') + " <br/> http://{{current_site }}\{{ 
current_path 
}}getit/" +  $(this).attr('findrandomnumber') )
});

{% endblock %}




{% block content %}

   <table>
    {% for invoiceheader in invoices %}
        <tr>
            <td> <label>{%trans "Number" %}:</label>
            {% if invoiceheader.number %}
                {{ invoiceheader.number }}
            {% else %}
                -
            {% endif %}
            </td>
            <td> <label>{%trans "Date Issue" %}:</label>
            {% if invoiceheader.dateissue %}
                {{ invoiceheader.dateissue }}
            {% else %}
                -
            {% endif %}
            </td>
            <td> <label>{%trans "Custumer" %}:</label>{{ 
invoiceheader.customer_name }}</label> </td>
            <td> <a href="{% url 'invoices:status' 
invoiceheader.id%}">{%trans "Status" %}</a> {{ 
invoiceheader.get_status_display }} </td>

            {% if invoiceheader.status == 'I' or invoiceheader.status 
== 'P' %}
                <td> <label class="getitinfo" findrandomnumber="{{ 
invoiceheader.findrandomnumber }}" number="{{ invoiceheader.number 
}}"> 
{%trans "Get It" %} </label> </td>
            {% else %}
                <td>&nbsp;</td>
            {% endif %}

            {% if invoiceheader.status == 'D' %}
                <td> <a href="{% url 'invoices:update' 
invoiceheader.id%}"> {%trans "Update" %} </a> </td>
                <td> <a href="{% url 'invoices:pdfhtml' 
invoiceheader.id%}">{%trans "pdf" %} </a> </td>
                <td> <a href="{% url 'invoices:delete' 
invoiceheader.id%}">{%trans "delete" %} </a> </td>
            {% else %}
                <td>&nbsp;</td>
                <td> <label href="{% url 'invoices:download' 
invoiceheader.id%}">{%trans "pdf" %}</label></td>
                <td>&nbsp;</td>
            {% endif %}

            {% if debug %}
                <td> {{ invoiceheader.id }} </td>
                <!--td> {{ invoiceheader.findrandomnumber }} </td-->
            {% endif %}

            <td> <label>{{ invoiceheader.status }} </label> </td>
         </tr>
    {% endfor %}
</table>
<p>
<li><a href="{% url 'new' %}">{%trans "new" %}</a></li>
</p>

{% endblock %}

2 个答案:

答案 0 :(得分:2)

该错误表明您的模板中某处{% url 'index' %}

它应该是{% url 'invoices:index' %}和其他地方一样。

这可能在您未展示的base.html中。

答案 1 :(得分:0)

我不知道您如何传递浏览器网址,但尝试将您的网址(r'^ $',views.index,name ='index')更改为网址(r'^ index / $', views.index,name ='index'),

我建议您查看您的网址格式