django site urls whose are not under site-packages - django

Lets assume you have a django website svn directory which is not under site-packages.
Whenever i run:
mysyper_dir/whatever_module/manage.py runserver 0.0.0.0:8888
and then connect, I realize that my request are still handled by the python files in:
....../site-packages/whatever_module/
while I can see the prints of
mysyper_dir/whatever_module/setting.py
from my server console.
is there a way to tell django that, every "non-framework" files it will ever need are in the "mysyper_dir/whatever_module" directory ?

Neither of these are Django specific (Python actually already handles this) but there's a couple of things you could do. You could set the environment variable PYTHONPATH, or you could add directories to sys.path. You can find more about where modules are located here.
If you are looking to have things just apply to Django, then adding to sys.path might be your best bet. You could try something weird like modifying manage.py and adding command line arguments after the #!/usr/bin/env python but that's uncharted territory for me.

Related

django - How to manually set sys.path?

Till now I was making change on my django production server (yes, really really bad :p ). I am wanna to go to a git process, and creating a local test server before deployement. So, I downloaded my python files, and ran a :
python manage.py runserver
hoping and prayed... but it was not enough, I got a nice error :
django.core.exceptions.ImproperlyConfigured: WSGI application 'issc.issc.wsgi.application' could not be loaded; Error importing module: 'No module named issc.wsgi'
I read in the documentation that [manage.py] is created automatically and sets up several key parts :
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of several things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
It calls django.setup() to initialize various internals of Django.
My question is : how can I manually set up these variables ? Because in my case I downloaded all the files on an arbitrary directory, but it was not enough. Eveything is here, but it is missing the link to this everything....
If you want to manually set the address of your config file, you can set DJANGO_SETTINGS_MODULE with the following:
export DJANGO_SETTINGS_MODULE='settings_to_load'
You can set all your environmental variables this way and it should work. I recommend to use a Virtualenv to this at least.

Deploying multiple projects on Heroku with Django

I would like to deploy two separate Django applications to Heroku. Two applications, with two separate domain names, that are logically different from each other. I set up a venv that contain all the Python/Django stuff. Now, I could create another application that duplicates all the Python/Django stuff in another project. But, is there a way to use the same venv?
My file structure currently looks like this
django
-.git
-projectname_1
-venv
.gitignore
requirements.txt
When I tried to add projectname_2 under django I got an error saying Django app must be in a package subdirectory
Is there a correct way to add a second application using the same venv?
This error occurs when your project doesn't conform to Heroku's specs for a Django project.
Specifically, that particular error occurs when Heroku did not find a settings file at ~/your_app_name/settings.py and therefore assumed it's a non-Django Python app. But then it did find settings.py and manage.py at your project root (~/).
The specific Heroku source code that throws this error is:
https://github.com/heroku/heroku-buildpack-python/blob/master/bin/compile
Your directory should look something like this:
~/.gitignore
~/Procfile
~/requirements.txt
~/your_app_name/
~/your_app_name/manage.py
~/your_app_name/settings.py
~/your_app_name/etc...
Your best bet really is to use two separate Heroku apps. Heroku makes some assumptions about what type of app you are deploying and doesn't necessarily account for multiple apps.
Also, it's probably best to not check in your virtualenv. Just make sure all your dependencies are defined in requirements.txt and Heroku will install them automatically inside a new virtualenv.

Is there a way to add custom django-admin.py commands that work outside of projects?

I'm trying to write a custom command that works outside of Django projects. I was thinking I could follow the coding patterns of Django's own such commands (e.g., startproject), include my command in an app and install it.
Alas, it seems django cannot see this command, as perhaps it doesn't scan site-packages for custom commands.
Is there a way to make this work or am I sadly correct?
UPDATE: I should note that the goal I was trying to accomplish (writing a command that starts projects based on custom templates) is supported in the coming 1.4 release of Django: https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-startproject (see the --template option).
Based on this code from django.core.management, it does appear that django only searches for project-less commands in its own packages, and will then only find command by scanning INSTALLED_APPS, which means a project is required.
You can use a custom manage.py.
You do need a project. A project is, although, nothing more than a python package with a settings.py (and maybe a urls.py file)
So you could just create a project, with whatever commands you want, and in your setup script include a binary script that is nothing more than a manage.py in disguise.
I use it to have a manage.py in the bin path of a virtualenv, but you can call it something else and have that "django" project installed in your system python.
I don't quite understand from your post, for what purpose do You want to write such command using Django's manage.py. But suppose you want (as I was) to run some script, that works with Django models, for example. You cannot run such script without setting Django environment.
I do the following:
put my code in script.py
manage.py shell
execfile('script.py')
Maybe, this helps.

Django App not working: "Error: No module named app_name"

I'm new to Django, and I can't figure out why my app isn't loading. I've created all the files: models.py, views.py, urs.py with default code in. But when I run python manage.py runserver, it says the following:
Error: No module named app_name
What am I doing wrong?
Did you remember to include __init__.py in your folder?
If you did, make sure the permissions on the file are also correct. There's also a similar question here.
Just an additional hint: Instead of manually creating the files you can use django-admin.py startapp APPNAME to automatically create a directory with all necessary files for a new app.
I got your point the init file thing also dosen't work out for me as well. Just ensure that are you writing the proper command inside the proper directory while creating the app, using the terminal.
Like while writing the command "django-admin startapp APP_NAME", ensure that the command is written inside your root configuration directory (which gets created after you typein command "django-admin startproject PROJECT-NAME"), not anywhere else. Then mention the app name inside the settings.py file, under the INSTALLED_APPS[ ] list. Then finally run the command "python manage.py runserver" in the same root configuration directory. I assure you, this will work for sure and the "No modules found" thing will disappear. Have a try and tell me if it don't. Thank you.

Django + Virtualenv: manage.py commands fail with ImportError of project name

This is blowing my mind because it's probably an easy solution, but I can't figure out what could be causing this.
So I have a new dev box and am setting everything up. I installed virtualenv, created a new environment for my project under ~/.virtualenvs/projectname
Then, I cloned my project from github into my projects directory. Nothing fancy here. There are no .pyc files sitting around so it's a clean slate of code.
Then, I activated my virtualenv and installed Django via pip. All looks good so far.
Then, I run python manage.py syncdb within my project dir. This is where I get confused:
ImportError: No module named projectname
So I figured I may have had some references of projectname within my code. So I grep (ack, actually) through my code base and I find nothing of the sorts.
So now I'm at a loss, given this environment why am I getting an ImportError on a module named projectname that isn't referenced anywhere in my code?
I look forward to a solution .. thanks guys!
Is projectname exactly (modulo suffix) the name of the directory the project is in? Wild guess, but I know Django does some things with the current directory…
Also, what is trying to import projectname? Do you get a traceback? If not, try running with py manage.py --traceback syncdb and see what happens.