I use django-tenant-schemas and I need to change the way it selects the current tenant:
from https:// tenant1.mydomain.com/
to https:// mydomain.com/tenant1/
To do that I've coded a middleware, it's quite simple and it's working, the tenant is selected. The problem comes in the url resolution in django. It raises a 404 cause there is no url mapping that includes that suffix "tenant1".
How could I tell django that the urls now have a dinamic suffix that changes deppending on the tenant? This should be used reversing and generating urls.
Related
When I try to render a html file in django project, I get this error, and I can't see the localhost page 'cause of this.
The error is:
NoReverseMatch at / Reverse for 'it_languages' not found. 'it_languages' is not a valid view function or pattern name.
and it languages is url for in html
Then it bolds me with yellow this:
Home
About Me
**IT-languages**
Projects
Contact
I expect to see my offline page rendered by django project
Should I keep it like the original html version:
Home
About Me
IT-languages
Projects
Contact
Please start by providing your urls.py, views.py, and the it_languages template.
As #cbirch said, it's impossible to exactly provide an answer without more information (preferably the code they listed).
In general, the error 'NoReverseMatch' happens when you are trying to refer to an endpoint by the name, but the name doesn't map to a provided endpoint.
When you create a route you add an entry to the urlpatterns list in urls.py (typically) - if you don't give them a specific name, they will be given an automatically generated name. Then in your template file, if you refer to one of these names, they have to match!
Probably you use { url it_languages } or similar in one of your templates, but you haven't set that as a name for a url.
If you install django-extensions, there's a tool (show_urls) that will allow you to list your endpoints and their names (I believe) - that may help you trace the issue down. Good luck!
Is it acceptable to use two includes for the same base url routing schema?
e.g. - I have allauth installed which uses r'^accounts/', include('allauth.urls')
and I want to extend this further with my own app, which extends the allauth urls even further.
An example of this would be accounts/profile or some other extension of the base accounts/ url.
Is it fine to do the following?
r'^accounts/', include('myapp.urls')
In additon to:
r'^accounts/', include('allauth.urls')
As far as I can tell both will just be included with the base url routing schema and it will just look for the allauth urls first?
Yes, that is perfectly fine.
Django will look for a matching url in the first one, and if it doesn't find it, it will move on to the next one.
I need to redirect my clients to another endpoint in my django app. I know I can use relative urls with request.build_absolute_uri() to do this, but I am searching for a generic solution that doesn't require the redirecting handler to know its own place in the URL hierarchy.
As an example, I have handlers at the following two URLs:
https://example.com/some/other/namespace/MY_APP/endpoint_one
https://example.com/some/other/namespace/MY_APP/foo/bar/endpoint_two
Both handlers need to redirect to this URL:
https://example.com/some/other/namespace/MY_APP/baz/destination_endpoint
I would like for endpoint_one and endpoint_two to both be able to use the exact same logic to redirect to destination_endpoint.
My app has no knowledge of the /some/other/namespaces/ part of the URL, and that part of the URL can change depending on the deployment (or might not be there at all in a development environment).
I know I could use different relative urls from each endpoint, and redirect to the destination URL. However, that required that the handlers for endpoint_one and endpoint_two know their relative position in the URL hierarchy, which is something I am trying to avoid.
After doing more research and talking with coworkers, I realized that reverse does exactly what I need.
I have this url in my urls.py:
url(r'^showrooms/', include('apps.showrooms.urls')),
and i want to have another url like this(i.e w/o 's' of showrooms)
url(r'^showroom/', include('apps.showrooms.urls')),
but having two urls like above results in two urls mapping to same data which is not good from seo perspective.So the solution is to redirect showroom/ urls to showrooms/ urls, but how do i get to this as showrooms/ is pointing to set of urls in apps folder ?
If you want to keep it close to django you can use the redirect shortcut as found in the documentation here: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
Otherwise this is more like a webserver question, for apache there is mod_rewrite in which you can 301 redirect, and nginx has the rewrite directive which should suit your needs just fine!
I have a django app that has / at the end of every URL conf. Example:
# user home page
(r'^home/$', 'user_home_page'),
However, I'm noticing this is causing a ton of redirects on my server, because when people dont add the /, it redirects them. Is there any way to have it accept both without a redirect except doing:
# user home page
(r'^home$', 'user_home_page'),
(r'^home/$', 'user_home_page'),
or should I avoid URL confs like that?
While you can accept both without doing a redirect by using:
(r'^home/?$', 'user_home_page'),
It is not best SEO practice because it will look like you have duplicate content and your hits will be split between the two pages.
You could modify your APPEND_SLASH Django setting, I haven't used it before.
You may be wondering what happens if someone requests the URL /hello
(that is, without a trailing slash). Because our URLpattern requires a
trailing slash, that URL would not match. However, by default, any
request to a URL that doesn’t match a URLpattern and doesn’t end with
a slash will be redirected to the same URL with a trailing slash.
(This is regulated by the APPEND_SLASH Django setting, which is
covered in Appendix D.)
This was taken from http://djangobook.com/en/2.0/chapter03/
The patterns are Python regular expressions. How about:
(r'^home(/?)$', 'user_home_page'),
should I avoid URL confs like that?
Yes. Django already handles this for you.
See this: https://docs.djangoproject.com/en/1.3/ref/middleware/#module-django.middleware.common
If APPEND_SLASH is True and the initial URL doesn’t end with a slash,
and it is not found in the URLconf, then a new URL is formed by
appending a slash at the end. If this new URL is found in the URLconf,
then Django redirects the request to this new URL. Otherwise, the
initial URL is processed as usual.
Use Django REMOVE_SLASH middleware instead.
https://gist.github.com/2204099
https://gist.github.com/gists/2204099/download
You need to ensure your application uses / consistently, make a choice which to use, and 301 redirect the rogue variation to the correct canonical one.
There should only be one version of a page, and the correct solution is in most cases to 301 redirect to the canonical version.
In some situations with additional paramenters for tracking you should also define a canonical in the head.
By using consistent URLs within your APP and out on the web in links pointing back to your app, you then won't have lots of redirects.