'Post'对象在django Photo Blog App中没有属性'get_content_type'。

问题描述 投票:1回答:1

我真的很迷茫,并试图解决这个问题几个小时。我正在做一个摄影师的社交媒体,他们可以互相评论对方的照片。我为评论设置了表单,但我得到了AttributeError。'Post'对象没有属性'get_content_type'。这是我的Views.py - photo_detail,哪里出错了。

def photo_detail(request, id):
    instance = get_object_or_404(Post, id=id)
    comments = Socialmedia.objects.filter_by_instance(instance)
    initial_data = {
        "content_type": instance.get_content_type,
        "object_id": instance.id
    }
    socialmedia_form = SocialmediaForm(
        request.POST or None, initial=initial_data)
    if socialmedia_form.is_valid():
        print(socialmedia_form.cleaned_data)
    context = {
        "title": instance.title,
        "photo": instance.photo,
        "instance": instance,
        "comments": comments,
        "socialmedia_form": socialmedia_form,
    }
    return render(request, "photo_detail.html", context)

当我注释出一行 "content_type": instance.get_content_type,, 该错误disaapers, 但在写评论后,它说字段是必需的,而不是张贴任何东西 , 所以它不加载数据从评论. 这里是我的models.py :

from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from socialmedia.models import Socialmedia, SocialmediaManager
from django.contrib.contenttypes.models import ContentType


def upload_location(instance, filename):
    return '%s/%s' % (instance.id, filename)


class Post(models.Model):
    # category = models.ForeignKey(Type, blank=True, null=True, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
    title = models.CharField(max_length=200)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    time = models.DateTimeField(auto_now=False, auto_now_add=True)
    photo = models.ImageField(upload_to=upload_location,
                              null=True, blank=True,
                              height_field="height_field",
                              width_field="width_field")
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("photo_detail", kwargs={"id": self.id})


    @property
    def get_content_type(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type

和最后一页photo_detail.html,我试图实现它。

{% extends "base.html" %}


{% block content%}

    <div class="col-sm-8 col-sm-offset-6">
        {% if instance.photo %}
        <img src="{{ instance.photo.url }}"  class="img-responsive"/>
        {% endif %}
    <h1>{{ title }} </h1>
    <p>Author: {{ instance.user}}</p>
    <p><div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-width="" data-layout="button" data-action="like" data-size="small" data-share="true"></div></p>
    <br>
    <p>Updated : {{ instance.updated }}<br/></p>
    <p> Created :{{ instance.time }}<br/></p>

    <div>
    {{instance.comments.all}}
    <p class="lead">Comments</p>
    <form method="POST" action="."> {% csrf_token %}
    {{ socialmedia_form}}
    <input type="submit" value="Post" class="btn btn-default">
    </form>
    {% for comment in comments %}
    <div class="">
    {{ comment.content }}
    {{comment.user}} {{comment.time | timesince }} ago
    </div>
    {% endfor %}
    </div>

    </div>

{% endblock %}
django comments blogs attributeerror
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.