Django url redirect issue - python

I have a view (Let's call it handle_login_redirection) that handles log-in from a url and redirects to an appropriate url based on the request. This has been working fine.
Now when I use a custom domain and add the r'^$' pattern to my urlconf so that the Top Level Domain (TLD - www.mysite.com) is redirected to the same view that handles login (i.e handle_login_redirection), I can see that the urls are not rewritten in the browser even when the page is correctly redirected.
Even other urls on the site, while they are directed fine, do not change to their corresponding url path and instead show the TLD in the browser. Is there anything wrong with the url pattern or is there a better way to handle TLD redirects?
Also note, I'm deployed to Heroku and there is no Nginx,Apache to configure url rewrites.

So I was doing domain forwarding and masking which was causing the issue. The right way to do this is to set CNAME entry source to point from your subdomain (www.mysite.com) to mysite.herokuapp.com (for Cedar stack). Also had to make entries for A records for naked domains i.e mysite.com (Without the www) and point that to mysite.herokuapp.com
And then running heroku domains:add <domain> on both the domain and the sub-domain.
Helped by How to configure heroku application DNS to Godaddy Domain? and
https://devcenter.heroku.com/articles/custom-domains

Related

Connect domain to pythonanywhere

I did a webpage in pythonanywhere and I'm using the subdomain they gave me (username.pythonanywhere.com) but I have a domain that I want to use for this project (I bought the domain in domain.com). I already expanded my plan in pythonanywhere to put my own domain but I've been trying without sucess. This is the documentation that I've been reading.
pythonanywhere documentation
Image of the Domain.com panel
Is the first time that I make this and a pretty confused.

Django - redirecting to home page if no url matched

Basically, my goal is to redirect the user to the view that renders index.html if nothing from URLConf matched the requested url. Then, on the clientside, I would manage routing as appropriate or show 404 page.
So basically, the question is,
how to implement this redirecting in Django? I have tried this:
url(r'^(.*)$', views.index),
...some other urls
...and not surprisingly, (.*) means that ALL urls are redirected to index.html, no matter if there it matches any other URL or not. So should I just place this in the very end of the url list? I have a feeling that I should use some sort of middleware, or maybe Django has a native way to address this issue?
An umpleasant side-effect of this would be for users of my rest app. Each time they all an invalid URL, they would get HTML (that would in effect be the HTML for home.html) instead of json or 404 error. Well, nothing ugly about index.html code, and yet it sounds like a bad idea. So would I separate this logic so that the users of REST app get 404 error instead of being redirected to index.html? This is much about coding patterns, not Django itself. And yet I would really appreciate any comments on this as well :)
You can add the catch all at the end of your URL config, but it leads to some unexpected behaviour (e.g. redirects from /my-page to /my-page/ (appending slash) no longer work).
A better approach would be to write a custom middleware. You could look at the redirect middleware for inspiration since it handles redirects.
If you don't want to redirect API requests, you could add a check to your middleware. For example you could not redirect requests where the URL starts with /api/, or the request has an Accept: application/json header.
Another option would be to use a custom 404 handler that renders your index page. Again, in the handler you have the request object, so you can treat API requests differently.

Getting a URL in python2.7

In a Djano project, I need to get the complete URL of my web app in a view, along with the URL's anchor tag. For instance, if the URL is:
http://example.com/#section0
I need to get that in its entirely, including the #section0 part. As you'd already know, HTTP_REFERER doesn't suffice (which is what I've tried).
What's a good way to read URLs in python/django? Beginner here, please advise.
Sorry, but the anchor tag is never sent to the server. The only way a server could know what it was is if the server itself sent the user to a url with the anchor tag.
See here for more information:
Is the anchor part of a URL being sent to a web server?

get_login_url goes to blank page when coming from naked url

I just started hosting my app on a new domain. I selected a url forwarding option with my registrar. Which seems to work. example.com didn't work before (as opposed to the subdomain www.example.com) but now it does.
The problem is that if I access the site via example.com and try to login, users.create_login_url(self.request.uri), I go to a blank page. If I go to www.example.com the login works fine. In both cases self.request.uri is returning the www subdomain, even though in the first case example.com is still shown in the browser.
I don't really know much about this stuff. Any tips on where I should go from here?
When accessing the site via "example.com", self.request.uri is from that domain, which is the domain where the cookie is created. When it then redirects back, it goes to "www.example.com", which has no cookie, but your site is expecting a user to be logged in.
Suggestions: use a relative path like / (or webapp2's uri routing) instead of a full URI, or first redirect to www.example.com before doing the login.

How do I get the URL of an HTTP redirect's target?

I am writing client-side Python unit tests to verify whether the HTTP 302 redirects on my Google App Engine site are pointing to the right pages. So far, I have been calling urllib2.urlopen(my_url).geturl(). However, I have encountered 2 issues:
the URL returned by geturl() does not appear to include URL query strings like ?k1=v1&k2=v2; how can I see these? (I need to check whether I correctly passed along the visitor's original URL query string to the redirect page.)
geturl() shows the final URL after any additional redirects. I just care about the first redirect (the one from my site); I am agnostic to anything after that. For example, let's assume my site is example.com. If a user requests http://www.example.com/somepath/?q=foo, I might want to redirect them to http://www.anothersite.com?q=foo. That other site might do another redirect to http://subdomain.anothersite.com?q=foo, which I can't control or predict. How can I make sure my redirect is correct?
Supply follow_redirects=False to the fetch function, then retrieve the location of the first redirect from the 'location' header in the response, like so:
response = urlfetch.fetch(your_url, follow_redirects=False)
location = response.headers['Location']
Use httplib (and look at the return status and Location header of the response) to avoid the "auto-follow redirects" that's impeding your testing. There's a good example here.

Categories