Django-Rest-Framework: router not working - django-rest-framework

I have this base urls file:
url(r'^locations/', include('locations.urls')),
in locations.urls.py app i have following url references:
url(r'^/?$', LocationList.as_view()),
url(r'^(?P<pk>[a-zA-Z0-9\-\$]+)/?$', LocationDetail.as_view()),
url(r'services/?$', LocationServiceseList.as_view()),
url(r'services/(?P<service_id>[a-zA-Z0-9\-\$]+)/?$', LocationServicesDetail.as_view()),
For above url referce i want to user routers of Django-Rest-framework
for locations/services/ i created GenericViewSet from DRF and i tried router successfully made following changes in locations.urls:
router = routers.SimpleRouter()
router.register(r'services', LocationServiceSet)
url(r'^/?$', LocationList.as_view()),
url(r'^(?P<vehicle_id>[a-zA-Z0-9\-\$]+)/?$', LocationDetail.as_view()),
url(r'^', include(router.urls)),
Now i want to create router for /locations/ endpoinsts and made following changes
router = routers.SimpleRouter()
router.register(r'services', LocationServiceSet)
router.register(r'', LocationSet)
url(r'^', include(router.urls)),
Getting 404 for /locations/ with stacktrace shows although /locations/services/ work fine:
^locations/ ^ ^services/$ [name='locationsservice-list']
^locations/ ^ ^services/(?P<pk>[^/.]+)/$ [name='locationsservice-detail']
^locations/ ^ ^/$ [name='locations-list']
^locations/ ^ ^/(?P<pk>[^/.]+)/$ [name='locations-detail']

This is happening because of an empty string prefix argument to router.register() function for LocationSet.
When you used an empty string '' as prefix for registering the router, it generated the following urls. (Notice the double slash in urls)
locations//$ # double slash in urls
locations//(?P<pk>[^/.]+)/$ # double slash in urls
To solve this, you need to define and register this router in base urls file instead of locations/urls.py with prefix value as locations.
# base urls.py
router = routers.SimpleRouter()
router.register(r'locations', LocationSet)
...
url(r'^locations/', include('locations.urls')), # include app urls
url(r'^', include(router.urls)), # include router urls
Another solution is to use a non-empty string as prefix while registering the router for LocationSet in locations/urls.py.

Related

How to serve index.html on non root path using golang

I want to serve my angular app index.html under localhost:3000/mypath/ is there a way to accomplish that?
package main
import (
"net/http"
)
func main() {
// This works
http.Handle("/", http.FileServer(http.Dir("./my-project/dist/")))
// This doesn't work, you get 404 page not found
http.Handle("/mypath/", http.FileServer(http.Dir("./my-project/dist/")))
http.ListenAndServe(":3000", nil)
}
Remove the / handler, and change the /mypath/ handler into code below:
http.Handle("/mypath/", http.StripPrefix("/mypath/", http.FileServer(http.Dir("./my-project/dist/"))))
The http.StripPrefix() function is used to remove the prefix of requested path. On your current /mypath handler, every request will be prefixed with /mypath/. Take a look at example below.
/mypath/index.html
/mypath/some/folder/style.css
...
If the requested url path is not stripped, then (as per above example) it'll point into below respective locations, which is INVALID path and will result file not found error.
./my-project/dist/mypath/index.html
./my-project/dist/mypath/some/folder/style.css
...
By stripping the /mypath, it'll point into below locations, the correct one.
./my-project/dist/index.html
./my-project/dist/some/folder/style.css
...

Lighttpd rewrite-if-not-file cannot process *php and *html in one rule

To enable a shorten-url service, I had to add a rewrite rule to my
lighttpd.conf. This seems to be important, otherwise shortlinks like
http://example.com/Xf6Y would return a 404 error. This is because Xf6Y
is not a file but a shortlink hash.
So I added these lines to my lighttpd.conf:
url.rewrite-if-not-file = (
"^/(.*)$" => "/index.php?fwd=$1"
)
This works excellently, except for websites that contain a index.html
instead of an index.php.
I tried to duplicate the rewrite rule, like url.rewrite-if-not-file = (
"^/(.)(\?.)?$" => "/index.php?fwd=$1",
"^/(.)(\?.)?$" => "/index.html?fwd=$1"
)
This resulted in a "duplicate rule" error, so lighttpd is
not able to restart.
Does anyone know how to write a rewrite rule that will redirect index.php and index.html?

Redirect example.com/?p=25 to example.com/25 in CodeIgniter

I'm using CodeIgniter and I want to redirect links like this:
example.com/?p=25
to this:
example.com/25
How can this be achieved?
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
or
to create url like this : http://yyyy.com/article/finishing-dan-snapshop-salkulator
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
1. article : class name
2. readmore : method from class article
3. $1 : get value from uri segment 2 value
its .htaccess rewrite rule.make sure u have activated mod_rewrite .then put this line into website application root .htaccess file
RewriteRule ^/([0-9]+)/?$ p=$1 [NC,L] # Handle product
requests

Escape forward slash in Ruby url helper

Setuping a staticMatic project using /index.html:
#slug = current_page.gsub(/\.html/, '')
returns "/index(.html)", but should be /index
Changing term corrects: - #slug = current_page.gsub("/", "").gsub(".html", "") as found in:
https://github.com/adamstac/staticmatic-bootstrap/blob/master/src/helpers/application_helper.rb
To delete the beginning "/" after you've stripped the html simply execute this (which will do both in one command):
current_page.gsub(/\.html/, '').gsub(/\//,''))

Lighttpd url rewrite removes querystring variables

I am rewriting a URL in Lighttpd using
url.rewrite-once = (
"^/(.*)\.(.+)$" => "$0",
"^/(.+/?)\??$" => "/index.php?q=$1"
)
So that all urls are passed to index.php as variable q. However when I visit http://mydomain.com/account/edit?user=5 my script at index.php gets
q=account/edit?user=5
on apache I would get all variables i.e.
q=account/edit AND
user=5
How can I preserve the variables in Lighttpd?
(the first part of the url.rewrite rule is to ensure that files that exist are displayed properly)
Try something like this:
"^/something/(\d+)(?:\?(.*))?" => "/index.php?bla=$1&$2"
or this
"^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/([^.?]*)$" => "/index.php?q=$1"

Resources