首页 > 解决方案 > 如何获得 QuerySet 的字段

问题描述

当我需要从 post.title 文本创建 qr cod 时,我在管理面板中添加帖子

我尝试使用 django-qr-code 库和 ContactDetail 模型,但我得到错误:** 之后的类型对象参数必须是映射,而不是 Post

views.py def 索引(请求):

# Use a ContactDetail instance to encapsulate the detail of the contact.
posts = Post.objects.all()
contact_detail = ContactDetail(
    first_name=Post.objects.get())    

# Use a WifiConfig instance to encapsulate the configuration of the connexion.

# Build context for rendering QR codes.
context = dict(
    posts=posts,
    contact_detail=contact_detail,

)

# Render the index page.
return render(request, 'blog/post_list.html', context=context)

模型.py

class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=400)

html

{% qr_for_contact contact_detail=contact_detail size='S' %}

标签: django-modelsdjango-viewsdjango-queryset

解决方案


推荐阅读