Django application installation - django

I'm still busy with my Django learning adventure. In another post I asked about how to structure a Django project and applications using buildout. In the details of doing this arose another issue, simply installing 3rd party Django applications using either easy_install or setup.py. My question is, where should you install a Django application? If looking at Django documentation, one would think to put a Django application inside the project folder. But if your Django application is an egg (a mystifying term in my opinion) and you use easy_install without option '-b' (build-directory) the application will be installed into your current python site-packages directory. Using option '-b' will put a copy of the application in your directory, but still will install it in your current site-packages directory. Then there are other options like --install-dir and prefix. Also how should installation happen when using setup.py which have similar options as buid-directory, install-dir, and prefix?
Is there a 'good practice' standard for installing 3rd party Django applications into a Django project?
Thank a lot,
Todd

They usually aren't installed directly into the project. They're either installed into the system's site-packages/ directory, or in the virtualenv's site-packages/ directory, or in some other well-defined place that the sysadmin has set for this purpose.

This is where virtualenv comes into its own. It basically enables a project-specific site_packages directory, where you can install all the third-party applications that relate to your project. I'd definitely recommend it.

Follow these steps :
change the path according to your local setup
C:\Python27\Lib\site-packages>python pip install django
Create Project
Go to folder where you want to create a project
E:\djangoProject>C:\Python27\Lib\site-packages\django\bin\django-admin.py startproject myproject
python manage.py help is used to list all the commands
Manage.py  This file is kind of your project local django-admin for interacting with your project via command line (start the development server, sync db...)
Run Server
E:\djangoProject\myproject>python manage.py runserver
Create App
E:\djangoProject\myproject>python manage.py startapp myapp
Go to myproject  settings.py and register your app “myapp” created under INSTALLED_APPS
Migrate DB  E:\djangoProject\myproject>python manage.py migrate
Migrate will create necessary tables or collections depending on your db type, necessary for the admin interface to run

Related

In the netbox plugin development tutorial, where is manage.py being run from?

I am following the tutorial on plugin development at https://netbox.readthedocs.io/en/stable/plugins/development/
I have gone as far as creating the models and want to make migrations...
However, the manage.py is not found in the root of my plugin folder.
Where is it expected that manage.py is?
The manage.pyis part of the Django application (= website). It is located in the Django root folder, see e.g. Django tutorial.
So, a plugin never has got its own manage.py, but it may have got a set of migration files that are used by the Django app when python manage.py migrate is invoked and the plugin has been installed and defined as being a part of the Django app (within settings.py).
Assuming you've followed the installation instructions and installed Netbox in /opt/netbox, the manage.py file you need to use is located in /opt/netbox/netbox/ folder.
Don't forget to activate the virtual environment in /opt/netbox/venv and to set DEVELOP to True in /opt/netbox/netbox/netbox/configuration.py

How to start an existing Django project?

I am a new to Django and have 0 experience with this framework.
I cloned a project from git it came with requirements.txt file, but I am not sure how to run it.
Do I need to create virtual environment first and clone the project into the directory of the virtual environment, and then install the requirements?
Do I need to clone the project first into some folder and then create the virtual environment inside this folder and then install the requirements?
Do I need to use any special IDE to run the project? I tried opening the project in PyCharm, without creating a virtual environment first, and it asked me if I want to install the requirements.
I would be glad if someone could explain what is the correct way to run an already existing project.
Let's come to your doubts one by one :
Cloning a repo has nothing to do with Django. You are just making a copy of the code on your disk. Now the copy can be kept anywhere you like(say in ~/Desktop). Also, the directory of a virtual envt. just contains the code of modules you might directly import(like Django), and has nothing to do with the code of the django project. So cloning can be done before or after activating the virtual envt.
You need not create a virtual envt., but you should. A virtual envt. just ensures that you can have different versions of the same module for your different projects. It keeps things organised. So, for example you can create two different virtual envt.'s one with Django=2.0 and another with Django=1.9, to test your website for the two different Django versions.
requirements.txt contains all the modules you will be needing to run the django application. So you first create a virtual envt, activate it and then in the virtual envt., install all the modules you will need. Generally do pip install -r requirements.txt
Now all the required modules are installed, To run the website on a local server( which Django will create for you ), do python manage.py runserver and open in the browser.
No, you don't need an IDE to run the django project. Editing the code on any text editor and running the server from terminal works just fine.
P.S: If you are completely new to python, I would recommend using the conda python distribution. You can create new virtual envt. using conda create as well.
1.Grab a copy of the project.
git clone new_project.git
2.Create a virtual environment and install dependencies.
mkvirtualenv new_project
pip install -r requirements.txt
3.Duplicate new_project/new_project/local_settings_example.py and save as local_settings.py.
4.Enter your database settings in local_settings.py.
5.Initialize your database.
python ./manage.py syncdb
python ./manage.py migrate
6.If your app has a custom user model, you'll need to create a new superuser for the admin.
python ./manage.py createsuperuser
7.Run the development server to verify everything is working.
python ./manage.py runserver

