试图在新应用中追踪 NoReverseMatch

Trying to track down NoReverseMatch in new app

所以我做了很多博客教程中的一个,现在我正在开发自己的应用程序。我 运行 遇到了这个错误:

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

这是我的模型文件:

from django.db import models


#Basic tracking information of assets:
#Host name
#IP Address
#CNames assigned in eng.silverspringnet.com
#eng cluster
#Description
#Owner

# Create your models here.

class Asset(models.Model):
    created_by = models.ForeignKey('auth.User')
    created_date = models.DateTimeField()
#    modified_date = models.DateTimeField()
    owner = models.CharField(max_length=100)
    asset_name = models.CharField(max_length=300)
    fqdn = models.CharField(max_length=300)
    ip_address = models.GenericIPAddressField()
    cluster_name = models.CharField(max_length=300)
    description = models.TextField()

    def create_asset(self):
        self.created_date = timezone.now()
        self.save()

    def __str__(self):
        return self.asset_name

以及我的观点:

from django.shortcuts import render_to_response
from django.http import HttpResponse
from .models import Asset 
from pysphere import VIServer
from django.shortcuts import render, get_object_or_404

# Create your views here.

# Huge performance penalty here
#def vm_status(vm_name):
#    server = VIServer()
#    server.connect("it-vcenter-01.silverspringnet.com", "jhamm@silverspringnet.com", "St31l@h@mMn1reug")
#    vm = server.get_vm_by_name(vm_name)
#    vm_stat = vm.get_status(vm)
#    server.disconnect()
#    return vm_stat 

def asset_list(request): 

    assets = Asset.objects.all()

#    for vm in assets:
#        vm_stat = vm_status(vm.fqdn)

    return render_to_response('assets/asset_list.html', {'assets': assets}, content_type="text/html")

def asset_detail(request, asset_name):
    asset_detail = get_object_or_404(asset, asset_name=asset_name)
    return render(request, 'assets/asset_detail.html', {'asset_detail': asset_detail})

URLs:

from django.conf.urls import patterns, include, url
from . import views

urlpatterns = patterns('',
    url(r'^$', views.asset_list),
    url(r'^detail/(?P<asset_name>.*)/$', views.asset_detail),
)

有问题的模板:

<table border="1" style="width:100%">
  <tr>
    <th>Asset Name</th>
    <th>VM Status</th>
    <th>FQDN</th>
    <th>IP Address</th>
    <th>Owner</th>
  </tr>
{% for asset in assets %}  
  <tr>
      <td><a href="{% url assets.views.asset_detail asset_name=asset.asset_name %}">{{ asset.asset_name }}</a></td>
      <td>{{ vmstat }}</td>
      <td>{{ asset.fqdn }}</td>
      <td>{{ asset.ip_address }}</td>
      <td>{{ asset.owner }}</td>
  </tr>
{% endfor %}
</table>

所以我尝试追踪这个问题,但显然我遗漏了一些关于这里工作流程的信息。根据错误,我看到 asset_name 试图通过,在我的 urls.py 中,我对正则表达式非常开放。 (顺便说一句: post_detail.html 只是一个 HI! 来尝试让任何东西工作)。所以我现在有点迷路,任何帮助将不胜感激。我是 Django 的新手,如果这是一个愚蠢的问题,我深表歉意。

首先,为您的 url 模式命名:

url(r'^detail/(?P<asset_name>.*)/$', views.asset_detail, name="asset_detail"),

接下来,使用 url 标签中的名称。对于 Django 1.5 及更高版本,将名称用引号引起来。

{% url "asset_detail" asset_name=asset.asset_name %}">{{ asset.asset_name }}</a></td>