如何列出数据库中的所有节点?
How can I list all the nodes in my database?
我正在尝试创建一个简单的模型 Node
和一个在列表中显示我的所有节点的简单网页。但它似乎不起作用,每次我更改代码时都会出现新错误。所以我放弃了,来到 here.This 就是我所做的:
我创建了一个节点模型:
class Node(models.Model):
ID = models.DecimalField(max_digits=9, decimal_places=6)
nb_solenoid = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
connexion = models.CharField(max_length=255)
def get_absolute_url(self):
return reverse("node:index", kwargs={"id": self.id})
使用这种形式:
class NodeForm(forms.ModelForm):
ID = forms.DecimalField(initial=0)
nb_solenoid = forms.DecimalField(initial=1)
connexion = forms.CharField(required=False,
widget=forms.Textarea(
attrs={
"placeholder": "type of connexion"
}))
class Meta:
model = Node
fields = [
'ID',
'nb_solenoid',
'connexion'
]
这是我的 views.py:
def index(request):
queryset = Node.objects.all()
context = {
"object_list": queryset
}
return render(request, "node/index.html", context)
这是我在 urls.py 中的代码:
urlpatterns = [path('', views.index, name='index')]
当我调用此 url 时:http://localhost:8000/node
我现在收到此错误:
NoReverseMatch at /node
Reverse for 'index' with keyword arguments '{'id': 1}' not found. 1 pattern(s) tried: ['node$']
什么是 NoReverseMatch 错误以及如何解决我的问题?假设我是一名 Django 初学者开发人员。
谢谢。
问题是您命名的 url 路径 node:index
没有参数(可能是因为该视图只是列出所有节点,而不是特定节点),但是您的模型 get_absolute_url
试图用 id
的 kwarg 来反转模式。核心问题是你的get_absolute_url
方法;但是,您真的可能也会受益于仅使用基于 class 的通用视图:
urls.py:
urlpatterns = [
path('nodes/', NodeList.as_view(), name="node-list"),
path('node/<id>/', NodeDetail.as_view(), name="node-detail")
]
view.py:
from django.views.generic import ListView, DetailView
from .models import Node
class NodeList(ListView):
model = Node
class NodeDetail(DetailView):
model = Node
models.py:
class Node(models.Model):
<snip>
def get_absolute_url(self):
return reverse("node-detail", kwargs={"id": self.id})
我真的应该提到,基于 class 的通用视图的文档位于:https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
我正在尝试创建一个简单的模型 Node
和一个在列表中显示我的所有节点的简单网页。但它似乎不起作用,每次我更改代码时都会出现新错误。所以我放弃了,来到 here.This 就是我所做的:
我创建了一个节点模型:
class Node(models.Model):
ID = models.DecimalField(max_digits=9, decimal_places=6)
nb_solenoid = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
connexion = models.CharField(max_length=255)
def get_absolute_url(self):
return reverse("node:index", kwargs={"id": self.id})
使用这种形式:
class NodeForm(forms.ModelForm):
ID = forms.DecimalField(initial=0)
nb_solenoid = forms.DecimalField(initial=1)
connexion = forms.CharField(required=False,
widget=forms.Textarea(
attrs={
"placeholder": "type of connexion"
}))
class Meta:
model = Node
fields = [
'ID',
'nb_solenoid',
'connexion'
]
这是我的 views.py:
def index(request):
queryset = Node.objects.all()
context = {
"object_list": queryset
}
return render(request, "node/index.html", context)
这是我在 urls.py 中的代码:
urlpatterns = [path('', views.index, name='index')]
当我调用此 url 时:http://localhost:8000/node
我现在收到此错误:
NoReverseMatch at /node
Reverse for 'index' with keyword arguments '{'id': 1}' not found. 1 pattern(s) tried: ['node$']
什么是 NoReverseMatch 错误以及如何解决我的问题?假设我是一名 Django 初学者开发人员。 谢谢。
问题是您命名的 url 路径 node:index
没有参数(可能是因为该视图只是列出所有节点,而不是特定节点),但是您的模型 get_absolute_url
试图用 id
的 kwarg 来反转模式。核心问题是你的get_absolute_url
方法;但是,您真的可能也会受益于仅使用基于 class 的通用视图:
urls.py:
urlpatterns = [
path('nodes/', NodeList.as_view(), name="node-list"),
path('node/<id>/', NodeDetail.as_view(), name="node-detail")
]
view.py:
from django.views.generic import ListView, DetailView
from .models import Node
class NodeList(ListView):
model = Node
class NodeDetail(DetailView):
model = Node
models.py:
class Node(models.Model):
<snip>
def get_absolute_url(self):
return reverse("node-detail", kwargs={"id": self.id})
我真的应该提到,基于 class 的通用视图的文档位于:https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/