Serving django app in an intended location (NGINX) - python

I have a web server serving my Django app (using NGINX) and I need to access it in a defined "location".
For example, I access my Django app XPTO in "ip:port/" but I need to access it like "ip:port/XPTO/". All urls specified in Django have to be resolved "after" this "base url".
Anyway I can do this without messing with my "urls.py" in Django? I had tried some configurations on NGINX but nothing worked.
Thanks in advance!

You need to use the location directive in your nginx config.
You probably have something right now that looks like this:
location / {
...
}
To serve under XPTO instead you want this:
location /XPTO/ {
...
}
You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.
If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.

Related

Redirect to a folder in Django

Django currently has a complete system for routing urls.
But I have a very specific situation where I am using django but actually need to use urls like in classic PHP language.
For example:
The url - localhost/reader/theeffort should take me to a folder called theeffort where I have my files index.html, 1.html, 2.html, 3.html
and so on!
Now all these files should be accessible by localhost/reader/theeffort/*.html and not by Django's default url system. Is this possible to achieve that? If yes, how?
This isn't a thing you would do with Django's URLs. If you just want to serve HTML files within a folder, they are static files; they should therefore be served by the web server itself, eg Apache. You just need to configure an alias in the Apache conf to point to the folder where the static files are.

Inject constant (e.g., API-keys) when serving javascript files with Flask

I have a web application using Flask and Jinja2. I would like to inject different API-keys depending on if I'm serving the site in production or testing.
I could inject them using the Jinja templating engine and use inline <script>-tags to then access them from my other JS. But preferably I would like to have some kind of really simple template-string in my .js-files, like this:
_ready: Keen.ready(function() {
var client = new Keen({
projectId: $KEEN_PROJECT_ID$,
writeKey: $KEEN_WRITE_KEY$
});
and then replace those keywords when Flask serve the file. Of course changing the value of the constant depending on which environment the server is running.
Are there any good ways of doing this?
A good way to do this is by adding a global to jinja. See this answer
You can then inject the api key wherever you need and keep your template code clean by having the lookup logic somewhere else

Django redirection without changing the browser's URL

I've got a Django app running on localhost:80, and another app running on localhost:41984. Now, in the app, I'm trying to hook things up so that hitting localhost/view/41984 redirects to localhost:41984, without changing the URL in the browser: effectively, URL masking of sorts.
Could someone give me some pointers on achieving this? Django's HttpResponseRedirect does the redirection, but the URL changes too, which isn't what I want. I read somewhere that people do this with .htaccess, but I'm not planning on using Apache.
Thanks!
The best option should be, to run both app in the same server. Just add one to the INSTALLED_APPS list.
Then, you can do:
return redirect('some-view-name', foo='bar')
The view name is the one you define in the urls.py file:
url(r'^enter/$', 'yourApp.views.viewName', name='some-view-name'),
For more info https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
If you need to run booth app in differents servers... I would say that you need a REAL web server, apache, ngix... what you want, but no ./manage.py runserver

google app engine application architecture in python

I am pretty new to google app engine and python. After successfully uploading an application, I am stuck with a basic question.
app = webapp2.WSGIApplication([
('/.*',IndexHandler)
], debug=True)
This is the code block which is generally used to map the request with the class that handles it. However there is a section in app.yaml which allows specifying handler for individual url.
My question is what is the correct architecture of a python application on google app engine. What if my application has several hundreds of classes for handling different request ?, do I have to specify all of them here in this code ?
I have googled but could not find a satisfactory answer. Link to a good tutorial or documentation would be a great help.
Basically, you define the app to be used in app.yaml. For example, if you've got multiple apps, you can specify here which to use.
Yes, you have to specify all the allowed URLs here (in main.py). Otherwise the request will get 404. However, you can use regular expressions to make certain type of addresses to match the given handler.
Check out the tutorial: https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp
Documentation for URI routing: http://webapp-improved.appspot.com/guide/routing.html?highlight=url
You can do like this.
In app.yaml
- url: /service/.*
script: service.app
- url: .*
script: main.app
In service.py
url_map = [
('.*/user.*', 'service.UserHandler'),
('.*/data/show/', 'appname.service.DataShowHandler'),
('.*/data.*', 'appname.service.DataHandler'),
]
app = webapp2.WSGIApplication(url_map)
When you tried to access http://your-appid.com/service/user, appengine will excecute GET function of UserHandler Class in service.py which is located in the Root Folder.
When you tried to access http://your-appid.com/service/data/show, appengine will excecute GET function of DataShowHandler Class in service.py which is located in the Root/appname Folder.
I have two anwers :
1) You can use webapp2 routing to handle the requests and uri routing. This is very powerfull. You can use url templates and / or write your own custom dispatcher.
2) For a lot of requests you can use a single URL and use a session / state to find out how to continue after a post. This means : you do not have to use a request handler for every request.
app.yaml can be used for setting such as secure and login options, though I personally don't use them.
I use main.py to map all urls to the right handlers.

Allowing users to use custom domains for Django app on Heroku

I have a Django app hosting on Heroku. In the app, the users create pages at http://domain.com/username
I'd like to give users the option to use their own domain name for their page using a CNAME. Ideally I'd like to avoid an A-Record in case I change hosts in the future and my IP changes.
This is completely new territory for me and dont even know where to start, or what to look for. Does anyone have a suggestion on where to start? I've seen mention of Wildcard DNS, but not sure how that ties into my app.
Any suggestions would be really appreciated.
Prelim Answer:
If you control the nameserver for the domain and have access to the RNDC Key, you can use the post-signup view/signal to squirt out a cname to your DNS server that will resove username.yoursite.com to yoursite.com. Make sure apache is set up to recieve a wildcard virtualhost to the correct app, and then use a custom middleware to read request.META['SERVER_NAME'].lsplit('.')[0] to see what the subdomain is. You can then use this information in your views to differentiate user subdomains.

Categories