I got this error "ValueError: attempted relative import beyond top-level package "

402 views
Skip to first unread message

hans alexander

unread,
Sep 8, 2020, 4:37:39 AM9/8/20
to django...@googlegroups.com
my structure is like this:

my_project
       blog
              ....
              sitemaps.py
              ....
       my_project
              ....
              urls.py
              ....

Inside sitemaps.py :
from django.contrib.sitemaps import Sitemap
from .models import *

class BlogPostsSitemap(Sitemap):
changefreq = 'daily'
priority = 0.9
def items(self):
return PostModel.published.all()

def lastmod(self,obj):
return obj.updated

my case:
1. In settings.py I want to import BlogPostsSitemap.
2. I tried like this
image.png

but then I got an error "ValueError: attempted relative import beyond top-level package" when I ran the server.

Is anyone here got the solution? Or maybe some references how to create sitemap on django? 

Thanks.

hans alexander

unread,
Sep 8, 2020, 4:39:05 AM9/8/20
to django...@googlegroups.com
Sorry, point no 1 needs to be revised..
1. In urls.py, I want to import BlogPostsSitemap


coolguy

unread,
Sep 8, 2020, 1:01:18 PM9/8/20
to Django users
Not sure why you are importing this into your main project urls.py but its very straight forward

from blog.sitemaps import BlogPostsSitemap

question... are you trying to map the application url here? then you should use path('xxxx/' , Include("blog.urls"))

hans alexander

unread,
Sep 8, 2020, 2:53:12 PM9/8/20
to django...@googlegroups.com
I followed a tutorial for creating django sitemap, but still got problems along the way. 
I've seen some videos, but didn't work.  
Do you have any references for easy way creating django sitemap for blog posts?
Sure it would help.

Thanks



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0f74b537-3fa8-4725-8098-b602417ea41bn%40googlegroups.com.

coolguy

unread,
Sep 8, 2020, 5:19:16 PM9/8/20
to Django users
I did this long time ago and here is what i did...

in settings.py
SITE_ID = 1
# Application definition
INSTALLED_APPS = [
           # ...
           'django.contrib.sites',
           'django.contrib.sitemaps',
]

run migration i.e. 
>>> py manage.py migrate    OR     python manage.py migrate
after this step, sites application will be in sync with the database.

create a file sitemaps.py in your blog application. Your sitemap.py seems okay to me.

update your main project urls.py file
....
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import BlogPostsSitemap

sitemaps = {
        'posts' : BlogPostsSitemap,
}

urlpatterns = [
          ....
          path('sitemap.xml', sitemap, {'sitemaps': sitemaps},  name='django.contrib.sitemaps.views.sitemap'),
]

Now run the development server and open (assuming you are using default configuration) http://127.0.0.1:8000/sitemap

lets try...

hans alexander

unread,
Sep 9, 2020, 12:33:29 AM9/9/20
to django...@googlegroups.com
Thanks guys.. Already solved it. 
I guess the main reason of the problem is that I'm using Pycharm Community Edition.
So the alert error not accurate.

Anyway I tried to py manage.py with some alerts, and worked.
for example:
image.png
I left it like that. And sitemap still works.

But now the new problem is when I submitted my sitemap.xml to google search console, I got this error.
image.png
I don't know why.

Even when I access the domain/sitemap.xml, the sitemap is showing.

Any suggestions?

Thanks

dum dum

unread,
Sep 12, 2020, 5:56:02 PM9/12/20
to django...@googlegroups.com
Another problem now..

I don't know why,
in local
localhost:8000/sitemap.xml will generate sitemap.xml.

But on heroku, 
domain/sitemap.xml will error h13.

Any suggestions?
Thanks.
Reply all
Reply to author
Forward
0 new messages