试图在新应用中追踪NoReverseMatch

时间:2015-02-09 19:13:57

标签: django django-views

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

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的新品牌,如果这是一个愚蠢的品牌,请道歉。

1 个答案:

答案 0 :(得分:1)

首先,命名您的网址格式:

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>