Add custom url action parameter to django-cms - django

The common url action parameters in django-cms are for example: ?edit to enter Edit-mode, ?toolbar_off to disable/hide the toolbar.
Now I'd like to add a new action parameter e.g. ?logout which just logs out the user no matter on which url he/she currently is. I'd tried to include this in the urls.py with the following pattern:
url(r'^.*\?logout$', RedirectView.as_view(url='/admin/logout/')),
I read in another SO answer that you shouldn't catch URL Params with an url pattern...
Should I do this in a kind of middleware? Or where else?
Using django==1.11, django-cms==3.5.3

This should definily go into a middleware. It would probably work as well as an url pattern, but is kind of not "how you do it" - at least I never saw anything like it in a tutorial or documentation.

Related

adding parameter to wordpress url without query string

i want to have parameter in friendly urls way, have done it with query string but this is not what i want
below is my link
http://www.example.org/category-name/sub-category/post-name-goes-here
and want to have link in template like this
http://www.example.org/category-name/sub-category/post-name-goes-here/parameter
it should still load the same single page but with parameter so i can use condition in single page template
how i can ignore this using rewrite rule or any other method?
You will need to add a rewrite rule so it doesnt try and "solve" the url address using its standard function .
See my previous answers here:
Add "custom page" without page
WordPress Rewrite based on form hidden field
you can pretty much add anything to the url after that.

Change link text in template based on referrer

I'm making a navbar for my blog. If a reader arrives at a post from the post listing, I want the navigation link to say "Back to the post listing" or something like that. If the visitor comes from another source, I'd like the link to say "Read other posts like this one."
I know I can test the referrer with request.META.HTTP_REFERER, but I'm wondering what to compare this with. I could always hard-code a comparison to the post listing URL, but I'd prefer something more durable.
A combination of reverse() and build_absolute_uri() should do the trick:
if request.META['HTTP_REFERER'] == request.build_absolute_uri(reverse('posts')):
...
(This assumes that you have only a single URL mapping to the post list.)
Since you're using Jinja2 you could do this in the template as well.

Is there something between middleware and view in Django so that I can plug my code into?

Is there something between middleware and view so that I can plug my code or do I have to subclass something from Django to provide this functionality?
Let me first explain why I need this, maybe there is a better solution that you can suggest. I want to restrict some of my url's based on some configuration. And,
- I want this configuration to be part of url configuration
- According to the config provided, I want to redirect, etc to some other view.
What I mean by 'part of url configuration' is something like the following.
url(r'^admin/blah/blah$', do_something, name='admin-blah-blah', {'security_level': 'very_secure', 'auth_method' : 'oauth', 'auth_url', 'http://www.foo.com'})
It seems like it is something that should be done by middlewares, but I don't want to do it with middlewares for 2 reasons.
- I don't want to maintain a separate config.
- I don't want to do regex matching for url patterns one more time, url resolver is already doing that
So if I can just find a way to plug some functionality just before view and can reach the configuration provided, it solves my problem.
Sounds like you could do this with a decorator on your views:
#restrict_url(security_level='very_secure', auth_method='oauth',
auth_url= 'http://www.foo.com')
def my_view(request):
... etc ...
You can get some ideas of how to write the restrict_url decorator by looking at the ones provided in django.contrib.auth.decorators.

Django's Satchmo and flatpages issue

I'm having a problem with configuring Flatpages in Satchmo. I've used them before, in a pure django app, but now it just doesn't work, returning 301 http error when I'm trying to enter a flatpage-configured site.
What I've done to configure it:
added the middleware "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" to MIDDLEWARE_CLASSES as last in the list,
configured example pages in admin module.
Simply what the docs say about flatpages config.
I'm feeling helpless. Don't know how could I debug this issue. Any thoughts on that?
And of course help appreciated.
Thanks to suggestion by Peter I've managed to narrow the problem to my urls.py file, for the satchmo shop.
The urlpatterns has only one entry:
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
This version does not work and moreover interfere with flatpages. But disabling flatpages from MIDDLEWARE_CLASSES and adding it to urls.py like the snippet below works:
(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'),
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
However next problem is with the redirection from / to /shop/. With the above config it results in infinite loop.
Perhaps you know the reason for that behavior (redirect overriding flatpage) and maybe You could suggest some working solution to this problem or what should be done with requests to /.
It returns 301? That's Page Moved Permanently (HttpResponsePermanentRedirect) and there are no references to that in the flatpages directory so I don't think it's coming from there. In fact there are only about 5 references to HttpResponsePermanentRedirect in all of the standard 1.1.1 release.
Possible approaches:
Comment out the FlatPages middleware and see if the error changes (I'm betting it won't).
Try changing the order of your MIDDLEWARE classes and see if things change.
When presenting a problem like this it's better to get very specific by showing the exact code from applicable portions of settings.py (or whatever) and by giving other things like precise URLs and the urls.py patterns you are trying to match.
Update:
OK, some random thoughts:
The pattern (r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'), will match anything. Any patterns after it will never be seen.
flatpages doesn't work by being called directly, it does its magic in middleware. It looks for 404 responses (Page Not Found) and then looks to see if that path exists in its table. If so, it calls a view that renders the page, etc. etc. If it doesn't find a match it let's the 404 continue on through middleware processing.
The pattern (r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}), will match anything (I just tested it). If you want to match an empty path, use r('^$', etc.). This is the source of your infinite loop.
If you are new to regular expressions the Django urls.py file can seem like F*cking Magic. I recommend starting very simply and add one rule at a time. Do some quick tests to ensure that the new rule a) matches stuff you want it to match, and b) doesn't match stuff it shouldn't. In particular, make sure that some of the rules that occur later in the file are still reachable. In this case they wouldn't have been which should have raised a red flag.

Recursive URL Patterns CMS Style

Whenever I learn a new language/framework, I always make a content management system...
I'm learning Python & Django and I'm stuck with making a URL pattern that will pick the right page.
For example, for a single-level URL pattern, I have:
url(r'^(?P<segment>[-\w]+)/$', views.page_by_slug, name='pg_slug'),
Which works great for urls like:
http://localhost:8000/page/
Now, I'm not sure if I can get Django's URL system to bring back a list of slugs ala:
http://localhost:8000/parent/child/grandchild/
would return parent, child, grandchild.
So is this something that Django does already? Or do I modify my original URL pattern to allow slashes and extract the URL data there?
Thanks for the help in advance.
That's because your regular expression does not allow middle '/' characters. Recursive definition of url segments pattern may be possible, but anyway it would be passed as a chunk to your view function.
Try this
url(r'^(?P<segments>[-/\w]+)/$', views.page_by_slug, name='pg_slug'),
and split segments argument passed to page_by_slug() by '/', then you will get ['parent', 'child', 'grandchild']. I'm not sure how you've organized the page model, but if it is not much sophiscated, consider using or improving flatpages package that is already included in Django.
Note that if you have other kind of urls that does not indicate user-generated pages but system's own pages, you should put them before the pattern you listed because Django's url matching mechanism follows the given order.