In the new Django 1.4 project layout, I have a declaration of os.environ.setdefault on manage.py and wsgi.py inside the project folder. What is the difference between the two?
Also, if I have this settings structure:
mysite
|-- mysite
| |-- settings
| |-- base.py
| |-- dev.py
| |-- production.py
| wsgi.py
|-- myapp
|-- manage.py
which os.environ.setdefault should I edit? The one in manage.py or the one in wsgi.py?
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
also, in which file should I detect the current env? and how do I do so?
So manage.py is never executed once deployed, its mainly for development and/or executing commands against your project so its the wsgi.py that you're interested in.
You can check this by looking at the file itself which has: if __name__ == "__main__": that should tell you that the file is meant to be executed directly from command line.
Related
This is my directory structure:
~
|--- scratchpad
|--- manage.py
|--- my_project
|--- __init__.py
|--- settings.py
|--- urls.py
|--- wsgi.py
|--- my_app
|--- __init__.py
|--- admin.py
|--- apps.py
|--- models.py
|--- tests.py
|--- views.py
|--- migrations
|--- __init__.py
The contents of tests.py are simple enough:
from django.test import TestCase
# Create your tests here.
class TrivialTestCase(TestCase):
def test_trivial(self):
self.assertTrue(True)
In this setting I can run my tests without problems:
m#mycomp:~/scratchpad$ python3 manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
BUT what if the tests.py file gets so large I'd like to split it into many smaller files AND move them all to a separate folder to keep the app directory clean?
~
|--- scratchpad
|--- manage.py
|--- my_project
|--- __init__.py
|--- settings.py
|--- urls.py
|--- wsgi.py
|--- my_app
|--- __init__.py
|--- admin.py
|--- apps.py
|--- models.py
|--- tests
|--- tests_trivial.py
|--- views.py
|--- migrations
|--- __init__.py
Now manage.py cannot find my tests!
m#mycomp:~/scratchpad$ python3 manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
I am surprised because this behavior seems to contradict certain highly upvoted answers here, at least if I'm reading these answers right.
Now I can run my tests if I modify my command a little:
m#m-X555LJ:~/scratchpad$ python3 manage.py test my_project.my_app.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
However typing all of this each time I want to run tests is a PITA.
Is there any way to make manage.py discover tests in the tests subfolder without having to type my_project.my_app.tests all the time?
Every django-app test-folder needs the following structure to get the tests running properly::
|__django-app
|__tests
|-- __init__.py
|-- test_models.py
|-- test_views.py
Important: Fill in an empty __init__.py File in every test-directory.
So, in your case it should be:
|--- scratchpad
|--- manage.py
|--- my_project
|--- __init__.py
|--- settings.py
|--- urls.py
|--- wsgi.py
|--- my_app
|--- __init__.py
|--- admin.py
|--- apps.py
|--- models.py
|--- tests
|--- __init__.py
|--- tests_trivial.py
|--- views.py
|--- migrations
|--- __init__.py
I highly recommend this tutorial William Vincent for beginning with testing with Django Django Testing Tutorial
I'm attempting to create a Django templates directory via django-admin startproject and --template.
I've tried: django-admin startproject main_project --template=./templates
However it only creates an empty main_project directory there is nothing reflecting a Django instance in this directory (ie. no urls.py, settings.py, wsgi.py, etc.)
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ templates/
I'm expecting a project Python package to be created as described here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/#creating-a-project
mysite/
manage.py
mysite/ # or main_project for my example
__init__.py
settings.py
urls.py
wsgi.py
I'm only getting:
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ main_project
|_ #empty directory
|_ templates/
|_ venv
As per the docs https://docs.djangoproject.com/en/2.2/ref/django-admin/#cmdoption-startapp-template
Perhaps try the command with the —template flag before the project name
I have my app runnning in Heroku, everything works really good with my models and forms, but there is a problem, I can't see any of my styles neither for my templates not for Django Grappelli, how can I solve this problem?
Thank you.
Check the path that your images/styles are trying to reference. Ensure that your STATIC_URL is a relative path. Also ensure that your STATIC_ROOT and STATIC_URL are not the same.
Ex:
settings/base.py
from unipath import Path
# Project directory root assuming: yourapp.settings.base
PROJECT_DIR = Path(__file__).ancestor(3)
# Static files
STATIC_ROOT = PROJECT_DIR.child("static")
# URL prefix for static files.
STATIC_URL = '/static/'
This layout follows the directory structure similar to:
project_name/
|-- app 1
|-- models.py
|-- views.py
...
|-- project_name
|-- settings
|-- base.py
|-- local.py
|-- dev.py
...
Also by default Heroku should collectstatic when you upload your project however if you have modified this setting ensure to call:
python manage.py collectstatic
you can then check to see if static files are present in the specified folder (in the example above it would be in /static/
I've just started a new project in django 1.4 and since they've changed the default layout for manage.py and the whole folder hierarchy (see https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py) i cannot decide where should i put my app packages - inside mysite or outside it? What's the best practice? For some reason, startapp command creates the app outside of the mysite package, but this feels somehow wrong.
So, what's the best? This:
manage.py
mysite/
__init__.py
settings.py
urls.py
myapp/
__init__.py
models.py
or this:
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
?
The second way.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
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.