I'm trying to redirect the Django site root to the Django admin panel, and I've mostly got it up and running. However, I've noticed that unless there's a trailing slash in the project URL, the links on the page don't include the project folder, and instead try to find the apps in the site root.
So, say I've got a project foo, and an app bar. If you visit http://server/foo/ (with a trailing slash), everything works fine, and the links on the page go to http://server/foo/bar. However, if one visits http://server/foo, the generated links go to http://server/bar instead, which generates a 404 error.
If I set the WSGIScriptAlias to point to /foo/ instead of /foo, it would give a 404 error if I navigated to /foo. I tried to force a trailing slash in the Apache conf with Redirect, but I end up generating a recursive redirect (http://server/foo//////...). I haven't yet tried using a .htaccess file, but I suspect the same thing might happen.
I tried the same thing in urls.py, however:
url(r'^$', redirect_to, {'url': '/'}), # goes to server root http://server/
url(r'^$', redirect_to, {'url': 'foo/'}), # goes to http://server/foo/foo
url(r'^$', redirect_to, {'url': '/foo/'}), # infinite redirect
I also tried simply appending a slash to all the Django urls like so:
url(r'^(.*)/', include(admin.site.urls))
But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).
I'm using Apache 2.2 with mod_wsgi, here is the configuration:
Alias /static "C:/DJANGO~1/apps/django/django/contrib"
<Directory 'C:/DJANGO~1/apps/django/django/contrib'>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias /foo "C:/Djangostack/apps/django/scripts/django.wsgi"
<Directory 'C:/Djangostack/apps/django/scripts'>
Order allow,deny
Allow from all
</Directory>
And this is the urls.py that mostly works:
urlpatterns = patterns('',
url(r'^', include(admin.site.urls)),
)
I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.
Try setting APPEND_SLASH to True in your settings.py. I had a similar problem and this fixed it for me. It's supposed to default to True but I had to set it explicitly.
Related
In a django 1.8 project, I am attempting to redirect http://myProject/ads.txt to an external url http://a.location.that.has.the.ads.txt.file and thus serve the ads.txt file without using ftp to simply place the ads.txt in the root.
Given this minimal directory structure:
django projects
myProject
myapp
urls.py
views.py
someotherapp
yetanotherapp
myProject
settings.py
urls.py (this is the top urls.py)
views.py
in myProject/myProject/urls.py, (the “top” urls.py) I have as the first entry in the urlpatterns list, the lines:
urlpatterns = patterns('',
(r'^ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
followed by many more pattern matching regex’s. This does not work and fails with a 404. What does work is
urlpatterns = patterns('',
(r'^foo/ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
and then calling http://myProject/foo/ads.txt
Unfortunately, ads.txt files must be placed at the site root. I have tried many different regex’s that test fine in a regex validator, but just don’t work (returns 404). How do I do this without the extra dir “foo”? Any thoughts appreciated. Thank you.
Turns out you cannot redirect with the top level urls.py "routing table" to above the Django project root. A nginx server level redirect did the trick.
I have Django app running live on AWS at, www.domain.com/admin. It doesn't posses any html pages, we only make use of Django-Admin.
Now I have to host a website at www.domain.com.
I have my website package in this form,
site
|-sass
|-js
|-img
|-fonts
|-css
|-index.html
I copy-pasted my site folder inside my Django app at my_django_app/templates/
Also, added this :
url(r'^$', TemplateView.as_view(template_name='site/index.html')),
inside my_django_app/my_django_app/urls.py.
And, updated my settings.py with,
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'templates/site'),
]
Now when I runserver and go to www.domain.com, it loads my html file without CSS, JS and Images.
Please suggest me what I am doing wrong.
I am a beginner to python and also never hosted a website ever before.
This answer is a reply to this comment, not an answer to the original question.
In your Apache config, change: WSGIScriptAlias / to WSGIScriptAlias /admin.
Add this before the </VirtualHost> closing tag:
Alias / /path/to/static/site/
<Directory /path/to/static/site/>
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
I haven't tested it, but I believe this should work.
I've been trying to setup an Apache (2.2.15) as a proxy with gunicorn and django (1.6.11) on a CentOS server (6.7), but I'm stuck on a problem regarding the static files. (I'm fairly new to django)
I have already looked at the great documentation that Django provides, as well as a few other stackoverflow's posts, but to no avail.
What I could check :
The problem doesn't seem to come from Django or the templates, as running the development server with 'DEBUG = False' and the option '--insecure' works fine.
It probably is a problem with the Alias and Directory block I added in my virtualhost, but I can't make sense of why it doesn't work.
Gunicorn shouldn't have any word to say in it, as it doesn't serve static files.
What my configuration looks like :
1) I got my project's static files into '/var/www/myproject/static' thanks to the 'collectstatic' command.
2) Then, I created the virtualhosts (where I think the problem is) :
(first one for the redirection to HTTPS)
<virtualhost *:80>
ServerAdmin user#domain.com
ServerName localhost
ServerSignature Off
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</virtualhost>
(second one for the actual work)
<virtualhost *:443>
ServerAdmin user#domain.com
ServerName localhost
ServerSignature Off
DocumentRoot /var/www/myproject
Alias /static/ "/var/www/myproject/static/"
<Directory "/var/www/myproject/static">
Order Allow,Deny
Allow from All
</Directory>
ProxyPass / http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
ProxyPassReverse / http://127.0.0.1:8000/
ProxyPreserveHost On
ProxySet http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
... here goes the SSL and Log configuration ...
</virtualhost>
After a service restart, my static files weren't loaded and I had a 404 error on their get. An 'httpd -S' didn't throw any error and the rest of the functionalities of the web interface are working great.
I also tried without the ending '/' for the '/static/' alias as it seemed to be a problem for some other people, tried to move the files directly under /var/www/myproject and have them accessed without an alias with the DocumentRoot...
If you want to have a look at the django settings.py (don't know if it's relevant, but some django guru could find something wrong there too) :
STATIC_ROOT = '/var/www/myproject/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static',),)
As well as the templates :
{% load staticfiles %}
....
<link href="{% static 'bower_components/bootstrap/dist/css/boostrap.min.css %}" rel="stylesheet">
Urls.py of the project :
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^$', lambda x: redirect('/Collecte/')),
url(r'^Collecte/', include('Collecte.urls', namespace="Collecte")),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
And the urls.py of the app (named 'Collecte'):
from django.conf.urls import patterns, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from Collecte import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^Collectes/Execution/ù', views.CollExecution, name='CollExecution'),
... quite a lot of them ...
)
# Commented this line from a suggestion, was present at start
#urlpatterns += staticfiles_urlpatterns
If you feel like any file is missing for the question to be relevant, just ask for it and I'll do my best :).
Alright,
I found the answer to my question in another stackoverflow thread that had a similar configuration (but with flask instead of django).
As Ajay Gupta said, I think I had a problem in the urls.py file of the project as I had to add 'static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)' (switched from using the staticfiles app django provides to this other way).
The other problem was in the apache virtualhost configuration, as I wanted my static files to be served by apache and not gunicorn or django. I was redirecting everything to the gunicorn server, even the requests to static files. So I had to add 'ProxyPass /static/ !' so that apache serves the files.
I don't know if it's the right way to do it, but it worked for me.
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
is a helper function that works only when
debug = True
So for instance just set debug = True again, restart your server and check
What is wrong:
I have installed django-tinymce to work with text fields in the admin section of my site. The editor works good, though not as expected, because whenever I try to insert an image, link, symbol etc. - that is, whenever a pop-up is ensued - editor opens a 404 Not found window instead of actual widget form.
What I have tried:
I 've tried adding
document.domain = 'my_site.com';
to tiny_mce_popup.js. Nothing changed.
Interestingly enough, the *.htm files, which are supposed to open in pop-up windows, are stored in the directory {{ MEDIA_URL }}js/tiny_mce/themes/advanced/ (as I've mentioned, server doesn't open them). But if I try and open in browser other files from that very same directory (e.g. {{ MEDIA_URL }}js/tiny_mce/themes/advanced/editor_template.js or {{ MEDIA_URL }}js/tiny_mce/themes/advanced/img/flash.gif), everything works - the files are displayed without any error.
In addition, on local server everything works just fine - the problem is encountered only after deployment.
Code that might help identifying the problem:
Project's urls.py:
urlpatterns = patterns('',
#...
url(r'^tinymce/', include('tinymce.urls')),
#...
)
TinyMCE's urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('tinymce.views',
url(r'^js/textareas/(?P<name>.+)/$', 'textareas_js', name='tinymce-js'),
url(r'^js/textareas/(?P<name>.+)/(?P<lang>.*)$', 'textareas_js', name='tinymce-js-lang'),
url(r'^spellchecker/$', 'spell_check'),
url(r'^flatpages_link_list/$', 'flatpages_link_list'),
url(r'^compressor/$', 'compressor', name='tinymce-compressor'),
url(r'^filebrowser/$', 'filebrowser', name='tinymce-filebrowser'),
url(r'^preview/(?P<name>.+)/$', 'preview', name='tinymce-preview'),
)
TinyMCE's settings.py:
import os
from django.conf import settings
DEFAULT_CONFIG = getattr(settings, 'TINYMCE_DEFAULT_CONFIG',
{'theme': "advanced", 'relative_urls': False})
USE_SPELLCHECKER = getattr(settings, 'TINYMCE_SPELLCHECKER', False)
USE_COMPRESSOR = getattr(settings, 'TINYMCE_COMPRESSOR', False)
USE_FILEBROWSER = getattr(settings, 'TINYMCE_FILEBROWSER',
'filebrowser' in settings.INSTALLED_APPS)
JS_URL = getattr(settings, 'TINYMCE_JS_URL',
'%sjs/tiny_mce/tiny_mce.js' % settings.MEDIA_URL)
JS_ROOT = getattr(settings, 'TINYMCE_JS_ROOT',
os.path.join(settings.MEDIA_ROOT, 'js/tiny_mce'))
JS_BASE_URL = JS_URL[:JS_URL.rfind('/')]
So how do I make django-tinymce's pop-ups work? Thanks in advance for your help!
EDIT: Solution found. Turns out my hosting provider didn't include htm(l) in allowed static files extensions list. Now everything's working.
The fact that it is working under the development server, but not live, leads me to think that you haven't set up the media root properly in your apache conf. The development server automatically serves your media and static files but for your live deployment you need to add the media and static aliases with the relevant path.
vhost.conf:
<VirtualHost *:80>
# Your setup for your main site url, then add the below to allow access
# to the media and static roots
Alias /media/ "/path/to/myproject/media/"
<Directory "/path/to/myproject/media/">
Order deny,allow
Allow from all
</Directory>
Alias /static/ "/path/to/myproject/static/"
<Directory "/path/to/myproject/static/">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I am deploying a Django project on apache server with mod_python in linux. I have created a directory structure like:
/var/www/html/django/demoInstall where demoInstall is my project. In the httpd.conf I have put the following code.
<Location "/django/demoInstall">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE demoInstall.settings
PythonOption django.root django/demoInstall
PythonDebug On
PythonPath "['/var/www/html/django'] + sys.path"
</Location>
It is getting me the django environment but the issue is that the urls mentioned in urls.py are not working correctly.
In my url file I have mentioned the url like:
(r'^$', views.index),
Now, in the browser I am putting the url like : http://domainname/django/demoInstall/ and I am expecting the views.index to be invoked. But I guess it is expecting the url to be only: http://domainname/ .
When I change the url mapping to:
(r'^django/demoInstall$', views.index),
it works fine. Please suggest as I do not want to change all the mappings in url config file.
Thanks in advance.
There's a fairly simple way around this using just django, without having to touch apache.
Rename your urls.py to something else, e.g. site_urls.py
Then create a new urls.py which includes that
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^django/demoInstall/', include('site_urls.py')),
)
This will ensure that all the url reversing continues to work, too.
That should be:
PythonOption django.root /django/demoInstall
Ie., must match sub URL in Location directive.
You shouldn't be using that prefix in urls.py.