Django tests failing on CI after adding django-bower - django

I have a Django project where I have a few frontend dependencies and so I tried adding django-bower to help me manage them.
I have replaced the previous dependencies with the bower versions and everything seems to be working fine. I'm running my tests with Selenium and when I run them locally, they are passing.
However, when I proceed to test on a CI server, the Selenium tests are failing, with an error message that it is unable to locate an element on the page. This is a form element and I am sure it is there.
I'm trying to figure out why the tests are passing locally but failing on the CI server. Before adding django-bower all tests were passing also in the CI server.
Here is a snippet of the relevant part in the Django settings
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Compressor finder
"compressor.finders.CompressorFinder",
# Django bower finder
"djangobower.finders.BowerFinder",
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
BOWER_COMPONENTS_ROOT = os.path.join(BASE_DIR, 'static', 'js')
BOWER_INSTALLED_APPS = (
'fontawesome#4.3.0',
'jquery-validation#1.13.1',
'magnific-popup#1.0.0',
'masonry#3.2.2',
'materialize#0.95.3',
)
The bower components are installed in static/js/bower_components
The error message provided on the CI server is
NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'message-form'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"93","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60555","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"bb17c920-c5dd-11e4-9c9b-e7fbd91dc2da\", \"value\": \"message-form\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/bb17c920-c5dd-11e4-9c9b-e7fbd91dc2da/element"}}
Any help would really be appreciated. I've Googled everything I can think of and can't seem to find a solution.

This would be still a guess, but I've seen these kind of problems solved by tweaking your tests and adding an Explicit Wait. Instead of just:
form = driver.find_element_by_id('message-form')
use:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
form = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "message-form")))
Basically, you are waiting for the element to become present on a page for 10 seconds, checking every 500 ms. If the element would not become present in 10 seconds, it would throw TimeoutException.

I use Bower with Django projects, but I don't used django-bower. I use it standalone (via bower install), have it store front-end deps in static/bower-components, and refer to that path in templates with the {% static %} template tag. No idea whether doing that would affect your odd test results, but worth a try.

Related

having problem with django static file set up

I can only and only set the static files in Django with this code
In Installed Apps in settings.py
'....',
'staticfiles',
'....
In end of settings.py
STATIC_URL = '/staic/'
STATICFILE_DIRS = [
os.path.join(BASE_DIR, '/static')
]
And It worked. At My First Time, I also tried this
In urls.py
#this way
urlpatterns = [
...,
...,
.....,
] + static(STATIC_URL, document_root=STATIC_ROOT)
#or
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
At the first time it worked but after that project this way didn't work
I viewed thousands of websites.
I did all the thing in the code like the youtube tutorial
But the second code didn't work anymore. But I cannot use the development server in production. But the first code (which works for me) requires a development server.
If Anyone knows please (if possible) give me the example code to try it, the possible answer, and all the websites where I can know more. It would be helpful.
Thanks Very Much
in settings.py write this code
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Is not a metter of URLs in your application.
As you say, django development server is not suitable for production environment.
You need a reverse proxy like Apache or Nginx in order to be able to serve the static files to your application and WSGI HTTP Server to run your application like gunicorn
Here is a link with a very nice guide on how to setup gunicorn and nginx for django production environment.
DigitalOcean
Vexxhost
Medium

Static method returning empty list in Django

I am trying to run a Django app on local server. It works fine on mu Ubuntu machine but in mac, I can't get the CSS for
localhost:8000/admin
and
localhost:8000/docs to load.
On digging further, I found out that the static URL in main "urls.py" file
return an empty list instead of a URL pattern.
Does anyone have an idea why it is like that on the new mac system?
I have had the same issue. I was able to fix it manually by adding the following snippet to my urls.py file in the project (the urls.py file next to settings.py)...
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
. . .
if settings.TESTING_PRODUCTION:
urlpatterns += [
re_path(r'^static/(?P<path>.*)$', serve, {
'document_root': settings.STATIC_ROOT,
}),
]
I pulled this together from the Django Docs here.
I needed to do this so that I could test the "production" environment with manage.py runserver by manually setting DEBUG = False in settings.py which changes a few other URLs and also turns off trace printing in my code.
In my settings.py file, I have some code to set TESTING_PRODUCTION to True as well. But in actual production with a real web server, the code should set TESTING_PRODUCTION to False so that the static files can be served by the webserver directly and not through Django.

Cannot get django-debug-toolbar to appear

