Django 1.9 Tutorial: No Tests are run - python

I am working through Django 1.9 Django 1.9 Tutorial Part 5.
I am using Python 2.7.6 and Django 1.9.4.
The tree structure of my folders is:
django-mysite/
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── __init__.cpython-34.sublime-workspace
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── polls
├── admin.py
├── admin.pyc
├── apps.py
├── apps.pyc
├── __init__.py
├── __init__.pyc
├── migrations
│   ├── 0001_initial.py
│   ├── 0001_initial.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── models.py
├── models.pyc
├── templates
│   └── polls
│   ├── detail.html
│   ├── index.html
│   ├── results.html
│   └── tests.py
├── tests.py
├── tests.pyc
├── urls.py
├── urls.pyc
├── views.py
└── views.pyc
When I run tests through command:
python manage.py test polls
or
python manage.py test polls.tests
It does not run tests. The output is:
Creating test database for alias 'default'...
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
The tests.py file has the code (as in tutorial)
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
class QuestionMethodTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertEqual(future_question.was_published_recently(), False)
Whats wrong?

Remove the tests.py from template/polls folder and then try.
To run the test, try python manage.py test polls

Related

NotSupportedError at / deterministic=True requires SQLite 3.8.3 or higher

I am trying to deploy a Django application using the default SQLite database to Elastic Beanstalk. The application works fine locally, however, on server, I get the error as :
Any idea what's wrong? Can we not deploy SQLite app on AWS EBS?
Here is my project structure that is getting deployed
├── .ebextensions
│   ├── django.config
├── app
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── static
│   │   └── app
│   │   ├── app.js
│   │   ├── logo.svg
│   │   └── style.css
│   ├── templates
│   │   └── app
│   │   ├── hello.html
│   │   ├── index.html
│   │   └── job_detail.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── env
│   ├── All Virtual env related files
├── images
│   ├── Photo_on_5-9-14_at_2.31_PM.jpg
│   ├── Photo_on_5-9-14_at_2.32_PM.jpg
│   └── Photo_on_5-9-14_at_2.32_PM_v4McLzE.jpg
├── myproject-test
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── requirements.txt
├── staticfiles
│   |-- All static files collected using collectstatic
│   ├── app
│   │   ├── app.js
│   │   ├── logo.svg
│   │   └── style.css
│   └── subscribe
│   ├── email-icon.png
│   ├── main.css
│   └── main.js
├── subscribe
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── form.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_subscribe_option_alter_subscribe_email_and_more.py
│   │   ├── 0003_alter_subscribe_option.py
│   ├── models.py
│   ├── static
│   │   └── subscribe
│   │   ├── email-icon.png
│   │   ├── main.css
│   │   └── main.js
│   ├── templates
│   │   └── subscribe
│   │   ├── subscribe.html
│   │   └── thank_you.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── templates
│   └── base.html
├── upload
│   └── images
│   ├── Photo_on_5-9-14_at_2.31_PM.jpg
│   └── Photo_on_5-9-14_at_2.31_PM_3.jpg
└── uploads
├── __init__.py
├── admin.py
├── apps.py
├── forms.py
├── images
│   └── Photo_on_5-9-14_at_2.31_PM_3.jpg
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_uploadfile.py
│   ├── 0003_alter_uploadfile_file.py
│   ├── __init__.py
├── models.py
├── templates
│   └── uploads
│   ├── add_file.html
│   └── add_image.html
├── tests.py
├── urls.py
└── views.py
Deployment process:
Zip all the necessary files
Create AWS EBS application with Python
Add environment variables
Create app and access URL
Please help.

How to create a table with django models inside the app?

my_api
├── admin.py
├── apps.py
├── heroes
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-38.pyc
│   │   ├── __init__.cpython-38.pyc
│   │   ├── models.cpython-38.pyc
│   │   ├── serializers.cpython-38.pyc
│   │   └── views.cpython-38.pyc
│   ├── serializers.py
│   └── views.py
├── __init__.py
├── migrations
│   ├── __init__.py
│   └── __pycache__
│   └── __init__.cpython-38.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-38.pyc
│   ├── apps.cpython-38.pyc
│   ├── __init__.cpython-38.pyc
│   ├── models.cpython-38.pyc
│   ├── urls.cpython-38.pyc
│   └── views.cpython-38.pyc
├── tests.py
├── urls.py
├── views.py
I am trying to build a simple rest api with drf.
created a directory heroes inside the my_api and created a new class.
now my_api/heroes/models.py looks like this:
class Hero(models.Model):
name = models.CharField(max_length=50)
power = models.CharField(max_length=100)
my_fav = models.BooleanField()
def __str__(self):
return self.name
The problem is, whenever I try to migrate this table doesn't get created. What should I change to make it work?
Hi and welcome to StackOverflow.
Looks like you messed up the folder structure.
From what I see you have one app "heroes" inside another app "my_api".
How did you created the structure?.

Unable to delete User objects in Django

I have a try except block in one of my Django view functions that will delete a User object that was created in the try block if anything should fail. When I attempt to delete the User I get this error message.
OperationalError: no such table: reversion_revision
The same issue happens in the Django admin as well. I am having issues finding similar cases of this OperationalError happening to other people, and am not sure why it is happening. All the other objects that I am deleting in the except block delete without any issues.
#csrf_exempt
def signup(request):
# """Register a new account with a new org."""
if request.is_ajax():
if request.method == "POST":
form = SignUp(requestPost(request))
if form.is_valid():
cleaned_data = form.cleaned_data
email = cleaned_data['email']
password = cleaned_data['password']
org_name = cleaned_data['org_name']
org_username = cleaned_data['org_username']
if cleaned_data['token']:
invite_token = cleaned_data['token']
else:
invite_token = cleaned_data['invite']
try:
account = Account.objects.create(email=email, password=password)
x = email[:30]
userExists = User.objects.filter(username=email[:30])
if not userExists:
if len(email) < 30:
user = User.objects.create_user(email, email, password)
else:
email = email[:30]
user = User.objects.create_user(email, email, password)
if invite_token:
if ThreadInvite.objects.filter(token=invite_token):
invitation = ThreadInvite.objects.get(token=invite_token)
thread = Thread.objects.get(id=invitation.thread.id)
ThreadMember.objects.create(thread=thread, account=account)
else:
invitation = OrgInvite.objects.get(token=invite_token)
if invitation.used:
raise Exception("invitation code is invalid")
org = Org.objects.get(id=invitation.org.id)
OrgMember.objects.create(org=org, account=account)
invitation.used = False
invitation.save()
add_to_welcome(org_id=org.id, account_id=account.id)
if org_username and org_name:
org = Org.objects.create(name=org_name, username=org_username,
actor=account)
OrgMember.objects.create(account=account, org=org)
Thread.objects.create(name='welcome', account=account, owner=account, org=org,
purpose='To welcome new members to the team.')
add_to_welcome(org_id=org.id, account_id=account.id)
login(request)
md = mandrill.Mandrill(settings.MANDRILL_API_KEY)
t = invite_token.replace(' ', '+')
url = "https://localhost:8000/verify/{}".format(t)
message = {
'global_merge_vars': [
{
'name': 'VERIFICATION_URL',
'content': url
},
],
'to': [
{
'email': 'tim#millcreeksoftware.biz',
},
],
'subject': 'Welcome to Human Link',
}
message['from_name'] = message.get('from_name', 'Humanlink')
message['from_email'] = message.get('from_email',
'support#humanlink.co')
try:
md.messages.send_template(
template_name='humanlink-welcome', message=message,
template_content=[], async=True)
except mandrill.Error as e:
logging.exception(e)
raise Exception(e)
context = {
'message': 'ok'
}
return composeJsonResponse(200, "", context)
except Exception, e:
logging.error(e)
Account.objects.filter(email=email, password=password).delete()
User.objects.filter(username=email[:30]).delete()
Org.objects.filter(name=org_name, username=org_username).delete()
Structure
├── account
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── api_helpers.py
├── api_helpers.pyc
├── app
│   └── components
├── bower.json
├── bower_components
│   ├── angular
│   ├── angular-animate
│   ├── angular-bootstrap
│   ├── angular-cookies
│   ├── angular-messages
│   ├── angular-sanitize
│   ├── angular-scroll-glue
│   ├── angular-touch
│   ├── angular-ui-router
│   ├── bootstrap
│   ├── checklist-model
│   ├── font-awesome
│   ├── jquery
│   ├── moment
│   ├── pusher-websocket-iso
│   └── underscore
├── cron_tasks.py
├── gulpfile.js
├── logs
│   └── readme.txt
├── manage.py
├── message
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
|
├── org
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── package.json
├── readme.txt
├── settings
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── base.py
│   ├── base.pyc
│   ├── cron.py
│   ├── development.py
│   ├── development.pyc
│   └── production.py
├── sqlite3
│   └── local.db
├── stylesheets
│   └── less
├── templates
│   ├── accounts
│   ├── admin
│   ├── dashboard
│   ├── footer.html
│   ├── home
│   ├── layouts
│   ├── nav.html
│   ├── pages
│   ├── settings
│   ├── shared
│   ├── site-alert.html
│   └── site.html
├── third_party
│   ├── classytags
│   ├── dateutil
│   ├── django_pusher
│   ├── itsdangerous.py
│   ├── itsdangerous.pyc
│   ├── mandrill.py
│   ├── mandrill.pyc
│   ├── mptt
│   ├── pusher
│   ├── requests
│   ├── reversion
│   ├── six.py
│   ├── six.pyc
│   ├── suit
│   └── werkzeug
├── utility.pyc
├── webapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── webapp_admin
├── __init__.py
├── __init__.pyc
└── static
Problem here is not in your code but in your database state. Looks like you added django reversion app. Which adds new Models in your project. Run
python manage.py syncdb
or if you in 1.8+
python manage.py migrate
Update
If this doesn't help than there are no migrations for your 3rd-party app you need to first create them with
python manage.py makemigrations name_3rd_party_app
Be careful with creating migrations on 3rd-party apps. When you run makemigrations it creates migrations in 3rd-party app package. So it won't be in your code. And after you deploy it, or other user uses it there are will not be this migrations. So you need to supply custom path for created migrations with https://docs.djangoproject.com/en/1.9/ref/settings/#migration-modules
And then run migrate
Try running
./manage.py makemigrations revisions
then
./manage.py migrate
or just delete the db file and start over with
./manage.py migrate

