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.
Related
I have a rule to ignore secret.py (production.py in my case) and once I added .gcloudignore, github stopped following that rule...
Is there some sort of a rule overriding between gitignore and gcloudignore that I am not aware of?
my-project/
.git
.gitignore
my-project/
.gcloudignore
settings/
base.py
local.py
production.py
my .gitignore:
my-project/my-project/settings/production.py
my-project/my-project/settings/local.py
my .gcloudignore:
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
*.sqlite3
settings/local.py
End result is that the 'local.py' is NOT pushed to google cloud NOR github.
However, 'production.py' IS pushed to github AND gcloud.
If you had previously (perhaps accidentally) submitted a change to git that included my-project/my-project/settings/production.py, then it will remain a part of the repository even if it is subsequently added to .gitignore.
Assuming you are at the root of your project, you can use
$ git log my-project/my-project/settings/production.py
to see its git history. If it is present in your repo, you can do
$ git rm --cached my-project/my-project/settings/production.py
to remove it from the repo, but keep it in your local (working) environment.
Looks like you are enforcing to ignore the rules applied before.
Though make sure that the cloudignore is enable via
gcloud config set gcloudignore/enabled true
And make sure to place .gcloudignore - > in the root of your project / basically where you .git .gitignore reside.
I've pulled my django repo from bitbucket onto my digital ocean server. My project folder name in my server is project whereas my initial app from my repo (the one with settings.py is called app. Does this cause any problems? Because I know when you create a django project offline, the first directory is always the same as the parent directory.
whereas my initial app from my repo (the one with settings.py
That's not an application, that's your project's configuration.
And no, having the same name for your project's root directory and it's configuration directory shouldn't cause any problem.
That's even how it's designed to be so it's perfectly normal.
Since the wsgi.py (which should be what you use for production) dynamically handles path as long as the internal structure of your project is preserved (don't rename the project's configuration directory).
The absolute path to your project isn't hardcoded, it's retrieved by getting parent directories of your configuration directory. You can move your project and even rename its root directory.
Here is how it's handled :
wsgi.py :
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
wsgi.py sets the right value to use the right settings.py file.
settings.py :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
settings.py sets BASE_DIR dynamically. This value is then what's used for handling pathes to avoid having an hardcoded absolute path that wouldn't work as soon as you move your project.
There won't be any issue with having the different name of parent directory and the project directory (the one with settings.py).
You can try renaming the parent directory and it would still work the same way but the name of project directory should not be touched since the same is linked with project settings which are used to run the project.
I distribute a small django app that I wanted to write a test for. It uses some settings and I was importing
from django.conf import settings
in the app file but this leaves me with a problem because the standalone app has no django project so how would one write and run tests on it?
In your app folder, create a folder named tests, and in that folder place your tests (in a single .py file or multiple files.
You will end up with a folder structure like this:
my_app/
tests/
test_views.py
test_other_stuff.py
Then you will be able to run your test suite for that app using:
`./manage.py test my_app`
Where my_app is the name of your app. As far as how to write the tests themselves, the Django docs are very helpful.
My solution was to create a django project with the name of the app and then put all the distribution files such as
setup.py
MANIFEST.in
in the project level directory and then indicate only the files I want to include with the distribution in the MANIFEST.in file.
MAINFEST.in:
include myapp/*
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',
)
I plan to organize my python project the following way:
<my_project>/
webapp/
mymodulea.py
mymoduleb.py
mymodulec.py
mylargemodule/
__init.py__
mysubmodule1.py
mysubmodule2.py
backend/
mybackend1.py
mybackend2.py
lib/
python_external_lib1.py
python_external_large_lib2/
__init__.py
blabla.py
python_external_lib2.py
in my development IDE (PYdev) to have all working I have setup webapp/, backend/ and lib/ as source folders and all of course works.
How can I deploy it on a remote server? Have I to set PYTHONPATH in a startupscript ?Or have I to it programmatively?
If you are treating webapp, backend, and lib as source folders, then you are importing (for example) mymodulea, mybackend1, and python_external_large_lib2.
Then on the server, you must put webapp, backend, and lib into your python path. Doing it in some kind of startup script is the usual way to do it. Doing it programmatically is complicated because now your code needs to know what environment it's running in to configure the path correctly.