No matter what I do, I simply cannot get django-debug-toolbar to appear. I've tried everything suggested in every answer on this question.
I have DEBUG=True in my settings
I have django.contrib.staticfiles and debug_toolbar in INSTALLED_APPS
I have 'debug_toolbar.middleware.DebugToolbarMiddleware' high up in MIDDLEWARE_CLASSES
I have INTERNAL_IPS = () in my settings
I tried adding print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR']) in a view, and it printed IP Address for debug-toolbar: 127.0.0.1
I have a closing </body></html> in my template
I have run pip install django-debug-toolbar in my virtualenv, without any issues
I have run python manage.py collectstatic and there is a debug_toolbar directory in my static files
When I run the app, I see no request in the console for any URLs containing django_debug_toolbar, so I suspect it's the application not being loaded.
I don't see any failed requests in the developer console, either.
I've read the django-debug-toolbar installation docs and am out of ideas.
Does anyone have any suggestions for debugging? I'm running OSX and Django 1.7. The curious thing is that debug-toolbar WAS appearing just fine - I think I've made some tweak that caused it to vanish, but I don't know what.
UPDATE: I've even tried adding this in my settings file, which is supposed to force the toolbar to appear:
def show_toolbar(request):
return True
SHOW_TOOLBAR_CALLBACK = show_toolbar
But it doesn't help.
I've also tried throwing a deliberate exception in my view, so that I can check DEBUG is on and all the settings are as above. They are, and still no toolbar!
UPDATE 2: When I set INTERNAL_IPS=('127.0.0.1',), I start to see debug-toolbar requests in the console, but no toolbar on the page.
And the following HTML appears in my page - so the toolbar is there, but it's not visible because it's got display=none set all over it:
I had the same problem but managed to fix it following dvl's comment on this page. Here is a summary of the fix:
In settings.py
if DEBUG:
MIDDLEWARE += (
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
INSTALLED_APPS += (
'debug_toolbar',
)
INTERNAL_IPS = ('127.0.0.1', )
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
}
In the project urls.py, add this url pattern to the end:
from django.conf import settings
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
Some information for news users as me, when dev on virtual or remote machine
Add this ligne in a views.py file
print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR'])
When the views is call, you can see the client IP in the shell
You have to add this IP the settings.py file
INTERNAL_IPS = ('IP')
All of the divs with display: none; are in fact behaving properly. They won't change to display: block; until you actually click on them in the toolbar itself.
The button used to toggle the toolbar is the div with an id="djDebugToolbarHandle". As you can see in your console, this button has a top position of 2310px. What this means is that it is rendering, but it is just way down off the page.
Try typing the following in the console to reset its position:
document.getElementById('djDebugToolbarHandle').style.top="30px";
I had the same problem. Changing the finder module in my settings.py worked for me:
STATICFILES_FINDERS = (
#'django.contrib.staticfiles.finders.FileSystemFinder', #THIS BREAKES debug_toolbar
'django.contrib.staticfiles.finders.AppDirectoriesFinder', #THIS WORKS
)
Make sure to clean the browser cache after this change.
But after this, Django gave me error messages during collectstatic, due to this issue. I solved creating two configurations in my settings.py:
class Production(Base):
DEBUG = False
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
class Develop(Base):
DEBUG = True
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I hope it helps.
One reason why django-debug-toolbar might appear but not appear correctly, (items stuck in "Loading") is if manage.py collectstatic has not been run. Just thought I'd post that here in case it helps someone.
i was have same issue with django-toolbar
all tags have class djdt-hidden and hidden
<div id="djDebug" class="djdt-hidden" dir="ltr" data-default-show="true">
i using pycharm and GoogleChrome
just using FireFox and it was fixed

Directory 'uploads/' for the site filebrowser.filebrowser does not exist

I've managed to use django-filebrowser to upload file via TinyMCE based on this documentation:
http://django-filebrowser.readthedocs.org/en/latest/quickstart.html
I've put reference to tiny_mce.js and tinymce_setup.js from Grappeli folder to my template,now TinyMCE works.
but when testing filebrowser with this command:python manage.py test filebrowser I get this error:Directory 'uploads/' for the site filebrowser.filebrowser does not exist. ,of course all of project throw this error.I searched this error in google but no proper result!
Note:I've gotten Grappeli worked already.
what do U think?
For those having problems with filebrowser setup, here is what is required to set it up after you install it.
Add it to your INSTALLED_APPS as:
'filebrowser',
In your urls.py master file, add the file browser pattern:
from filebrowser.sites import site
urlpatterns = patterns('',
(r'^admin/filebrowser/', include(site.urls)),
....)
Then make sure you have your MEDIA_ROOT set and that by default the uploads/ folder exists in the MEDIA_ROOT. You can change the uploads folder by settting FILEBROWSER_DIRECTORY to something different:
FILEBROWSER_DIRECTORY = 'filebrowser_uploads/'
Then I usually run a test and see if anything is broken and why:
python manage.py test filebrowser
I don't know why the setup doesn't create the default directory for it if MEDIA_ROOT exists. Instead it checks if MEDIA_ROOT + FILEBROWSER_DIRECTORY exist and it fails if it doesn't. More on this in the quick start guide.

Django Admin CSS missing

