简体   繁体   English

'QuerySet' 对象没有属性 'META' 500(内部服务器错误)

[英]'QuerySet' object has no attribute 'META' 500(internal server erro)

I'm trying to practice ajax in django but I got this error 'QuerySet' object has no attribute 'META'.我正在尝试在 django 中练习 ajax,但出现此错误“QuerySet”对象没有属性“META”。 But it's showing the data in the traceback but not in the template because of the error.How to fix this?但是由于错误,它在回溯中显示数据,但不在模板中显示数据。如何解决此问题?

models.py模型.py

from django.db import models

# Create your models here.


class Profile(models.Model):
    name = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    bio = models.CharField(max_length=100)

    def __str__(self):
        return self.name

I think it has something to do with the views but I cant figure it out.我认为这与观点有关,但我无法弄清楚。 views.py视图.py

from django.shortcuts import render
from .models import Profile
from django.http import JsonResponse

# Create your views here.


def list(request):
    return render(request, 'livedata/list.html')


def getProfiles(request):
    profiles = Profile.objects.all()
    # print(JsonResponse({"profiles": list(profiles.values())}))
    return JsonResponse({"profiles": list(profiles.values())})

urls.py网址.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.list, name='list'),
    path('getProfiles', views.getProfiles, name='getProfiles')
]

index.html索引.html

{% load static %}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    {% comment %} <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> {% endcomment %}
    <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
    <script src="{% static 'js/main.js' %}" defer></script>

    <title>Hello, world!</title>
  </head>
  <body>

    {% block contents %}{% endblock contents %}
    {% block scripts %}{% endblock scripts %}

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    {% comment %} <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> {% endcomment %}
    {% comment %} <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> {% endcomment %}
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  </body>
</html>

list.html列表.html

{% extends 'livedata/index.html' %}

{% block contents %}

    <h1>List of live data</h1>
    
    <ul id="display">

    </ul>



{% endblock contents %}

{% block scripts %}

<script>

$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            type: 'GET',
            url: "{% url 'getProfiles' %}",
            success: function (response) {
                // console.log(response);
                $("#display").empty();
                for (var key in response.profiles) {
                    var temp = "<li>" + response.profiles[key].name + "</li>";
                    $("#display").append(temp);
                }
            },
            error: function (response) {
                alert("Error occured");
            }
        });
    }, 1000);
});

</script>

{% endblock scripts %}

You should not name a view list , list is a builtin function that you use in other views.您不应该命名视图listlist是您在其他视图中使用的内置函数。 By defining a view named list , the list(profiles.values()) will call the view with profile.values() as the request.通过定义一个名为list的视图, list(profiles.values())将调用带有profile.values()作为请求的视图。

Rename list to view_list for example and update the url patterns to direct to view_list instead:例如,将list重命名为view_list并更新 url 模式以直接指向view_list

def view_list(request):
    return render(request, 'livedata/list.html')


def getProfiles(request):
    profiles = Profile.objects.all()
    #      does not refer to the view ↓ but to the list builtin
    return JsonResponse({"profiles": list(profiles.values())})

The urls.py thus looks like:因此urls.py看起来像:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.view_list, name='list'),
    path('getProfiles', views.getProfiles, name='getProfiles')
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;QuerySet&#39; 对象没有属性 &#39;_meta&#39; - 'QuerySet' object has no attribute '_meta' Django POST 请求 500 错误:AttributeError: 'QuerySet' object 没有属性 '_meta' - Django POST Request 500 Error: AttributeError: 'QuerySet' object has no attribute '_meta' Heroku 内部服务器错误 500:“NoneType”对象没有“用户名”属性 - Heroku Internal Server Error 500: 'NoneType' object has no attribute 'username' 'QuerySet' object 没有属性 '_meta' Model 查询到 JSON 格式 - 'QuerySet' object has no attribute '_meta' Model Query to JSON format 如何按订单上传付款图片,&#39;QuerySet&#39;对象没有属性&#39;_meta&#39; - How to upload payment picture by orders, 'QuerySet' object has no attribute '_meta' Queryset序列化:AttributeError:&#39;dict&#39;对象没有属性&#39;_meta&#39; - Queryset Serialize: AttributeError: 'dict' object has no attribute '_meta' Django Rest Framework&#39;QuerySet&#39;对象没有属性&#39;_meta&#39; - django rest framework 'QuerySet' object has no attribute '_meta' QuerySet 对象没有属性 - QuerySet object has no attribute 'QuerySet' object 没有属性 'ontbijt' - Django QuerySet - 'QuerySet' object has no attribute 'ontbijt' - Django QuerySet 使用 Flask JWT 时出错,AttributeError: &#39;list&#39; 对象没有属性 &#39;id&#39; 并显示 500 内部服务器错误 - getting error while using Flask JWT, AttributeError: 'list' object has no attribute 'id' and shows 500 Internal server error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM