I cant get past the first step, setting it up.
This is my settings.py of the application:
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/django/ristoturisto/media'
FILEBROWSER_DIRECTORY = ''
STATIC_ROOT = '/home/django/ristoturisto/static'
STATIC_URL = '/static/'
FILEBROWSER_DIRECTORY = MEDIA_ROOT
The folders exist, i copied the name from pwd.
I did collect static, that worked.
But when i do:
python manage.py test filebrowser
I get the follwing error:
Creating Test for the FileBrowser site: filebrowser
Creating test database for alias 'default'...
...........F......ERemoving left-over tmp dir: /home/django/ristoturisto/media/tmp_test_0
======================================================================
ERROR: runTest (filebrowser.tests.sites.TestSite_filebrowser)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/filebrowser/tests/sites.py", line 236, in runTest
test_do_upload(self)
File "/usr/lib/python2.6/site-packages/filebrowser/tests/sites.py", line 99, in test_do_upload
response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
File "/usr/lib/python2.6/site-packages/django/test/client.py", line 449, in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
File "/usr/lib/python2.6/site-packages/django/test/client.py", line 262, in post
return self.request(**r)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/views/decorators.py", line 16, in _checklogin
return view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/filebrowser/sites.py", line 476, in _upload_file
file_already_exists = self.storage.exists(file_name)
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py", line 230, in exists
return os.path.exists(self.path(name))
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py", line 246, in path
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
SuspiciousOperation: Attempted access to '/tmp_test_0/testimage.jpg' denied.
======================================================================
FAIL: test_directory (filebrowser.tests.settings.SettingsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/filebrowser/tests/settings.py", line 31, in test_directory
self.assertEqual(os.path.basename(DIRECTORY), '')
AssertionError: 'media' != ''
----------------------------------------------------------------------
Ran 19 tests in 3.810s
FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...
The variable MEDIA_ROOT must have trailing slash at the end.
Comment and one line below from django-filebrowser settings file
# DO NOT USE A SLASH AT THE BEGINNING, DO NOT FORGET THE TRAILING SLASH AT THE END.
DIRECTORY = getattr(settings, "FILEBROWSER_DIRECTORY", 'uploads/')
Two lines from settings test where script fail
# Check for trailing slash
self.assertEqual(os.path.basename(DIRECTORY), '')
Related
I'm following along with a lecture on django testing and this is one of the tests:
def test_invalid_flight_page(self):
max_id = Flight.objects.all().aggregate(Max("id"))["id__max"]
c = Client()
response = c.get(f"/flights/{max_id + 1}")
self.assertEqual(response.status_code, 404)
When I run manage.py tests it throws an error on this test, essentially saying there is no matching flight:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.......E..
======================================================================
ERROR: test_invalid_flight_page (flights.tests.FlightTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\sarah\Desktop\airline\flights\tests.py", line 68, in test_invalid_flight_page
response = c.get(f"/flights/{max_id + 1}")
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 732, in get
response = super().get(path, data=data, secure=secure, **extra)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 393, in get
return self.generic('GET', path, secure=secure, **{
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 470, in generic
return self.request(**r)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 709, in request
self.check_exception(response)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 571, in check_exception
raise exc_value
File "C:\Python\Python385\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Python\Python385\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\sarah\Desktop\airline\flights\views.py", line 21, in flight
flight = Flight.objects.get(pk=flight_id)
File "C:\Python\Python385\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python\Python385\lib\site-packages\django\db\models\query.py", line 429, in get
raise self.model.DoesNotExist(
flights.models.Flight.DoesNotExist: Flight matching query does not exist.
----------------------------------------------------------------------
Ran 10 tests in 0.120s
FAILED (errors=1)
Destroying test database for alias 'default'...
But that is the point, there is no flight with that id so the response status code for that request should equal 404. As far as I can see I have copied the code accurately from the lecture but the lecturer's tests are all running okay. Can anyone see what I may be missing?
When I change the expected response.status_code to 200 self.assertEqual(response.status_code, 200) it gives me the same error so this indicates to me that the main problem lies with the response line?
Please let me know if there is anything more you need to see.
Your view class is not handling when the query resolves to nothing. In django it's done as follows:
try:
result = SomeModel.objects.get(pk=some_id)
except SomeModel.DoesNotExist:
# Return 404 here
I've met the same problem. Indeed, it came from the response line or client.get() method itself. In my case, I add "/" at the end of the quotation mark as:
response = c.get(f"/flights/{max_id + 1}/")
And then check assert condition. Now it functions as I want.
Also found that:
If you substitute a word instead of a number as an invalid page, it works just fine. For example:
THIS ONE OK (404 were detected):
response = c.get(f"/flights/whatever")
THIS ONE DOESN'T WORK (404 were not detected and errors were alerted):
response = c.get(f"flights/15")
I am trying to learn Django,
In settings.py I have set:
MEDIA_ROOT = '/home/hussain/django/ratingsite/ratingsite/media'
MEDIA_URL = 'media/'
STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/home/hussain/django/ratingsite/ratingsite/static',)
I have my css files in my
/home/hussain/django/ratingsite/ratingsite/static/css/default.css
but when I try to access the webpage it doesnot load the css and gives Error.
"NetworkError: 500 Internal Server Error - http://localhost:8000/static/css/default.css"
I am new to this so I dont understand what is my root directory. what runs first and how Django tries to find the resources. If someone can guide me through this it would be great.
Guyz Guyz I was just doing hit and try and this happened. I renamed my static folder to staticfiles and then in STATICFILES_DIRS replaced the path from '/home/hussain/django/ratingsite/ratingsite/static/', to '/home/hussain/django/ratingsite/ratingsite/staticfiles/', and it worked. I have No Idea why. can someone please explain ?
output of python manage.py runserver --traceback
hussain#jarvis:~/django/ratingsite$ python manage.py runserver --traceback
Validating models...
0 errors found
December 08, 2013 - 01:48:39
Django version 1.5.4, using settings 'ratingsite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Dec/2013 01:48:41] "GET / HTTP/1.1" 200 930
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
return self.serve(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
return serve(request, self.file_path(request.path), insecure=True)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
absolute_path = finders.find(normalized_path)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
for finder in get_finders():
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
yield get_finder(finder_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
result = func(*args)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
return Finder()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
"The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
[08/Dec/2013 01:48:42] "GET /static/css/default.css HTTP/1.1" 500 59
Traceback (most recent call last):
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
return self.serve(request)
return self.serve(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
return serve(request, self.file_path(request.path), insecure=True)
return serve(request, self.file_path(request.path), insecure=True)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
absolute_path = finders.find(normalized_path)
absolute_path = finders.find(normalized_path)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
for finder in get_finders():
for finder in get_finders():
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
yield get_finder(finder_path)
yield get_finder(finder_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
result = func(*args)
result = func(*args)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
return Finder()
return Finder()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
"The STATICFILES_DIRS setting should "
"The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
This makes sense now.
The problem is this as is written in your stacktrace:
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
And per your initial configuration:
STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'
STATICFILES_DIRS = (
'/home/hussain/django/ratingsite/ratingsite/static',)
You will not that both of these point to exactly the same directory. This is not allowed for the reason described below.
The ImproperlyConfigured error was fixed when you changed the location of one of these configurations (so they were no longer identical).
The solution here is to:
create a different STATIC_ROOT directory (we'd call this something like /public/static/) do not put any files in this directory this is where all of the static files are automatically collected to when your site is ready to publish.
put the location/s of the files that you are developing in the STATICFILES_DIRS.
When you are ready to publish your project you are able to run $ ./manage.py collectstatic which will go through all the apps and staticfiles directories your project uses and compile a set of all the "final" static files you need in the STATIC_ROOT directory ready for publication with your finished site.
If your STATIC_ROOT directory was included in the directories that was gone through by collectstatic there would obviously be a recursive loop.
Have you added the following to your urls.py?
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Per the instructions here:
https://docs.djangoproject.com/en/dev/howto/static-files/
Although you are getting a 500 error which suggests another problem.
What is the output of?:
$ python manage.py validate --traceback
Does this state some other kind of error?
How about the output of:
$ python manage.py shell
>>> from django.conf import settings
>>> print(settings.STATICFILES_DIRS)
As it is returning 500 rather than the expected 404 you could run:
$ python manage.py runserver --traceback
... and then see what the traceback history returns when you load the webpage.
I have a website using Flask. The main program is pretty long, so I’ve used a paste to show you the code. When I run it with ./site serve --debug it works perfectly, but I can’t freeze it with Flask-frozen. I have this error :
$ ./site build
Building website...
./site:240: MimetypeMismatchWarning: Filename extension of u'sitemap.xml' (type application/xml) does not match Content-Type: text/html; charset=utf-8
freezer.freeze()
Traceback (most recent call last):
File "./site", line 346, in <module>
parser.dispatch()
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/argh/helpers.py", line 53, in dispatch
return dispatch(self, *args, **kwargs)
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/argh/dispatching.py", line 123, in dispatch
for line in lines:
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/argh/dispatching.py", line 199, in _execute_command
for line in result:
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/argh/dispatching.py", line 182, in _call
result = args.function(*positional, **keywords)
File "./site", line 240, in build
freezer.freeze()
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/flask_frozen/__init__.py", line 140, in freeze
new_filename = self._build_one(url)
File "/home/guillaume/nicolas.perriault.net/env/lib/python2.7/site-packages/flask_frozen/__init__.py", line 250, in _build_one
% (response.status, url))
ValueError: Unexpected status '500 INTERNAL SERVER ERROR' on URL /403.html
If I delete the part about 403.html in site.py, I have the same error with 404, then 500, then contact.html, then /. And I can’t find why. Is there anybody who has an idea ?
Enable testing while freezing your application. It should produce more information about error.
#command
def build():
""" Builds this site.
"""
print("Building website...")
app.debug = False
app.testing = True
asset_manager.config['ASSETS_DEBUG'] = False
freezer.freeze()
local("cp ./static/*.ico ./build/")
local("cp ./static/*.txt ./build/")
local("cp ./static/*.xml ./build/")
print("Done.")
Frozen-flask uses app.test_client() look at it's docs.
I get this over and over again after running manage.py test:
ERROR 4280 140735184636256 base: Internal Server Error: /add/error/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/messages/tests/urls.py", line 30, in add
getattr(messages, message_type)(request, msg)
File "/Library/Python/2.7/site-packages/django/contrib/messages/api.py", line 102, in error
fail_silently=fail_silently)
File "/Library/Python/2.7/site-packages/django/contrib/messages/api.py", line 22, in add_message
raise MessageFailure('You cannot add messages without installing '
MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
MessageMiddleware is enabled in my settings.py so not sure why. Any clue?
You are running all defined tests, including django's own tests. Specify the modules that you want to test:
python manage.py test my_app my_other_app
i got this error when i try to run this test case: WHICH IS written in tests.py of my django application:
def test_accounts_register( self ):
self.url = 'http://royalflag.com.pk/accounts/register/'
self.c = Client()
self.values = {
'email': 'bilal#gmail.com',
'first_name': 'bilal',
'last_name': 'bash',
'password1': 'bilal',
'password2': 'bilal',
}
self.response = self.c.post( self.url, self.values )
my django version is 1.2.1 and python 2.6 and satchmo version is 0.9.2-pre hg-unknown
the complete error log is:
.E....
======================================================================
ERROR: test_accounts_register (administration.tests.AccountsRegisterTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\pytho\satchmo\administration\tests.py", line 53, in test_accounts_reg
ister
self.response = self.c.get( self.url )
File "C:\django\django\test\client.py", line 290, in get
response = self.request(**r)
File "C:\django\django\test\client.py", line 230, in request
response = self.handler(environ)
File "C:\django\django\test\client.py", line 74, in __call__
response = self.get_response(request)
File "C:\django\django\core\handlers\base.py", line 141, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\django\django\core\handlers\base.py", line 180, in handle_uncaught_ex
ception
return callback(request, **param_dict)
File "C:\django\django\views\defaults.py", line 23, in server_error
t = loader.get_template(template_name) # You need to create a 500.html templ
ate.
File "C:\django\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\django\django\template\loader.py", line 134, in find_template
source, display_name = loader(name, dirs)
File "C:\django\django\template\loader.py", line 42, in __call__
return self.load_template(template_name, template_dirs)
File "C:\django\django\template\loader.py", line 48, in load_template
template = get_template_from_string(source, origin, template_name)
File "C:\django\django\template\loader.py", line 168, in get_template_from_str
ing
return Template(source, origin, name)
File "C:\django\django\template\__init__.py", line 158, in __init__
self.nodelist = compile_string(template_string, origin)
File "C:\django\django\template\__init__.py", line 186, in compile_string
return parser.parse()
File "C:\django\django\template\__init__.py", line 282, in parse
compiled_result = compile_func(self, token)
File "C:\django\django\template\defaulttags.py", line 921, in load
(taglib, e))
TemplateSyntaxError: 'settings_tags' is not a valid tag library: Template librar
y settings_tags not found, tried django.templatetags.settings_tags,satchmo_store
.shop.templatetags.settings_tags,django.contrib.admin.templatetags.settings_tags
,django.contrib.comments.templatetags.settings_tags,django.contrib.humanize.temp
latetags.settings_tags,livesettings.templatetags.settings_tags,sorl.thumbnail.te
mplatetags.settings_tags,satchmo_store.contact.templatetags.settings_tags,tax.te
mplatetags.settings_tags,pagination.templatetags.settings_tags,product.templatet
ags.settings_tags,payment.templatetags.settings_tags,payment.modules.giftcertifi
cate.templatetags.settings_tags,satchmo_utils.templatetags.settings_tags,app_plu
gins.templatetags.settings_tags,tinymce.templatetags.settings_tags
----------------------------------------------------------------------
Ran 6 tests in 47.468s
FAILED (errors=1)
Destroying test database 'default'...
It seems to me you probably have a code like {% load settings_tags %} somewhere in your template. Django looks for templatetags/settings_tags.py file in your installed apps' directories. This is the result of not finding a file like this. Maybe the app, which contains it is not in your INSTALLED_APPS or maybe it's a typo. You should be getting the same error when you put this url in your browser.
Sometimes this happens when you forgot to put an __ init __.py in the package.
Like #AJJ said, you may have to restart the server to get the new tags loaded
This is a common issue for this package. When you get it from pypi, it does not contains the template tag: settings_tag.py and that will cause the error 'settings_tags' is not a valid tag library: Template library settings_tags not found.
The current solution is to install it from the github zip.