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
480 stars 68 forks source link

You're using the Django "sites framework" without having set the SITE_ID setting. #155

Closed afdy closed 1 year ago

afdy commented 1 year ago

Description

Using Django-sites, and classed based views and attempting to render a meta object using get_context_data(), I hit a roadblock which fails to render any meta headers and results in an error.

When setting...
    with META_USE_SITES = True
        - fails with:
            You're using the Django "sites framework" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting or pass a request to Site.objects.get_current() to fix this error.

    without META_USE_SITES = False
        - it fails with:
            META_SITE_DOMAIN is not set?

Steps to reproduce

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['meta'] = Meta(
            request=self.request,
            title="A page title",
            image=static('image.png'),
        )
        return ctx

Versions

django 4.1.6 python 3.11.3

Expected behaviour

I would expect the page to render fine.

Actual behaviour

The page results in an error:

  "You're using the Django "sites framework" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting or pass a request to Site.objects.get_current() to fix this error."

Additional information

If I remove the image line, it works fine.

I believe I've managed to locate the problem. The request object is not loaded until the final step of the init function, but it is used before it when rendering the image url.

If I override the meta class and move the line:

        self.request = kwargs.get("request", None)

to the top, this works.

As a workaround I've extended the init function and moved the request object to the top, which works fine.

yakky commented 1 year ago

Hi @afdy thanks for reporting this. I have only been able to reproduce this with SITE_ID setting unset. As moving the request attribute set at the start of the __init__ method, I opened #156 which should fix your issue. Could you test if it works for you? thanks