Use hostname in Django Templates - django

I am developing a django app and want to use relative a href url paths, as I develop on localhost and will be working on www.example.com. What is the best way to make a relative link like this in my template.html files?

That's ok. You can get relative url using the following in Django template(but for making REST API calls you should use absolute url).
{{ request.path }}
eg. In home page,
Contact
Suppose my server is running on localhost and listening on port 8000 and I visit home page, then the above will be interpreted by django as follows,
Contact
In case of making REST API calls use the following in your template with HOST, PORT and PATH(eg. In home to get all posts),
All posts
And it will be rendered as follows,
All posts
I have one little example as send_contact_data.js. Visit and see line 8.

Related

Make django handle subdomain suffix

We're hosting several dockerized web-apps on our webserver, let's call it group.example.com. Our subdomains are handled via nginx as suffixes though, which translates to something like group.example.com/app1/ group.example.com/app2/ as root urls.
When using Django, we run into problems though, as all its urls generated by url in the templates such as home will be relative links, so rendered to home. This relative link will not be interpreted correctly, leading to the main, non-app page group.example.com.
So the goal is to have an app based prefix such as /app1/ for all links. I can hardcode these for static links, but there has to be a more elegant way. Also this leads to problem for the used forms submitting to the wrong page - redirecting again back to the main, non-app page group.example.com.
I tried adding /app1/ to all registered urls as prefix, but that doesn't seem to work either - that way the app is running but user would need to visit group.example.com/app1/app1/ to get to the index, and the relative links still don't work correctly.
In the app docker-container we're running the web-app with nginx and uwsgi. It works fine when using correct subdomains such as app1.example2.com - but we don't have that capability on our new faster webserver we want to host the app on.
Is there a way to resolve this using the app containers nginx, uwsgi or django / middleware config to get the links to resolve to group.example.com/app1/ as root?
As far as I know, there is two ways to resolve it.
One use SCRIPT_NAME in the NGINX configuration. For example, based on this server fault answer:
location /app1/ {
SCRIPT_NAME /app1;
# rest of the config
}
Two You can add FORCE_SCRIPT_NAME in your settings.py:
FORCE_SCRIPT_NAME = '/app1'
FYI, I would prefer using first solution.

Issue with Django URLs on apache production server

I have a Django site and I have set up a test server for it. Now, the test server is being used for something else (php) also and I have configured it as aliases in virtual host config for Apache.
Now the problem is this:
When I open it as: http://x.x.x.x/djangoSite/
the WSGI triggers Django and site loads perfectly. But still all the anchor tags point to http://x.x.x.x/viewFunction/ and not http://x.x.x.x/djangoSite/viewFunction/ and hence I get a 404 every time...
Is there a way to tell Django that the base URL for the site is not the domain (public IP) but instead domain/djangoSite/ from where the WSGI alias starts...
Regards
You don't show the code, but you are probably hard-coding the URLs in your templates. Don't do this: you should always use the {% url %} tag to calculate them dynamically. When you do this, Django will automatically apply the base path (passed as the SCRIPT_NAME parameter in the WSGI environment) and all will be well.

Shopify Django app failing in shopify_app/views.py in finalize

I created a Shopify app hosted on Heroku. I had to modify the name of the shopify app from shopify_app to shopifyapp for Heroku to recognize it as a Django app.
If I visit my app directly though app-name.herokuapp/login and connect the app to my store, It correctly pulls my recent orders and products.
If I visit the app through the app menu and it redirects to app-name.herokuapp/login/finalize it shows
KeyError at /login/finalize/
I haven't modified anything in shopify_app except changed it's name to shopifyapp everywhere.
I suspect since the app cant finalize, that is why the links like this also don't work:
https://{{ current_shop.domain }}/admin/orders/{{ order.id }}">{{ order.name }}
They just direct to something like
https://admin/orders/000000000
Can anyone help troubleshoot this problem with the shopify app?
I've made my app repo public since it's still essentially just the demo app:
https://github.com/dpetrillo740/scm
App is running at http://scmapp.herokuapp.com/
This was a bug in the demo App. I just fixed it with this commit 27d5091.
Update the path in your application url to /login from /login/finalize. The redirect_uri is now provided for authentication with shopify, so it will still redirect back to the finalize endpoint.

Django admin "view on site" gets a wrong redirection

I have just copied a working Django project from a development server to a production server. I have not touched the code in any way. In the admin interface there is a blog app, in each blog post there is a get_absolute_url so the admin creates the "view on site" button. On the development server when I click this button it opens the post on the site as expected: an example link would be /admin/r/25/515/ which when loaded then redirects to /blog_app/posts/515/slug. That's perfect. On the production server though the "view on site" link has the exact same value of /admin/r/25/515/ but when loaded returns a strange url that leads to nowhere: admin/r/25/515/.com/blog_app/posts/515/slug. I have no idea where this ".com" comes from, I started looking everywhere but have no clue. Any idea?
That was an Nginx conf problem, I use it as a proxy / load balancer and in the proxy_pass setting I did not enter the full domain name thinking it was a kind of variable. My bad. Not really Django related but since many people use it that way it can be useful to know.

Apache | Django: How to run websites on the back of a base URL?

I've got a base url. http://baseurl.com/
I'm trying to run projects on the back of it. For example
http://baseurl.com/mongoose/
The projects run but the URL don't work properly because they all
reference the base url. So for 'About Me' page it points to
http://baseurl.com/about instead of http://baseurl.com/mongoose/about
Is this something i need to change in django or apache? Is what I'm
trying to do even possible?
Coming from an IIS .net background I know that in IIS you can "Create and application" within a site which essentially does what I'm trying to achieve now with Apache and Django.
Thanks
You shouldn't need to do anything. Apache is supposed to be setting a request header called SCRIPT_NAME, which is your base URL, and all URL reversing takes that into account.
How are you creating these URLs in your templates?
Update
So your problem is with getting the URLs of Flatpages. The issue is that the normal way of calculating URLs dynamically, so that they do take SCRIPT_NAME into account - using the reverse() function or the {% url %} tag - doesn't work with Flatpages, because they are not dispatched via urls.py but via a custom middleware which fires on a 404.
So instead of using that middleware, I would use the urls.py mechanism to dispatch to flatpages. Remove the flatpagemiddleware from your settings.py, and in urls.py at the end of your patterns add this:
url(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage', name='flatpage'),
Now, in your templates, you can do:
<a href="{% url flatpage page.url %}">
and it should work correctly.
Check any urls.py in the project(s) to see if they expect to be top-level. But if the application outputs links like /something then it's going to mean the root directory. The application should be reversing a view/parameter into a URL, which would allow you to move it around. If you wrote the apps, check out reverse in django.core.urlresolvers