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.
Related
I want to get url parameters as list in django. Say for example, I will add each parameters to url as;
mydomain.com/param1/param2/param3/.../paramx
Where each param may be existed or not. For example a link may be;
mydomain.com/param1/param3/param4/...
So my question is, How can I get list of params in Django?
I tried handling parameters manually but since they are seperated it doesn't work as expected.
looking at the docs you should be able to use path https://docs.djangoproject.com/en/4.0/topics/http/urls/#path-converters
you then most likely have the path/string param1/param2... as a variable on your view
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.
I use a regex for URL dispatching like described in the Django docs.
r'^remote/(?P<slug>[^/]+)/call_rfc/(?P<rfc_name>.+)$'
Unfortunately rfc_name can start with a slash!
Example:
https://example.com/remote/my_slug/call_rfc//MLK/FOO
The rfc_name is /MLK/FOO.
But this fails. Somewhere (I don't know yet if it is in the browser or in Django) the duplicate slash gets removed.
What is the best practice to handle URL parameters which can start with a slash?
It almost seems that you can consider the latest "slug" as a path. If that's the case, in your URL definition you can use path to represent that. You can have a look here to check if it helps.
path('remote/<slug:slug>/call_rfc/<path:rfc_name>', yourviewhere)
Or, you can perhaps write your custom path converter.
I have an entry in my urls.py that acts as a catch-all which loads a simple view if it finds an appropriate page in the database. The problem with this approach is that the URL solver will then never fail, meaning that the APPEND_SLASH functionality won't kick in - which I need.
I'd rather not have to resort to adding a prefix to the static page URLs to stop it being a catch-all. I do know about flatpages, which uses a 404 hook rather than an entry in urls.py, and I had kinda hoped to avoid having to use it, but I guess this problem might be exactly the kind of reason why one would use it.
Any way round this problem or should I just give in and use flatpages?
Make sure that your catch-all URL pattern has a slash at the end, and that the pattern is the last in your URLconf. If the catch-all pattern doesn't end with a slash, then it will match stray URLs before the middleware tries appending a slash.
For example, use r'^.*/$' instead of r'^.*' as your last pattern.
To do the same, but pass the url to the view as a named argument, use r'^(?P<url>.*)/$'.
The statement if it finds an appropriate static page in the database seems like your static pages are not quite static so, you either pass your links through urls.py (just like you do now), or you extract those pages from the DB, put them in a directory and configure that directory as one for serving static files
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.