Django 3 TypeError:上下文必须是dict而不是set

2024-09-28 03:14:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,正在用django 3.0.2和django 1.10教程建立一个网站,但我遇到了错误,我已经搜索了可能的解决方案,但没有找到

这是终端的输出

Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\LENOVO\Desktop\TGPB\blog\views.py", line 12, in post_list
    return render(request, 'blog/post_list.html', {'posts: posts'})
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\template\backends\django.py", line 59, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "C:\Users\LENOVO\Desktop\TGPB\env\lib\site-packages\django\template\context.py", line 270, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than set.

这是我的看法

from django.shortcuts import render
from .models import Author, Tag, Category, Post


def index(request):
    return HttpResponse("Hello Django")

#View function to display list of posts
def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts: posts'})

和我的帖子列表模板

{% extends "blog/base.html"  %}

{% block title %}
    Blogg - {{ block.super }}
{% endblock %}

{% block content %}    

    <div class="content">
        <div class="section-inner clearfix">        

        {% for post in posts %}
            <h3>                        
                <a href="http://127.0.0.1:8000/{{ post.pk }}/">{{ post.title|capfirst }}</a>
            </h3>
            <p class="post-info">
                <span>Date: {{ post.pub_date }} </span> |
                <span>Category: <a href="http://127.0.0.1:8000/category/{{ post.category.slug }}">{{ post.category.name }}</a></span> |
                <span>Tag:
                {% for tag in post.tags.all %}
                      <a href="http://127.0.0.1:8000/tag/{{ tag.slug }}">{{ tag.name }}</a>
                {% empty %}
                      None
                {% endfor %}
                </span> 
            </p>
        {% empty %}
            <p>There are no posts</p>
        {% endfor %}    

        </div>
    </div>

{% endblock %}

Tags: djangoinpyenvrequestcontextlinerender
2条回答

在您的函数中,post_list视图.py文件中{'posts: posts'}应该是{'posts':posts}posts应该是一个变量,而不是字符串更改它就会正常工作

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

在你看来,你有一个bug
posts应该是变量,而不是字符串

from django.shortcuts import render
from .models import Author, Tag, Category, Post


def index(request):
    return HttpResponse("Hello Django")

#View function to display list of posts
def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

相关问题 更多 >

    热门问题