【问题标题】:How to fix 'CustomUser' object has no attribute 'get'如何修复“CustomUser”对象没有属性“get”
【发布时间】:2019-07-10 17:15:57
【问题描述】:

我正在尝试从数据库中获取一些数据以显示在视图上,但我向我显示了一个错误,我的代码甚至不存在。

我有模型 CustomCliente:

class CustomCliente(AbstractUser):
        email     = models.EmailField(unique=True, null=False, blank=False)
        full_name = models.CharField(max_length=120, null=False, blank=False)
        password  = models.CharField(max_length=40, null=False, blank=False)
        username  = models.CharField(max_length=110, unique=True, null=False, blank=False)

我有以下方法的看法:

def mostrar_relatorio(request, id):
    aluguel = Aluguel.objects.get(cliente=CustomCliente.objects.get(id=id))
    if aluguel is not None :
        context = {'aluguel': aluguel}
        template_name = 'relatorio.html'
    else:
        raise Http404

    return render(request, template_name, context)

我在那个模型中的 urls 模式是:

urlpatterns = [
    path('cliente/<int:id>', CustomCliente, name='relatorio'),
]

当我尝试访问以下 url 127.0.0.1:8000/cliente/2 时,会发生错误: 'CustomCliente' 对象没有属性 'get.即使我的代码中没有任何地方调用 CustomCliente.get。 我已经尝试关闭服务器并再次尝试,重写代码,但它似乎不起作用。

【问题讨论】:

    标签: django django-models django-views django-users


    【解决方案1】:

    你在 urls.py 中使用了你的模型而不是你的视图。

    应该是:

    path('cliente/<int:id>', mostrar_relatorio, name='relatorio'),
    

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      相关资源
      最近更新 更多