I'm having issues getting the Django setup to generate a sitemap for me.
I have added the following to my settings file
'django.contrib.sites',
'django.contrib.sitemaps',
and in my urls file I have the following:
from django.conf.urls import include, url
from django.contrib import admin
from ames import views
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
When deploying these amends I get the following error on the site:
TypeError at /sitemap.xml/
view must be a callable or a list/tuple in the case of include().
Any thoughts would be most welcome.
Amended urls.py file:
from django.conf.urls import include, url
from django.contrib import admin
from cms.sitemaps import CMSSitemap
from django.contrib.sitemaps.views import sitemap
from ames import views
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
try it:
from django.contrib.sitemaps.views import sitemap
# you code
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
and remove
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
all information for the solution is in error view must be a callable or a list/tuple in the case of include()
Related
I got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]
'Copied this code from the Django tutorial into my app's urls.py file...
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
When I start my server it produces the following error...
(urls.W005) URL namespace 'polls' isn't unique. You may not be able
to reverse all URLs in this namespace
I've tried using some other name besides 'polls' but with the same result. What am I doing wrong?
Check your root urls file, and be sure to have unique name:
(django 2+)
For example:
mysite/urls.py
-->
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), #polls.urls is unique
path('admin/', admin.site.urls), #admin.site.urls is unique
]
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9a-f-]+)/$',views.detail,name='detail'),
url(r'^(?P<question_id>[0-9a-f-]+)/results/$',views.results,name='results'),
url(r'^(?P<question_id>[0-9a-f-]+)/vote/$',views.vote,name='vote'),
]
Write your url file like shown above.
I have a django rest project which is built on Django1.7. I need to run it on Django 1.11. When i run
python manage.py migrate
The error is:
ImportError: No module named urls
on url.py line
url(r'^docs/', include('rest_framework_swagger.urls')),
I have already made modifications in url.py file to avoid patterns. The url.py file look like
from django.conf.urls import include,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('gcm.urls')),
url(r'^', include('apps.account.urls')),
url(r'^', include('apps.vegetables.urls')),
url(r'^', include('apps.orders.urls')),
url(r'^', include('apps.listings.urls')),
url(r'^', include('apps.rating.urls')),
url(r'^', include('apps.faq.urls')),
url(r'^thank-you/$', TemplateView.as_view(template_name="thankyou.html"), name="thankyou"),
url(r'^/error/$', TemplateView.as_view(template_name="error.html"), name="error"),
url(r'^$', TemplateView.as_view(template_name="index.html"), name="home"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
'',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT})]
How could i run it?
In urls.py add this at the bottom,
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
From Django 1.9 it does not support to add the URLs as a string, but it need to be imported as callable. So remove this from your urls.py.
urlpatterns += [ '', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT})]
Hi I'm looking to setup some 301 redirects for some urls that have been changed on a site for a client.
I was thinking the following might work:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^case-study/', include('casestudies.urls')),
url(r'^contact-us/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^', include('cms.urls')),
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^old-link/$', RedirectView.as_view(url='new-link/'), name='new-link'),
)
This seems to make sense to me but this doesn't seem to work. Now i am extremely new to Django so apologies if this is a really simple query.