When I submit a form in django (or access admin) the address changes in URL bar to an ip address - django

I have the following set up.
On my dns:
an A name record that points my domain to an IP addres
On my server:
nginx -> gunicorn -> django
But when I submit a form (e.g. log in form) the address in the URL bar changes from my domain name to my IP address. This causes a Cross Domain Error.
This happens on forms and when I access mydomain/admin
when I access pages my domain name in the URL bar remains intact. When I go to mydomain/admin it automatically switches to the IP w/o submitting any other forms. Any other time I have to submit a form for it to change to the IP address.
All my media and everything use mydomain in the path. It is the strangest thing. I only first noticed it when I went to my admin page, but didnt think much of it. But I just added a few forms and now I can't submit the forms because the forms try to submit to the IP address causing the cross domain error.
I should add teh forms are auto generated by django-registration.

Look for your IP in your settings.py file. It could be a setting like MEDIA_URL set to use your IP and not your domain.

This has to mean that your forms are configured to submit to the IP address. Look at the source of your form and look for the form tag:
<form action="http://(ip here?)/form/" method="GET">
Make sure that IP address isn't inserted there. You could use a relative URL. I normally leave it blank since most of my forms are processed by the same view that generated the form to begin with.

I'm assuming that you were developing locally at some point and this did not occur so I'd suggest it's something in your Nginx config. Can you use Firebug/Web Inspector to see if there's any redirects happening from your domain to your IP?
I'd also check to make sure that your Site's URL is correctly set up in the admin. Sometimes this can cause incorrect urls although I don't think the cases you've mentioned apply.

Related

Django Invalid HTTP_HOST header: '<ip>'. You may need to add '<ip>' to ALLOWED_HOSTS

<ip> is showing an actual ip address, I just didn't include it in the title.
I believe this ip address is the internal ip of my EC2 instance. I'm using AWS Elastic beanstalk to host.
I see this question has been answered a lot on SO, and the answer is always to add the ip address to the ALLOWED_HOSTS, but in my case I've set ALLOWED_HOSTS=['*'] and I'm still getting the error.
The weird thing is, I'm only getting the error when I try to access the site from my phone. When I access from the desktop browser, it works fine...
Things I've tried:
I've double checked my elastic beanstalk deployment and the changes are definitely deployed.
Ok so this probably won't be the answer for other people but it was for me..
In my case, I was doing an http GET request from my frontend and forgot the extra "/" at the end of the url. My django urls.py defines the url with a slash at the end. My fix was to add the extra "/" when doing the http GET.
On my desktop, this was automatically handled because django would reply with an automatic redirect (302) and my desktop browser would go to the url with / at the end, but my phone was not doing the redirect!
This somehow was throwing the invalid HTTP_HOST header error.
For most people, the fix for an error message like this is to add all of your domains to the list of ALLOWED_HOSTS.
Oh and if you're using elasticbeanstalk like me, don't forget to add the AWS domain name. It should look something like this:
ALLOWED_HOSTS = ['<your-unique-id>.elasticbeanstalk.com',
'example.com', '<your-subdomain>.example.com']
DON'T do ALLOWED_HOSTS = ['*'] in prod!!

Wagtail internal link urls arent working properly

in my rich body text fields, when an internal page is used for a link, the url that gets attached to the a tag is "https//example.com/example", ie it's missing the colon and the link doesnt work. I get the error "https's server ip address could not be found".
any idea why it is doing this? thanks
Check the site record under Sites -> Settings. The hostname field should NOT contain https - i.e. it should be example.com, not https://example.com.
in case anyone else has this issue, it seems having more than one site in wagtail caused this. I had the default localhost site still on there, even after switching to a new production site configuration. deleting the localhost site fixed this.

can't access facebook using fandjango

I have fandjango setup on my django app and I have the "Site URL" pointing to the exact spot where the dynamic page is shown.
http://www.example.com/apps/myapp
This gives me the following error message box.
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
I'm not sure how to debug this. Looking at similar people's posts, they said they had this problem only if there was a mismatch between the site url, but even if I copy and paste the site url into my browser, I still get the same error. I don't have https setup, if that matters.

URL fails django validation, cannot ping, definitely exists

I am trying to add this URL to a URLField in a model with URL validation: http://www.scscourt.org. Obviously the URL exists, but for whatever reason django won't validate it -- and I can't ping it either, but that could be a separate issue. Ideas?
Edit: Additionally, I have logged into the server and tried nslookup from there -- it finds the DNS info, and a wget returns the index page -- so my server can see it.
Behind the scenes DJango sends a HEAD request to http://www.scscourt.org/. When you do this ( I did it with fiddler) they are actually denying you and sending back a 403.
You could technically set verify_exists=False for that particular link.

Name URL is re-written as IP by something? nginx, apache, django?

I am using django, nginx and apache. When I access my site with a URL (e.g., http://www.foo.com/) what appears in my browser address is the IP address with admin appended (e.g., http://123.45.67.890/admin/). When I access the site by IP, it is redirected as expected by django's urls.py (e.g., http://123.45.67.890/ -> http://123.45.67.890/accounts/login/?next=/)
I would like to have the name URL act the same way as the IP. Where should I be looking to make this happen?
Do you have virtual domains?
If so, your domain name and your ip address may be served differently.