djangorecipe test command - django

I've got a question about testing my Django applications in a built out Django project.
First, I've got the same project not built out and everything works fine. This project follows the standard Django project architecture apart from putting my tests in their own directory:
django_project/
manage.py
settings.py
urls.py
app1/
models.py
views.py
urls.py
tests/
app2/
...
If I run the tests in this situation great!
This is all a little bit different when it comes to buildout environment. There I've tried to svn check my project and applications and make the paths to all of them available in my bin directory (and I hope that also means making it available for whatever magic djangorecipe is doing). Anyway this is what my buildout looks like:
[buildout]
parts =
django_project
app1
app2
django
extra-paths =
${buildout:directory}
${buildout:directory}/parts
${buildout:directory}/parts/django_project
${buildout:directory}/parts/app1
${buildout:directory}/parts/app2
[django_project]
recipe = infrae.subversion
urls =
https://svn/django_projects/trunk/ .
[app1]
recipe = infrae.subversion
urls
= https://svn/path/app1/trunk/ .
[app2]
recipe = infrae.subversion
urls =
https://svn/path/app2/trunk/ .
[django]
recipe = djangorecipe
version = 1.2
project = django_project
projectegg = django_project
wsgi=true
settings=settings
extra-paths = ${buildout:extra-paths}
test =
app1
app2
When I run the buildout I get the following directory tree.
django_buidout/
...
bin/
django
django.wsgi
test
parts/
django/
django_project/
__init__.py
settings.py
...
app1/
setup.py
app1/
__init__.py
tests/
app2/
setup.py
app2/
__init__.py
tests/
I can get to the django shell so that works. But if I run ./bin/test I get an 'ImportError: No module named django.project.urls'. (the dot notation is not a mistake my django_project.settings.URL_CONF=django_project.urls) This is interesting because if I start the shell I can import django_project.urls. In addition to that if I run the tests through the ./bin/django test app1 all the test run. app1 is interesting because it has no tests on views.
In the views tests I'm using urlresolvers.reverse and that also shows up in the traceback along with the django.tests Client() class.
Is there something in my architecture that is messed up, or is urlresolvers.reverse doing something I'm not aware of?
Many thanks,
Todd

Related

Where should i put django application folder

I have followed several django tutorials on the web.
There is 2 folders: One for django project and another for application. We can have multiple applications for the same project but i my case, i have just one.
Sometimes, the application folder is located at the same level than project folder and sometimes, the application folder is located inside project folder.
I do not know what is the best solution... It works in both case. It is just a state of the art question
Thanks
Mostly it is a matter of choice and the organization of your Project. Even thought, i will post you here a recomented layout and good reasons to choose this
myproject/
manage.py
myproject/
__init__.py
urls.py
wsgi.py
settings/
__init__.py
base.py
dev.py
prod.py
blog/
__init__.py
models.py
managers.py
views.py
urls.py
templates/
blog/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_managers.py
test_views.py
users/
__init__.py
models.py
views.py
urls.py
templates/
users/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_views.py
static/
css/
…
js/
…
templates/
base.html
index.html
requirements/
base.txt
dev.txt
test.txt
prod.txt
Allows you to pick up, repackage, and reuse individual Django applications for use in other projects
Environment specific settings. This allows to easily see which settings are shared and what is overridden on a per environment basis.
Environment specific PIP requirements
Environment specific PIP requirements
Small more specific test files which are easier to read and understand.

Django makemigrations not detecting project/apps/myapp

Solved
Interesting:
./manage.py makemigrations apps.myapp -> "App 'apps.myapp' could
not be found. Is it in INSTALLED_APPS?"
./manage.py makemigrations myapp -> Works.
./manage.py makemigrations -> "No changes detected" (it does detect changes if myapp is in the project root)
Original question:
Project structure
myproject/
myproject/
apps/
myapp/
__init__.py
admin.py
models.py
urls.py
views.py
tests.py
myapp2/
myapp3/
__init__.py
static/
templates/
virtualenv/
manage.py
myproject/apps/myapp/models.py
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
Settings.py
INSTALLED_APPS = [
# ...
'apps.myapp',
'apps.myapp2',
'apps.myapp3',
]
makemigrations cannot find "myapps" that's not in the project root.
At the same time, migrate does find it.
$ ./manage.py makemigrations apps.myapp
App 'apps.myapp' could not be found. Is it in INSTALLED_APPS?
$ ./manage.py migrate apps.myapp
CommandError: App 'apps.myapp' does not have migrations.
Isn't the "let's put our apps into an apps folder" practise valid any more, am I doing something wrong, or is it a bug of the makemigrations command?
Note1: before running makemigrations, I removed the "migrations" folder of myapp, but I'm pretty sure it doesn't matter. It did get re-created when the app was in the project root.
Note2: I did research google and stackoverflow, I only found similar questions where the solution was either "adding the app to settings.py", "running makemigrations before migrate" or "some migration issue between Django 1.6 and 1.7". None of these apply to my situation, I think.
You may have deleted the migrations folder inside the app or __init__.py inside the <app>/migrations/ folder, create a new one
myproject/
apps/
myapp/
migrations/
__init__.py
You can always do makemigrations seperately without doing the above step
python manage.py makemigrations myapp
try
INSTALLED_APPS = [
# ...
'myproject.apps.myapp',
]
and
python manage.py makemigrations myapp
It's works now. It didn't work before because I didn't make any changes to my model.
I'm new to django and was trying to execute the command from the videos I watched. Django documentation tells you that makemigrations create new migrations based on the changes you made to your model. So, I make changes in models.py and admin.py and it's working now.
I had the same problem. I solved it by adding "migrations" file into the app folders.

django cannot import name patterns on app engine as app is working fine on heroku but not working on app engine

I am learning django and i have deployed it on heroku it is working fine here it is the link http://whispering-bayou-9769.herokuapp.com/ but on app engine it is not working giving me error
500 please try again in few time
it is the url for app engine http://djangocomment.appspot.com/
Please help me i think it may be problem of app.yml because on app engine logs there is log telling me error
'exceptions.ImportError'>: cannot import name pattern i have check url.py it is fine already working on heroku
in app.yml i have wriiten
application: djangocomment
runtime: python
version: 1
api_version: 1
handlers:
- url: .*
script: /post/urls.py
env_variables:
DJANGO_SETTINGS_MODULE: 'post.settings'
urls.p[y in post folder contain the path
file structure
app/
build/
categopry/
admin.py
templates/
models.py
urls.py
tests.py
post/
urls.py
forms.py
settings.py
wsgi.py
static/
admin.py
templates/
models.py
urls.py
tests.py
template/
registration/
userprofile/
admin.py
templates/
models.py
urls.py
tests.py
app.yml
manage.py
procfile
README.md
requiremtn.txt
That really isn't how you run a Django app on GAE. What gave you the idea to point the script to urls.py?
For a start, you need to reference Django in the libraries setting in your app.yaml. Then, you need to set the script to the WSGI object within your project's wsgi.py file: perhaps something like post.wsgi.app. See the documentation.

ImportError After Moving App to Nested Folder

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.
Before my project structure was as follows:
camucamu
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:
camucamu
apps
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
I then changed the imports in views.py and admin.py
from books.models to apps.books.models.
I also changed INSTALLED_APPS in settings.py from books to apps.books.
When I then tried to run syncdb, I get the following error:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books
What am I messing up here so it can't find my app anymore?
Your apps folder does not have an __init__.py file so it cannot be recognized as a python module
I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:
Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
Update the import statements wherever you refer to an external app:
from projectname.apps.appname.models import YourModel, YourOtherModel
Inside settings.py edit INSTALLED_APPS such that it looks like this:
INSTALLED_APPS = (
...
# apps
'projectname.apps.appname1',
'projectname.apps.appname2',
)
This one is not specified in the guide: In all your urls.py files, update the urlpatterns!
BEFORE:
# client views
urlpatterns += patterns('appname',
...
)
AFTER:
# client views
urlpatterns += patterns('projectname.apps.appname',
...
)
Finally remember to update your changes by calling python manage.py syncdb
Hope that helped.

No module named urls.py

I've been following the django tutorial over at https://docs.djangoproject.com/en/dev/intro/tutorial03/ but I've hit a brick wall.
yes I have searched google and looked up the 15 or so exact questions and nothing is working.
My directory structure is:
djangotest/
urls.py
settings.py
My settings has this
ROOT_URLCONF = 'urls.py'
The generator generated urls.py, not djangotest.urls as detailed in the tutorial. However, I don't think the naming matters too much. I've tried every variation of this urls file and I am having errors spat at me every time. The main one being the above.
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
url(r'^reviews/$', 'djangotest.review.urls')
)
I'd love some help with this like how I can debug?
Are you being careful about the projectname and appname? Are you following the right tutorial for the version of django you downloaded? I'd also recommend using the latest stable release http://www.djangoproject.com/download/1.3.1/tarball/ rather than the latest dev release.
I'd recommend starting over; because without posting all your files (or steps you did), we aren't going to be able to debug where you went wrong.
Also, it seems like just running:
django-admin.py startproject my_test_project
cd my_test_project
django-admin.py startapp my_test_app
that you should have a directory structure like:
my_test_project/
|-- __init__.py
|-- manage.py
|-- my_test_app
| |-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- settings.py
`-- urls.py
settings.py should have ROOT_URLCONF = 'my_test_project.urls', which reads the data in urls.py with no errors if you try
./manage runserver
and point your web browser to http://127.0.0.1:8000
On your update, it seems your specific mistake seems to be that your urls.py references 'djangotest.review.urls' Is there an app (in the installed apps) named review in the project djangotest with a urls.py? Are all the permissions correct?
Well, you need to have a file named "__init__.py" in each directory where you have .py files. These indicate to python that the directory is a python package. Also, verify the ownership and permissions on the files/folder to make sure you can access them as the user running the server.