找不到页面(404)-当前路径editEstHab /与以下任何路径都不匹配

时间:2019-11-16 01:37:00

标签: python django django-models django-forms django-views

我想更新现有对象时遇到问题...在另一个项目中,我使用了类似的代码行,但是现在,当我要保存实际信息时,它不起作用。

更奇怪的是...最后说当前路径editEstHab /与其中任何一个都不匹配。,但是当我搜索项目时,这是唯一一次我在urls.py中使用 editEstHab / 。 所以...帮助:(我不知道我的错误是什么。

models.py

class habitacion(models.Model):
    nroHabitacion = models.IntegerField(null=False)
    tipoHabitacion = models.CharField(max_length=70, null=True)
    tipoCama = models.ForeignKey(tipoCama, on_delete=models.CASCADE, blank=True, null=False)
    accesorios = models.CharField(max_length=70, null=True)
    precio = models.IntegerField(null=False)
    estado_habitacion = models.ForeignKey(estadoHab, on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.tipoHabitacion

forms.py

class UpdateHabForm(forms.ModelForm):
     class Meta:
         model = habitacion
         fields = ['estado_habitacion']

views.py

def editHab(request,id_habitacion):
    # llamando datos de habitacion seleccionada
    hab = habitacion.objects.get(id=id_habitacion)
    if request.method == 'GET':
        form = UpdateHabForm(instance=hab) 
    else:
        form = UpdateHabForm(request.POST, instance=hab)
        if form.is_valid():
            form.save()
    context = {
        'form' : form,
        'hab' : hab
    }
    return render(request,"editEstHab.html",context)

urls.py

    path('editEstHab/<id_habitacion>', views.editHab, name="editEstHab"),

Error ScreenShot

1 个答案:

答案 0 :(得分:0)

似乎错误是POST请求的网址

如果将html表单的action属性保留为空白,则它应该可以正常工作

<form class="" action="" method="post">
  {% csrf_token %}
  {% for field in form %}
  {{field}}
  {% endfor %}
  <button type="submit" name="button">UPDATE</button>

</form>
相关问题