У меня возникает эта ошибка, когда я пытаюсь щелкнуть, чтобы отредактировать уже отправленную форму, это как если бы я не передавал arvore.id в шаблоне ver_arvore. Когда я меняю arvore.id на число (например, 1), я вижу все свои записи в базе данных, в противном случае у меня возникает ошибка NoReverse Match. Это как если бы представление ver_arvore не сохраняет мой словарь, как показано в трассировке
Мои модели:
class Arvore(models.Model):
tag = models.CharField(max_length=255)
nome = models.CharField(max_length=255)
nivel = models.CharField(
default='Planta',
max_length=50,
choices=(
('Planta', 'Planta'),
('Sistema', 'Sistema'),
('Equipamento', 'Equipamento'),
('Peça', 'Peça'),
)
)
criticidade = models.TextField()
requisitos = models.TextField(blank=True, null=True)
descricao = models.TextField(blank=True, null=True)
fabricante = models.CharField(max_length=255, blank=True, null=True)
modelo = models.CharField(max_length=255, blank=True, null=True)
pai = models.CharField(max_length=255, blank=True, null=True)
localizacao = models.CharField(max_length=250,blank=True, null=True)
link_historico = models.CharField(max_length=255, blank=True, null=True)
link_pm = models.CharField(max_length=255, blank=True, null=True)
link_foto = models.ImageField(blank=True, null=True)
def __str__(self):
return self.nome
class Meta:
managed = True
db_table = 'arvore'
class FormArvore(forms.ModelForm):
class Meta:
model = Arvore
мой URL:
app_name = 'arvore'
urlpatterns = [
path('', views.ver_arvore, name='ver_arvore'),
path('cadastro_arvore/', views.cadastro_arvore, name='cadastro_arvore'),
path('editar_arvore/<int:id>/', views.editar_arvore, name='editar_arvore'),
path('deletar_arvore/<int:id>/', views.deletar_arvore, name='deletar_arvore'),
]
Мои представления :
def cadastro_arvore(request):
if request.method != 'POST':
form = FormArvore
return render(request, 'arvore/cadastro_arvore.html', {'form': form})
form = FormArvore(request.POST, request.FILES)
if not form.is_valid():
form = FormArvore(request.POST)
return render(request, 'arvore/cadastro_arvore.html', {'form': form})
form.save()
return redirect('arvore:cadastro_arvore')
def ver_arvore(request):
arvore = Arvore.objects.all().order_by('id')
paginator = Paginator(arvore, 9)
page = request.GET.get('page')
arvore = paginator.get_page(page)
pesquisa_id = request.GET.get('pesquisa_id')
pesquisa_nome = request.GET.get('pesquisa_nome')
pesquisa_tag = request.GET.get('pesquisa_tag')
pesquisa_nivel = request.GET.get('pesquisa_nivel')
if pesquisa_id:
arvore = Arvore.objects.filter(id=pesquisa_id)
if pesquisa_nome:
arvore = Arvore.objects.filter(nome=pesquisa_nome)
if pesquisa_tag:
arvore = Arvore.objects.filter(tag=pesquisa_tag)
if pesquisa_nivel:
arvore = Arvore.objects.filter(nivel=pesquisa_nivel)
return render(request, 'arvore/ver_arvore.html', {
'arvore': arvore
})
def editar_arvore(request, id):
arvore = get_object_or_404(Arvore, pk=id)
form = FormArvore(instance=arvore)
if request.method != 'POST':
return render(request, 'arvore/editar_arvore.html', {'form': form, 'arvore': arvore})
form = FormArvore(request.POST, instance=arvore)
if not form.is_valid():
return render(request, 'arvore/editar_arvore.html', {'form': form, 'arvore': arvore})
arvore.save()
return redirect('arvore:ver_arvore')
def deletar_arvore(request, id):
arvore = get_object_or_404(Arvore, pk= id)
arvore.delete()
return redirect('arvore:ver_arvore')
ver_arvore. html
{% for equipamentos in arvore %}
<tr>
<td>{{ equipamentos.id }}</td>
<td>{{ equipamentos.tag }}</td>
<td><a href="{%url 'arvore:editar_arvore' arvore.id %}"> {{ equipamentos.nome }}</a></td>
<td>{{ equipamentos.nivel }}</td>
<td>{{ equipamentos.fabricante}}</td>
<td>{{ equipamentos.modelo }}</td>
</tr>
{%endfor%}
editar_arvore. html
<form action="{% url 'arvore:editar_arvore' arvore.id %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend><h2>Registro Árvore de Equipamentos</h2></legend>
<table class="table">
{{ form }}
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary">Adicionar</button>
<button type="reset" class="btn btn-primary">Cancelar</button>
</td>
</tr>
</table>
</fieldset>
</form>
<hr>
<form action="{% url 'arvore:deletar_arvore' arvore.id %}">
{% csrf_token %}
<td>
<tr>
<input type="submit" class="btn btn-default" value="Deletar">
</tr>
</td>
</form>
{% endblock %}
traceback: Di c at ver_arvore isnt рабочий