ImportError: No module named lop.models

I know there are a lot of questions related to this, but those answers don't seem to work in my situation. I'm new to Django (I've done the tutorial), but I'm fixing someone else's code who I can no longer contact.
I'm running django 1.5 on Debian with python 2.7.
I received this error.
File "views-full.py", line 1, in <module>
from lop.models import File, V1, V2
ImportError: No module named lop.models.
views-full.py:
from lop.models import File, V1, V2
...
My tree is this (to save time, my views-full.py is under lop):
Main
├── Main
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── lop
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_migrate.py
│   │   ├── 0001_migrate.pyc
│   │   ├── 0002_migrate.py
│   │   ├── 0002_migrate.pyc
│   │   ├── 0003_auto__add_category.py
│   │   ├── 0003_auto__add_category.pyc
│   │   ├── 0004_auto__add_field_script_category.py
│   │   ├── 0004_auto__add_field_script_category.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── viewsb.py
│   │   ├── viewsb.pyc
│   │   └── viewsb.py.save
│   ├── views-full.py
│   ├── views.pyc
│   ├── views.py.save
│   └── views-test.py
├── scripts [39 entries exceeds filelimit, not opening dir]
├── sqlite3.db
├── static [29 entries exceeds filelimit, not opening dir]
├── templates
│   ├── entry2-full.html
│   ├── entry2.html
│   ├── entry3-full.html
│   ├── entry3.html
│   ├── entry.html
│   ├── index.html
│   ├── index.html.old
│   ├── scriptlist.html
│   └── testData.html
└── user-dirs [109 entries exceeds filelimit, not opening dir]
As you see, both my __init__.py and models.py are in the same folder (which I know that them not being there was the problem in other cases).
settings.py:
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
# 'django.contrib.admindocs',
'lop',
'south',
)
...
I feel like I'm making some rookie mistake, but I can't figure it out.
I am not familiar with django, but here's what I do in that kind of situation:
import sys
sys.path.append('/path/of/folder/where/module/is/')
from new_module import new_function
So I figured out the problem which was from a fault on my part.
For the longest time, I thought that to test for any compilation errors (syntax), I would run "python < filename > migrate" or "python < filename > makemigrations" on the shell. My belief was that all changes needed to be migrated and not just ones in the database. I get errors messages when syntax mistake including this one and it has worked in other cases before. Someone eventually pointed out to me that is NOT how you test it and running "python manage.py runserver 0.0.0.0:8000" would point out any errors in the code in your local machine. I got no error messages for my specific file after that.
Gabriel L'Heureux, I still appreciated your help (I would give you a point up for it but I don't have enough points to do so) so you have my thanks.