Port DjangoCMS - Project to another machine

I recently made a Website based on DjangoCMS for a project I had to do in school. I also integrated a blog- app which I had made earlier in Django.
When I tried porting the project to another machine though, I get an error message saying "App 'blog/' could not be found. Is it in INSTALLED_APPS?" Also, my whole content is gone.No menus, content, pages, nothing. My DB is SQlite3
I ran the project with makemigrations blog, makemigrations website (that's what the cms-project is called) followed by runserver.
So basically my question would be: What can I do to fix this? Is there any way to make something like an identical copy of my existing project on another machine?
VirtualEnv will be good for you, i was doing the same thing, make your virtualenv relocatable with virtualenv venv_folder --relocatable, then move project folder and virtualenv folder to new server(you can use scp for transfering files over ssh).Just activate virtualenv again and runserver on your new place. >> source venv_folder/bin/activate; python manage.py runserver

Django: how to group apps in directories?

I am using modified sources from few third party apps in my project. I would like to put these third party apps in a separate directory, so that they are not on the same directory level as my own apps. Is this possible in django?
I tried simply putting the apps in a directory thirdparty and changed my INSTALLED_APPS like so:
INSTALLED_APPS = (
'my_app',
...
'thirdparty.django_messages',
This of course failse with:
ImportError: No module named thirdparty
After which I naturally added __init__.py to the directory. But it fails again:
ImportError: No module named django_messages.apps
Just to avoid any confusion, the app django_messages does contain apps.py
Is there a way to group django apps in a directory or do they all have to be in the same project root directory?
Edit
A better alternative is in the accepted answer by Antoine Pinsard
For those persistent on grouping apps see accepted answer here!
Don't do this. If you really need to modify the source code of third-party apps, fork the repositories so that you will be able to watch and merge upstream updates.
Then install the modified apps with pip.
For instance, if you have forked django-autocomplete-light on your github (let's say https://github.com/dsalaj/django-autocomplete-light):
pip install git+ssh://git#github.com/dsalaj/django-autocomplete-light.git
You will be able to upgrade it like any other pip package:
pip install --upgrade git+ssh://git#github.com/dsalaj/django-autocomplete-light.git
And even add it to your requirements.txt.
As Mad Wombat mentioned in the comments, you can use pip's --editable (-e) option to install these packages in a specific folder within your project. From pip help:
-e, --editable Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url.
Nevertheless, to answer the question. The issue is that the app django_messages considers it is a top-level module (and it is supposed to be). Thus it can import its submodules using an absolute python path (starting with django_messages.). However, when you place it within a module thirdparty, django_messages becomes a submodule of thirdparty. You could add the thirdparty directory to your PYTHON_PATH so that django_messages is available as a top-level module. But it is really not advisable to do so. lib/pythonX.Y/site-packages is the best place for your third party packages and this is where pip installs them.
You may also be interested in python virtualenvs if you don't know what they are.

Django: where are packages installed

I am quite a django n00b, but I am reading the eyes out of my head to get it all going. I have a PHP background and struggle with the way and location of reusable apps.
I thought that installed apps should go in an App folder (example django-registration or django-profiles), but after I PIP the app in my virtualenv, I see that the app is installed in a Django folder names "site packages".
Is this the default behavior? Should I copy the 'registration' or 'profile' folder from site packages to my Project? or should I leave them there
Thanks for the help.
If you're intending to simply install packages and not amend their code, there's no problem with them living in Python's site-packages dir.
Because you're using virtualenv, the packages installed while that virtualenv is active will be stored in:
/path/to/virtualenvs/myvirtualenv/lib/python2.x/site-packages/
And it's completely fine for them to stay there. As Daniel R says, what matters is that they are your PYTHONPATH, and virtualenv takes care of making sure they are.
Custom apps you write go in your project. Installed apps you just want to import from into your custom apps can stay in the site-packages folder.
This has nothing to do with Django. This is where Python installs packages. Django doesn't care where they are, as long as they're on the Pythonpath (which they are if they're in site-packages).