nephila / django-meta

Pluggable app to allow Django developers to quickly add meta tags and OpenGraph, Twitter, and Google Plus properties to their HTML responses.
https://django-meta.readthedocs.io
Other
484 stars 68 forks source link

'ImageFieldFile' object has no attribute 'startswith' #124

Closed kamuri-chan-alt closed 3 years ago

kamuri-chan-alt commented 4 years ago

I'm trying to add the tags to a website, but it's showing this error and I don't know what to do.

My models.py

class News(ModelMeta, models.Model):
    title = models.CharField(max_length=200)
    content= RichTextUploadingField()
    date = models.DateTimeField('published_date')
    image = models.ImageField(upload_to="images", blank=False)
    category = models.ForeignKey(
        Category, default=1, verbose_name="Categories",
        on_delete=models.SET_DEFAULT)
    post_slug = AutoSlugField(populate_from='title')

    _metadata = {
        'title': 'title',
        'image': 'image',
    }

    def get_meta_image(self):
        if self.image:
            return self.image.url

    def __str__(self):
        return self.title

I'm using Python 3.8 and Django 3.1.2

Traceback:

File "/home/kamurichanalt/.virtualenvs/tvsite-virtualenv/lib/python3.8/site-packages/meta/views.py", line 125, in image if not image.startswith('http') and not image.startswith('/'):

Exception Type: AttributeError at /cookie-day Exception Value: 'ImageFieldFile' object has no attribute 'startswith'

Thanks.

yakky commented 3 years ago

@kamuri-chan-alt you must rewrite _metadata as

    _metadata = {
        'title': 'title',
        'image': 'get_meta_image',
    }

see example here: https://django-meta.readthedocs.io/en/latest/models.html?highlight=get_meta_image#usage

with your code, the value if self.image (which is an object) is used, but a string is expected

kamuri-chan-alt commented 3 years ago

Thank you, but now it's raising another error:

Request Method: GET

http://127.0.0.1:8000/jojofag 3.1.2 ImproperlyConfigured META_SITE_PROTOCOL is not set

I know it's related to settings.SITE_PROTOCOL, but adding it to settings.py in mysite don't seems to work. Sorry, I don't really have much experience with Django.

yakky commented 3 years ago

you should set META_SITE_PROTOCOL in project settings.py file

kamuri-chan-alt commented 3 years ago

Thank you for all the help, I really appreciate it.

Now there's only (at least I hope) one last thing: only the meta description is showing on the page.

result:

<head  prefix="og: http://ogp.me/ns#">
  <title>bump of chicken</title>
<meta name="description" content="News">
</head>

html:

<html>
{% load meta %}
<head {% meta_namespaces %}>
  <title>{{post.title}}</title>
  {% include "meta/meta.html" %}
</head>

views.py:

def single_slug(request, single_slug):
    news = [p.post_slug for p in News.objects.all()]
    if single_slug in news:
        this_post = News.objects.get(post_slug=single_slug)
        meta = this_post.as_meta()

        return render(request=request,
                      template_name='main/posts.html',
                      context={'post': this_post, 'meta': meta
                               })

models.py:

class News(ModelMeta, models.Model):
    title = models.CharField(max_length=200)
    content = RichTextUploadingField()
    datetime = models.DateTimeField('published date')
    image = models.ImageField(upload_to="images", blank=False)
    post_slug = AutoSlugField(populate_from='title')
    category = "Notícia"

    _metadata = {
        'title': 'title',
        'description': 'category',
        'image': 'get_meta_image',
        'url': 'post_slug',
    }

    def __str__(self):
        return self.título

    def get_meta_image(self):
        if self.image:
            return self.image.url
yakky commented 3 years ago

@kamuri-chan-alt have you set the META_USE_<FOO>_PROPERTIES = True in the project settings?

kamuri-chan-alt commented 3 years ago

@yakky i've solved it two days after the comment but couldn't access this account in the meantime. All working fine, thank you for all the help.