简体   繁体   English

python django:如何修复错误说:AttributeError at / 'Home' object has no attribute 'get'

[英]python django: How to fix error saying: AttributeError at / 'Home' object has no attribute 'get'

i'm trying to create two pages for my website in django.我正在尝试在 Django 中为我的网站创建两个页面。 One called home and other called fruits, but I get an Error: AttributeError at /'Home' object has no attribute 'get' The above 'Home' is a class in my models but I do not know why it says 'Home object no attribute get' when I have called Home.objects.all() in my views.py一个叫做home,另一个叫做fruits,但我得到一个错误:/'Home'对象的AttributeError没有属性'get'上面的'Home'是我模型中的一个类,但我不知道为什么它说'Home object no当我在 views.py 中调用 Home.objects.all() 时,属性 get'

My models:我的模型:

from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=20)
    price = models.FloatField()
    stock = models.IntegerField()
    image_url = models.CharField(max_length=2083)


class Home(models.Model):
    heading = models.CharField(max_length=10)
    words = models.CharField(max_length=10000)

My views:我的观点:

from django.http import HttpResponse
from django.shortcuts render
from .models import Product, Home


def index(request):
    products = Product.objects.all()
    return render(request, 'index.html',
                  {'products': products})


def home(request):
    home_obj = Home.objects.all()
    return render(request, 'home.html',
                  {'Home': home_obj})

My app urls:我的应用网址:

from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    path('fruits/', views.index),
    path('', views.Home),
]

index.html:索引.html:

{% extends 'base.html' %}


{% block content %}
<div class="row">
    {% for product in products %}
    <div class="col">
        <div class="card" style="width: 18rem;">
            <img src="{{ product.image_url }}" class="card-img-top" alt="...">
            <div class="card-body">
                <h5 class="card-title">{{ product.name }}</h5>
                <p class="card-text">₹ {{ product.price }}</p>
                <a href="#" class="btn btn-primary">add to cart</a>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
{% endblock %}

home.html:主页.html:

<h2>{{ home_obj.heading }}</h2>

<p>{{ home_obj.words }}</p>

base.html:基地.html:

 <!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://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
          integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
crossorigin="anonymous">

    <title>LibMax - Your trusted store</title>
</head>
<body>
<!-- As a link -->
<nav class="navbar navbar-light bg-light">
    <a class="navbar-brand" href="#">LibMax</a>
</nav>

<div class="container">
    <h2>Fruits</h2>
    {% block content %}
    {% endblock %}
</div>
<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
        crossorigin="anonymous"></script>

 <!-- Option 2: jQuery, Popper.js, and Bootstrap JS
 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" 
integrity="sha384- 
9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" 
 integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" 
 crossorigin="anonymous"></script>
 -->
 </body>
 </html>

The base.html can be found athttps://getbootstrap.com/docs/4.5/getting-started/introduction/ base.html 可以在https://getbootstrap.com/docs/4.5/getting-started/introduction/找到

under the starter templates section.在入门模板部分下。

traceback:追溯:

Environment:环境:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.1.2
Python Version: 3.8.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'products.apps.ProductsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Ethan\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Ethan\anaconda3\lib\site-packages\django\utils\deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "C:\Users\Ethan\anaconda3\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:

Exception Type: AttributeError at /
Exception Value: 'Home' object has no attribute 'get'

You have a typo.你有一个错字。 In urls.py , you are setting path('', views.Home) .urls.py ,您正在设置path('', views.Home) views.Home is the model which is imported inside views.py , not the function. views.Home是在views.py导入的模型,而不是函数。 Your function has a lowercase h .您的函数有一个小写的h

urls.py

from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    path('fruits/', views.index),
    path('', views.home), # Not Home
]

暂无
暂无

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

相关问题 如何修复“列表”object 在 Python/Django 中没有属性“get”(AttributeError) - How to fix 'list' object has no attribute 'get' (AttributeError) in Python/Django 如何修复&#39;AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;get&#39;错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python 无法修复 Python 错误 AttributeError: 'int' object 没有属性 'get' - Cant fix Python error AttributeError: 'int' object has no attribute 'get' 如何修复 Django 中的“/api/doc &#39;AutoSchema&#39; 对象没有属性 &#39;get_link&#39; 属性错误”错误 - How to fix " AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link' " error in Django “如何解决&#39;AttributeError:&#39;NoneType&#39;对象在Python中没有属性&#39;tbody&#39;错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error Django 3.2 如何修复 AttributeError: 'itertools.chain' object has no attribute 'filter' 错误? - Django 3.2 how to fix AttributeError: 'itertools.chain' object has no attribute 'filter' error? 错误消息说“AttributeError:'NoneType' object 没有属性'text'” - Error message saying "AttributeError: 'NoneType' object has no attribute 'text'" 如何修复python中的“AttributeError:type object has no attribute”? - How to fix "AttributeError: type object has no attribute" in python? 修复 Python 中的“AttributeError: type object has no attribute” - Fix “AttributeError: type object has no attribute” in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM