I want to deploy a django app into an existing webenvironment, with other sites already existing.
Therefore i received a domain from the webadmin like this: www.websites.com/theapp
Now, in my local development environment the path /theapp does not exist, because the project is starting on / and the project folder is called /myapp
Myapp's urls.py is pointing to two included apps like /app1 and /app2
I tried to add /theapp to the urls, what really doesn't work.
As i am fairly new to Django i would appreciate any assistance on how to handle this leading path, so Django could handle the incoming url correctly.
First, you should configure Web Server (like nginx, apache, etc...). Below is the Nginx configuration:
location /theapp {
... something uwsgi configuration or proxy_pass things ...
}
After that, set up Django's urls.py file to accept requests starting with the path /theapp.
from django.urls import path, include
urlpatterns = [
path('theapp/', include('yourapp.urls')),
]
Related
I'm trying to get my Django app to play audio files (supposedly) uploaded by users via a form. Said files are tied to a model :
# models.py
class Doc(models.Model):
# ...
tape = models.FileField()
The uploading and saving parts are working fine, and the files are stored where they should be :
- djangoproject
|
- docapp
|
- media <- here
So, in order to get where I want, I added these two lines to the settings.py file MEDIA_ROOT = os.path.join(BASE_DIR, 'docapp/media/') and MEDIA_URL = 'docapp/media/'.
I hoped to be able to link to the audio files thus:
# templates/docapp/index.html
...
<audio src='{{ doc.tape.url }}' controls></audio>
Unfortunately, this wasn't working because the link generated by doc.tap.url (http://localhost/docapp/media/filename.aac) was returning a 404 error.
After a lot of googling I found this answer, which I happily copy-pasted into my app ... and it worked. This is the code in question :
# urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The problem is that I'm not comfortable with inserting code in my apps that I don't understand. I did some research about the static() function and all I could get is this :
Helper function to return a URL pattern for serving files in debug mode
Does this mean that the static function should not be used in production? If so, what should I be using in production? And what exactly does it do?
EDIT To be clear, the generated URL after injecting the solution is the same as the one generated without it. Yet, it only works when the static() function is present.
EDIT 2 I forgot to mention that the 404 errors persisted even after I chmoded the media folder to allow others to read-access it.
Thanks in advance!
You shouldn't do anything. No errors no problem. The docs write about development server and serving static files AND that it is for development only. In a production environment you configure your server (Apache, NGNIX or third party like S3) to serve the files. That's all.
Try to configure media files and access the file via it's url. If it works, try the {{ doc.tape.url }} template tag.
In your development environment your media may live in /media/ (route and directory). While on production it may be something like media.example.com. Running Django with the settings for that environment will change all static/media domains and paths to their correct locations.
You may split settings file into a settings file for each environment (production, acceptance, development). Like this:
project/
settings/
__init__.py
base.py
local.py
staging.py
test.py
production.py
You can run your project with a specific env: ./manage.py runserver --settings=project.settings.development. Do not repeat yourself and put development specific settings in development.py and from base import * so that base.py contains the default settings.
This project and settings layout is taken from the book Two Scoops of Django. It is just an example. Adjust to your own needs.
Yes, django.conf.urls.static.static is only for development and not for production. For production, you should just need to configure your MEDIA_URL and MEDIA_ROOT settings and have your webserver serve the MEDIA_ROOT directory in the MEDIA_URL path.
Basically adding that in the URL urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) will make the media files in the URL existing. Try removing that and visit your media file in the URL and you will receive a 404 not found. It's very similar to the concept that you are inserting a view for rendering the media files
I followed the tutorial below to create a django project on azure:
http://www.windowsazure.com/en-us/develop/python/tutorials/web-sites-with-django/
Everything worked fine until I tried to install the django_admin_bootstrapped app. Now all static requests return 404 error. I don't if the new app caused the problem or just exposed it.
I have this:
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
#os.path.join(PROJECT_DIR, 'site-packages/django_admin_bootstrapped/static'),
)
I read quite a bit online and some people say you have to configure your server to serve static files. I don't have direct access to the server, it's an azure website and I deploy through git. And like I said, the admin and everything used to work before I tried to install these apps that bootstrap the admin. Now even when the app is not under INSTALLED_APPS I don't get any css or js files.
Thanks!
I had this problem and none of the suggested answers seemed to fit. My bizarre solution was to switch off Python on the Azure web site configure page.
I arrived at this odd conclusion by installing the PTVS Django sample and following the steps in this tutorial http://azure.microsoft.com/en-us/documentation/articles/web-sites-python-ptvs-django-sql/. The only difference I could fine between my site and the working tutorial was that Django was off! If someone has an explanation I would love to hear it (PHP is enabled!!!).
I found my solution on this page: http://mrtn.me/blog/2012/06/27/running-django-under-windows-with-iis-using-fcgi/
I had to create a central static folder and add a web.config for iis to serve the files. web.config below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- this configuration overrides the FastCGI handler to let IIS serve the static files -->
<handlers>
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
Hope this helps someone!
I don't have enough rep to vote up but the answer does in fact work on Azure for me as well, and appears to be, so far, the only method of doing it. Just remember that the web.config needs to be in the right place...lol...I apparently had multiple static folders as I was trying different ways of solving this.
If after trying all the other proposed solutions you still find yourself at trouble, you may have to understand that depending on the server that's running you application is the way static files are server. Django has it's own server, which is run by using the command:
python manage.py runserver
But PAAS providers do not use this server in most of the cases. gunicorn is in most times the chosen one. Azure sometimes uses IIS's FastCGI handler but at current date it is intelligent enough to detect a django application and try to use django's default server.
So, the first step you have to take is to find out what server is azure using to run your app, and you may know that by reading azure's log:
https:// YOUR APP NAME.scm.azurewebsites.net/api/logstream
If you confirm that azure is using django's default server, you must bear in mind that django does not server static files automatically when in a production environment. You must configure the static folder's url. So in your config/urls.py set the static url, like this:
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
urlpatterns = i18n_patterns(path('admin/', admin.site.urls), prefix_default_language=False) + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
As you can see, I enable admin, static and media urls. This is enough to let django's default server know where to find and serve static files (and media files too, as well as admin routes)
Yes, I can confirm that the 'right place' to store your static files for a Django deployment on Azure Web Sites is the 'static' directory in the web root.
Step 1: create a directory called static in your web root (www root)
Step 2: edit Django's settings.py and set STATIC_ROOT = 'D:/home/site/wwwroot/static'
Step 3: In your templates use the construct {% static "static_file_name" %}.
I am having some trouble getting django and lighttpd setup.
Here ist what I want to do:
Have lighttpd manage the fascgi processes (they both are run on the same server)
Have the django app run at the root of the site (so no django.fcgi)
Alternativly I could start django on startup, but I would need a start/stop script for ubuntu/debian.
Also how do I handle the media / admin media urls? Also I would like to serve /css /js /img from the document root ...
TIA
Patrick
Please see:
https://github.com/cdsi/elrond/blob/master/etc/lighttpd.conf
https://github.com/cdsi/elrond/blob/master/var/www/elrond.wsgi
Be sure to update the env.ELROND_XXX paths accordingly, and change:
fastcgi.server = ("/elrond" =>
to:
fastcgi.server = ("/" =>
I'm getting started with a Django tutorial, and I've run into a snag. Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. The tutorial points me to http://127.0.0.1:8000. However, that's not going to work, as I'm doing this remotely.
[background information]
What I have done, apparently successfully, is
django-admin.py startproject mysite
(created mysite directory containing four files)
python manage.py runserver
(Validating models... 0 errors found, etc.)
The absolute path is
/home/toewsweb/public_html/pythonlab/mysite
What URL should I be able to use to bring this up in my browser?
I also put mysite at
/home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory)
What URL should I be able to use in this case?
This is a virtual private server, so I have access to httpd.conf. I have downloaded and installed mod_wsgi and have added it to the Apache configuration. I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; however, when I point the browser to that subdomain, I just get the directory listing.
[/background information]
Right now, I just want to know how to view what I'm working on in my browser.
Thanks!
For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). Just start the dev server so that it listens to an external port:
./manage.py runserver 0:8000
This binds to the external IP address, so now you can access your Django site via port 8000 on that server:
http://whatever.my.ip.is:8000
You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. Django's docs have a great explanation on what you need to do.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your httpd.conf file and add:
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. This tells Apache to serve any request below the given URL using the WSGI application defined by that file.
Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If your project is not on your PYTHONPATH by default you can add:
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
just below the import sys line to place your project on the path. Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location.
OR
The other option is to run the dev server so it's accesible externally like so:
python manage.py runserver 0.0.0.0:80
though please DO NOT use this in production. The dev server is single-threaded, and has not been auditing for security.
I am generating tar.gz files with Django and save it to somewhere like /home/foo/foo.tar.gz but I don't know what is a good way to serve these generated files under django view.
I am using return HttpResponseRedirect("/home/foo/foo.tar.gz") but it is actually not a good way to serve tar.gz files because the generated tar.gz file path start from / (root dir) of my linux server instead of a relative path.
Thanks.
Just send it in the response.
If you're not looking to protect with with authentication via Django, you can serve it up with your http server (nginx, lighttpd, apache, etc) - this reduces server impact.
You could define the redirect path relative to MEDIA_ROOT or another setting in settings - and as James says you should definitely consider configuring your http server to handle those files if you haven't already.
# settings.py
TARBALL_ROOT = '/home/foo/tarballs/'
# views.py
import os
from django.conf import settings
def your_view(request):
# do some stuff
filepath = os.path.join(settings.TARBALL_ROOT, 'relative/path/from/media/root'
return HttpResponseRedirect(filepath)