为什么可以在我的URL中看到描述文本?

发布于 2025-02-05 00:11:11 字数 1201 浏览 0 评论 0 原文

在家庭类别中,我有 ELF 作为描述

图像

我在视图中尝试了此代码,以便能够在模板中显示 ELF

def home(request ):
    p=get_object_or_404(category,pk=1)
    return render(request,'home.html',{'p':p})

在模板中,我使用了此代码< p id =“ id”> { {p.description}}</p> 在此路径 description elf elf >路径('',views.home),

模型:

class category(models.Model):

    name=models.CharField(max_length=255, db_index=True)

    def __str__(self):
        return self.name

class product(models.Model):
     
    category = models.ForeignKey(category, related_name='products',on_delete=models.CASCADE) 
    image=models.CharField(max_length=500)
    description=models.CharField(max_length=500)
    price=models.CharField(max_length=50)
    buy=models.CharField(max_length=100)
 

urls:

urlpatterns = [
    path('api/', include(router.urls)),
    path('',views.home),
    path('admin/',admin.site.urls),
     
]

为什么我在我的URL中看不到 Descript> Description 的文本?

In home category i have elf as a description :

image

I tried this code in my views to be able to display elf in my templates:

def home(request ):
    p=get_object_or_404(category,pk=1)
    return render(request,'home.html',{'p':p})

and in templates i used this code <p id="id">{{p.description}}</p> to display it but it is not working i can not see the description or elf in this path path('',views.home),

models:

class category(models.Model):

    name=models.CharField(max_length=255, db_index=True)

    def __str__(self):
        return self.name

class product(models.Model):
     
    category = models.ForeignKey(category, related_name='products',on_delete=models.CASCADE) 
    image=models.CharField(max_length=500)
    description=models.CharField(max_length=500)
    price=models.CharField(max_length=50)
    buy=models.CharField(max_length=100)
 

urls:

urlpatterns = [
    path('api/', include(router.urls)),
    path('',views.home),
    path('admin/',admin.site.urls),
     
]

Why can't i see the text of description in my url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2025-02-12 00:11:11

您正在尝试获取类别模型的描述。试试看:

视图:

def home(request ):
    p=get_object_or_404(product,pk=1)
    return render(request,'home.html',{'p':p})

You are trying to get the description of the category model. try this instead:

views:

def home(request ):
    p=get_object_or_404(product,pk=1)
    return render(request,'home.html',{'p':p})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文