Django: Application labels aren't unique

I have been working on the issue of duplicate labels in Django and from this answer I have added the following files to my "jobs" project folder:
jobs/apps.py
# jobs/apps.py
from django.apps import AppConfig
class JobsConfig(AppConfig):
name = 'jobs'
verbose_name = "jobs2"
jobs/init.py
# jobs/__init__.py
default_app_config = 'jobs.apps.JobsConfig'
This hasn't really helped much and I still get the error when trying syncdb:
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: jobs
Also, changing from "name = 'jobs'" to "name = 'jobs2'" just gives me the error:
ImportError: No module named jobs2
File Structure
/opt/Webapp
├── userfiles
├── templates
│   └── admin
│   └── base.html
├── static
│   ├── admin_tools
│   │   ├── images
│   │   │   └── apto.gif
│   │   └── css
│   │   └── theming.css
│   └── admin
│   └── css
│   └── base.css
├── smartrecruitment
│   ├── wsgi.py
│   ├── urls.py
│   ├── settings.pyc
│   ├── settings.py
│   ├── __init__.pyc
│   └── __init__.py
├── requirements.txt
├── manage.py
├── jobs
│   ├── views.py
│   ├── urls.py
│   ├── tests.py
│   ├── testhelpers.py
│   ├── templates
│   │   └── jobs
│   │   ├── test.html
│   │   ├── success.html
│   │   ├── registration.html
│   │   ├── registrationcomplete.html
│   │   └── application.html
│   ├── tables.py
│   ├── static
│   │   └── jobs
│   │   ├── styles
│   │   │   ├── index.css
│   │   │   ├── hide_admin_original.css
│   │   │   └── application.css
│   │   ├── style.css
│   │   └── images
│   │   └── apto.gif
│   ├── models.py
│   ├── migrations
│   │   ├── __init__.py
│   │   ├── 0002_auto__del_field_registrant_name__add_field_registrant_first_name__add_.py
│   │   └── 0001_initial.py
│   ├── lists.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.py
│   ├── apps.pyc
│   ├── apps.py
│   └── admin.py
├── fileuploads
│   ├── tests.py
│   ├── templates
│   │   └── fileuploads
│   │   ├── index.html
│   │   ├── details.html
│   │   ├── base.html
│   │   └── add.html
│   ├── models.pyc
│   ├── models.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.pyc
│   ├── forms.py
│   ├── context_processors.py
│   └── admin.pyc
├── dashboard.pyc
└── dashboard.py
You have a mix of old-style (south: 0002_auto_del...) and new-style (django: 0001_initial) migrations in your jobs app. Easiest fix would be to delete all numbered migrations rm jobs/migrations/0???_*.py* and recreate migrations by running manage.py makemigrations

Categories