I've been messing around with the new collectstatic command and have got it working for my normal pages. That is to say, I am able to load my css at this location http://localhost:8000/static/css/main.css. However, the css for my django admin doesn't seem to be showing up.
When I navigate to the admin css location at http://localhost:8000/static/admin/css/base.css, I'm getting a 404 page not found with the following error: /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/css/base.css" does not exist. Looking in django-trunk, I never had the /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/ folder to begin with.
Is that weird?
In any case, in my static folder, there is an admin folder with the accompanying css, img and js folders which was created when I ran collectstatic and the url of the base.css seems to be pointing to that location.
This is happening on my django development server. Here are some snippets to aid in the bug hunt:
urls
33 # In order for Dev Server to serve media files for the frontend site.
34 urlpatterns += staticfiles_urlpatterns()
35
36 try:
37 if settings.DEBUG: # defined in manage.py when the first arg is "runserver"
38 urlpatterns += patterns('',
39 (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
40 (r'^media-admin/(?P<path>.*)$', 'django.views.static.serve',{'document_root': os.path.join(settings.MEDIA_ROOT, '..', settings.ADMIN_MEDIA_PREFIX)}),
41 )
42 except NameError:
43 pass
I think it might be something to do with line 40 in my URLS file but changing media-admin to static/admin didnt help.
settings
58 ADMIN_MEDIA_PREFIX = '/static/admin'
69 STATIC_ROOT = os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', MEDIA_DIR, 'static')), '')
70
71 # URL prefix for static files.
72 # Example: "http://media.lawrence.com/static/"
73 STATIC_URL = '/static/'
74
75 # Additional locations of static files. Global files are stored in here
76 STATICFILES_DIRS = (
77 os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', 'proj_public', 'static', 'proj')), ''),
78 )
79
Django recommends that you deploy static files with a web server other than wsgi.
In settings.py, set:
STATIC_ROOT = 'static'
Run python manage.py collectstatic, which will copy the Django admin static files to /path/to/project/static/
Configure your static file server. If you use Nginx, you could add this config:
location /static/ {
alias /path/to/project/static/;
expires modified +1w;
}
Reload your web server
You should now have access to the static files.
In Django 1.4 ADMIN_MEDIA_PREFIX is deprecated. Here are the steps I followed to catch up with these somewhat recent Django changes:
in settings.py, add django.contrib.staticfiles to INSTALLED_APPS
in settings.py define STATIC_URL — the staticfiles app won't run without it. While using runserver they'll get handled magically, but when you deploy, this needs to be a location where those resources can be fetched by a browser.
I think that's all there was to it.
I'm using Django 1.4.3
What did NOT work for me:
No matter how much I edited ADMIN_MEDIA_PREFIX in settings.py I noticed no change in the HTML generated for the Django Admin pages. It always says /media/admin/base.css when I view the source.
What DID work for me.
Copied the 'admin' folder from /django/contrib/admin/static/ and pasted it into my projects 'media' folder
Now it works great.
It seems dumb, but I actually had this exact issue and the solution was to set DEBUG=False to DEBUG=True on my local dev environment. When debug is set to False, it thinks it's in a production environment which relies on a place to put static files, such as /var/www/html/static, whereas the debug set to True just uses the local directory.
Also make sure that AppDirectoriesFinder is not commented, happens when you're trying to customize your own app structure. Unfortunatelly it's pointless to seek such information in official docs.
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
just change one thing in settings.py
DEBUG = False # not working if you are not serve your static files for production.
just change
DEBUG = True
You need a trailing slash in your ADMIN_MEDIA_PREFIX setting.
Change to:
ADMIN_MEDIA_PREFIX = '/static/admin/'
I'm using chef to auto-build my django server on an AWS Ubuntu server. This post helped, but what I wound up doing was to add the directory to the package admin static pages in a local_setings.py:
https://github.com/jaycrossler/geoq-chef-repo/blob/master/cookbooks/geoq/templates/default/local_settings.py.erb#L16
(added to local_settings.py or to settings.py):
STATICFILES_DIRS = ('<%= node['geoq']['virtualenv']['location'] %>/local/lib/python2.7/site-packages/django/contrib/admin/static/',)
This resulted in local_settings.py having:
STATICFILES_DIRS = ('/var/lib/geoq/local/lib/python2.7/site-packages/django/contrib/admin/static/',)
Note, that if you have other items already in your STATICFILES_DIRS, you might want to append to the list, rather than overwriting it.
in my project, the solution is in settings.py, set:
DEBUG = False # debug false mod not working css
One solution might be to add your local IP to the ALLOWED_HOSTS list in settings.py
e.g.
CURRENT_IP = '192.168.0.123'
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '0.0.0.0', CURRENT_IP]
you need
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I don't know how it was resolved, but I took these steps when I encountered the same problem. I simply add these line in settings.py.
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder','compressor.finders.CompressorFinder', )
i hope this will help you all.
In settings.py
Don't use tuple for the
STATICFILES_DIRS =(
os.path.join(BASE_DIR, 'static'),
)
you should use list,like this
STATICFILES_DIRS =[
os.path.join(BASE_DIR, 'static'),
]