Which view handles a url defined from content block?

59 views
Skip to first unread message

Aaron Kim

unread,
Jan 10, 2017, 9:33:48 PM1/10/17
to django-oscar
Please excuse my newbie question. I have created a page content block and defined the url to be 'about/disclamer'. Now, I've modified a footer template to link it. 

<a href="www.xxx.com/about/disclaimer">Disclaimer</a>

Here, I don't want to hard code the url. So, I've added urlpattern like following. 

url(r'^about/disclaimer', name='disclaimer'),

and used it from template

<a href="{% url 'disclaimer' %}">Disclaimer</a>

the problem is that view name is mandatory for url(). So, I get this error. 

TypeError: url() missing 1 required positional argument: 'view'

Which view name should I use above?

Bastien Roques

unread,
Jan 11, 2017, 9:01:49 AM1/11/17
to django-oscar
Here what I've done to link flat pages in templates. I'm not sure this is the solution but at least it works.

in app.py from root dir, add these lines

from django.contrib.flatpages  import views

(...)

def get_urls(self):
    urls = [
    ...
    url(r'^(?P<url>.*/)$', views.flatpage, name='flatpage'),
]


Then you'll be able from your template:

<a href="{% url 'flatpage'  url='disclaimer' %}">Disclaimer</a>



Bastien Roques

unread,
Jan 11, 2017, 10:46:23 AM1/11/17
to django-oscar
To me, the best way to add this url is to add it to urlpattern in you config/urls.py 


from django.contrib.flatpages import views
(...)

urlpatterns = [
(...)

# flatpages
    url(r'^(?P<url>.*/)$', views.flatpage, name='flatpage'),


It avoids overriding **oscar/app.py** . 

Aaron Kim

unread,
Jan 12, 2017, 12:21:40 AM1/12/17
to django-oscar
Thank you, Bastien. Using urlpatterns after stopped using middleware worked. 

Bastien Roques

unread,
Jan 12, 2017, 5:09:23 AM1/12/17
to django-oscar
I don't know why you had to stop the flatpage middleware. it works without being deactivated on my project!

Aaron Kim

unread,
Jan 12, 2017, 9:53:06 PM1/12/17
to django-oscar
I get Specified page already exists! whenever I try to create a page after this setup. Have you experienced this issue?

Bastien Roques

unread,
Jan 13, 2017, 4:18:25 AM1/13/17
to django-oscar

Sorry, I'm still working on it. I hadn't seen this issue since I use fixtures to import pages. You're absolutely right, creating new page return this Specified page already exists! error.

Removing the middleware changed nothing to me either.

Bastien Roques

unread,
Jan 13, 2017, 6:26:31 AM1/13/17
to django-oscar
The solution I found consisted in overriding dashboard.pages and then in a validators.py recreate the URLDoesNotExistValidator in order to be specific to the flatpages.

So I've overriden PageUpdateForm() and PageCreateView() in order to import my version of this specific validator.

class URLDoesNotExistValidator(validators.URLValidator):

def __call__(self, value):
try:
FlatPage.objects.get(url=value)
except FlatPage.DoesNotExist:
return
else:
raise ValidationError(
_('Specified page already exists!'), code='invalid')

Aaron Kim

unread,
Jan 16, 2017, 1:16:45 AM1/16/17
to django-oscar
Thanks, Bastien. I have solved this issue a bit differently - by adding urlpattern for flatpages only to be limited to /page/

from django.contrib.flatpages import urls as flatpage_urls  # noqa
urlpatterns += [
url(r'^page', include(flatpage_urls)),
]

This means that you will need to specify 

/page/about/

instead of 

/about/ (let's say you've specified the page url to be /about/ on dashboard)

In this way, however, it doesn't cause oscar's validator to spit out error and therefore, won't have to do further. 

It is good to know that I could override URLDoesNotExistValidator as well. 
Reply all
Reply to author
Forward
0 new messages