vendredi 14 novembre 2014

Django get_absolute_url de page cms - Stack Overflow


I want to access to the url from a cms page


from cms.models import Page

page = Page.objects.published()[3] #clinics page
print page.get_absolute_url()

But it shows this error:


ImproperlyConfigured at /en/clinics/ 
The included urlconf urls doesn't have any patterns in it

Clinics page works (admin + frontend), the problem is when I try to call get_absolute_url()


part of urls.py:


urlpatterns += i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)

clinics/urls.py:


from django.conf.urls import patterns, url
from clinics import views

urlpatterns = patterns('',
url(r'^$', views.clinic_list, name='clinic_list'),
)



page = Page.objects.published()[3]

This won't work, as published is a BooleanField from the model Page.


What you want to do is:


page = page.objects.get(pk=3) # At least if this pk is 3, check with your installation.
page.get_absolute_url()



I found another way to do it and it works. Calling the reverse of the clinic_list route directly. I think the way of passing through the cms page is not necessary.


from django.core.urlresolvers import reverse

print reverse('clinic_list')


I want to access to the url from a cms page


from cms.models import Page

page = Page.objects.published()[3] #clinics page
print page.get_absolute_url()

But it shows this error:


ImproperlyConfigured at /en/clinics/ 
The included urlconf urls doesn't have any patterns in it

Clinics page works (admin + frontend), the problem is when I try to call get_absolute_url()


part of urls.py:


urlpatterns += i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)

clinics/urls.py:


from django.conf.urls import patterns, url
from clinics import views

urlpatterns = patterns('',
url(r'^$', views.clinic_list, name='clinic_list'),
)


page = Page.objects.published()[3]

This won't work, as published is a BooleanField from the model Page.


What you want to do is:


page = page.objects.get(pk=3) # At least if this pk is 3, check with your installation.
page.get_absolute_url()


I found another way to do it and it works. Calling the reverse of the clinic_list route directly. I think the way of passing through the cms page is not necessary.


from django.core.urlresolvers import reverse

print reverse('clinic_list')

0 commentaires:

Enregistrer un commentaire