是否可以解析get对象?

问题描述 投票:0回答:1
  1. 第一次尝试Django,我的产品视图现在通过模板获取数据。有时候,pycharm警告无效缩进,但我用不必要的空间解决了它。
  2. 这是'base.html'文件:
<!doctype html>
<html>
<head>
    <title>Programmierung ist schön.</title>
</head>

<body>
    <!-- <h1>This is navbar</h1> -->
    {% include 'navbar.html' %}

    {% block content %}
        replace me
    {% endblock %}

    {% block another_content %}
        replace me
    {% endblock another_content %}
</body>
</html>
  1. 这是product \ detail.html文件:
{% extends 'base.html' %}

{% block content %}

    <h1>{{ item }}</h1>
    <p>{ % if description  } {{ description }} { % else %  } Description Coming Soon { % endif % }</p>

{% endblock %}
  1. Safari浏览器出错:
AttributeError at /product
'tuple' object has no attribute 'get'
Request Method: GET
Request URL:    http://127.0.0.1:8000/product
Django Version: 2.0.7
Exception Type: AttributeError
Exception Value:    
'tuple' object has no attribute 'get'
Exception Location: /Users/kuldeep/Dev/trydjango/lib/python3.7/site-packages/django/middleware/clickjacking.py in process_response, line 26
Python Executable:  /Users/kuldeep/Dev/trydjango/bin/python
Python Version: 3.7.0
Python Path:    
['/Users/kuldeep/Dev/trydjango/src',
 '/Users/kuldeep/Dev/trydjango/lib/python37.zip',
 '/Users/kuldeep/Dev/trydjango/lib/python3.7',
 '/Users/kuldeep/Dev/trydjango/lib/python3.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/Users/kuldeep/Dev/trydjango/lib/python3.7/site-packages']
Server time:    Mon, 29 Apr 2019 09:53:16 +0000

这是view.html文件。

from django.shortcuts import render

from .models import Product
# Create your views here.

def product_detail_view(request):
    obj = Product.objects.get(id=1)
    context = {
        'title': obj.title,
        'description': obj.description,
        'summary': obj.summary,
    }
    return render(request, "product/detail.html", context),
django
1个回答
0
投票

你有一个简单的拼写错误:在你的最后一行末尾有一个额外的逗号。

return render(request, "product/detail.html", context),

应该

return render(request, "product/detail.html", context)
© www.soinside.com 2019 - 2024. All rights reserved.