integrate an external app in django taking the code from any folder - python

I want to install the external app django-disqus in my blog. However, instead of install the module in the system via pip install or python setup.py install, I would like to download the code to a specific folder called libs and then link it to my project.
My folder structure is like this:
-root_folder
-- project (here I have settings.py, urls.py and wsgi.py)
-- blog (here I have models.py, admin.py, urls.py, templatetags/, template/)
-- libs (here I want to add the code of disqus)
If I downloaded the code in libs, how can I link it to INSTALLED_APPS in setting.py?
Note: I run django 1.8

You should be able to register it the same way you'd register any django app.
Make libs a package by adding __init__.py file
Then add it to your settings.INSTALLED_APPS
INSTALLED_APPS = (
...
'libs.disqus',
)

Related

What files/directories belong in a django repo?

So I have been working with a Django tutorial on a Windows Machine and now I'm trying to push that code onto Github. This is what my upper level directories look like:
Envs/
myproject/
Include/
...
Lib/
...
Scripts/
...
tcl/
...
pip-selfcheck.json
mysite/
polls/
...
mysite/
...
db.sqlite3
manage.py
What directories should I be adding to the repo so that I could pull the repo from another Django-installed machine and be able to run the code? Which directory should be the root for my repo?
Github contains a set of gitignore files at https://github.com/github/gitignore. Have a look at the python one which includes django stuff https://github.com/github/gitignore/blob/master/Python.gitignore#L53
also, about gitignore: https://git-scm.com/docs/gitignore
everything that is inside mysite/
virtualenv things dont belong to github.
The answer is not really Django (nor even Python) specific - nor specific to git or github FWIW. The rule is: your source files and assets (icons, images, fixtures, requirements files, installation scripts etc) belong to the repo. Everything that is either installed / compiled / generated by your installation scripts or is "user content" (databases, user uploaded/user generated files etc) should stay out of the repo and actually out of your project's root.
For a more Django specific answer, your virtualenv, database (if using sqlite or any other file-based db), MEDIA_ROOT and STATIC_ROOT (the first storing user generated content and the second collected project's and apps static assets) should be left out of both your repo and your project's root.

how to import django project from github to local system?

I am new to Django.
I am trying to run a project, that downloaded from github .
I run the setup.py file . that downloaded all requirements .
after that i am stuck because there is no any manage.py file as i read in tutorial ( http://www.tangowithdjango.com/book17/chapters/setup.html) .
That is not a project. It's an app that you need to install in your own project.
you have to install it
pip install django-mongo-auth
docs are here
You should then add mongo_auth and dependency django_browserid to INSTALLED_APPS. Add django_browserid.context_processors.browserid_form to TEMPLATE_CONTEXT_PROCESSORS to conclude django_browserid installation. Add mongo_auth.middleware.LazyUserMiddleware just after django.contrib.auth.middleware.AuthenticationMiddleware in MIDDLEWARE_CLASSES.
Optionally, to use provided templates, you can add mongo_auth.contrib and sekizai to INSTALLED_APPS, and mongo_auth.contrib.context_processors.mongo_auth and sekizai.context_processors.sekizai to TEMPLATE_CONTEXT_PROCESSORS, too.
Afterwards, you configure authentication providers you want to offer:
AUTHENTICATION_BACKENDS = (
'mongo_auth.backends.MongoEngineBackend',
'mongo_auth.backends.FacebookBackend',
'mongo_auth.backends.TwitterBackend',
'mongo_auth.backends.FoursquareBackend',
'mongo_auth.backends.GoogleBackend',
'mongo_auth.backends.BrowserIDBackend',
'mongo_auth.backends.LazyUserBackend',
)
Hope it helps.

'app' level imports with django 1.4?

I have a django app that I developed in the 1.2 days. I'm now trying to port it over to the 1.4 project format.
The old way my project was set up was as follows:
django_project/
settings.py
manage.py
urls.py
app1/
app2/
app3/
I'm changing it to use the new manage.py and my directories look like this:
django_project/
manage.py
project
urls.py
wsgi.py
app1/
app2/
app3/
The problem is that all over my code I import stuff like this:
from app1.models import SomeModel
which now gives me an import error. Doing this fixes it:
from project.app1.models import SomeModel
I really don't want to have to go all through my project to change all those imports. Is there something I'm missing? Is there an easier way? Or is this how you're supposed to do it?
You should not put your apps into the project module. Django's startapp puts them in the project root, as it was before. project module is a place for project-wide settings, urls and such stuff only. Your apps should stay outside, in project root.
You can keep your current layout, since it will works fine. For new projects I think you can start to put your apps inside the 'project' module. If you check the 1.4 Release Notes you'll see it's the recommended layout. But if you are developing generics apps (that you can use in more than one project) probably the better place is project root.

Install Django Application so It's Available on Python Path

I have a django application structured like this...
app_foo
__init__.py
urls.py
views.py
models.py
bar_app
__init__.py
...
bar_app...
By using distutils, I can get the application to install into the python path under the "app_foo" module name.
However, any of the code inside of the "bar_app" python files which refers to things inside the django app relatively does not work when executed from the python path. For example,
from bar_app.views import stuff
I know that I can go through the app and change all the references to be absolute. For example,
from app_foo.bar_app.views import stuff
My question:
Is there anyway I can get all of the apps inside "app_foo" to also be on the python path?
Conceptually this would be similar to saying from app_foo import * for the entire path.
You can do
from .bar_app.views import stuff
http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports

How do I setup and alternate directory structure for django?

I'm starting on my first large django project and have realized that the default directory structure isn't going to work well.
I saw this question and answer and decided to implement something similar.
Large Django application layout
What do I need to do to make something like this work? How can I change where django looks for apps/modules?
Python works automatically with deep directory structures. That's porbably you didn't find any instructions on how to do it.Here are some instructions on how to get classes and models to work.
If you want to have a module in folder yourproject/apps/firstapp you can just add it to INSTALLED_APPS by adding line 'apps.firstapp',. You will have to add a __init__.py file to each of the directories so that they are recognized as python packages.
When you import classes you can simply use from yourproject.apps.firstapp.filename import yourclass.
You should also make sure all template directories are listed in TEMPLATE_DIRS.
I have two methods to handle this; one for production and one for development.
In development:
Append the path to your apps in your settings.py. In my case, I have my main project in ~/hg/django/project/ and my apps are in ~/hg/django/apps/. So I use:
if DEVEL:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apps'))
I also use the production method on my development box. This method has a knock on effect of letting me roll back to the most recent production version by simply commenting out the line path insertion in the settings.py.
In production:
I install the apps using distutils so that I can control deployment per server, and multiple projects running on each server can all access them. You can read about the distutils setup scripts here. Then, to install, you simply:
./setup.py install
if you add:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
to your settings .py and the following to your manage.py:
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
sys.path.insert(0, join(settings.PROJECT_ROOT, "lib"))
then you can include them in your installed apps just as you would if they were in your project root:
INSTALLED_APPS = (
#local apps
'myapp',
#local libs
'piston_dev',
)
this allows you a bit more freedom to move apps around without having to redeclare imports and such.

Categories