gpt4 book ai didi

django - 试图在新应用中追踪 NoReverseMatch

转载 作者:行者123 更新时间:2023-12-02 03:29:03 27 4
gpt4 key购买 nike

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

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(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>

关于django - 试图在新应用中追踪 NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28417484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com