I am relatively new to Django, and I must admin I'm getting confused (not to mention frustrated) with its frankly, bizarre folder structure - and the differentiation between website, projects, apps and modules.
I am trying to install and use django-realestate into my virtenv (using virtualenvwrapper)
After I created the new virtenv, I installed the Django app in the new environment. The problem is that the entire codebase is actually created under ~./virtualenvs/myenv
The problem is that I want to be able to modify and (quite extensively) extend the code to suit my own purposes. However, I can't do that - if the code is under the "control" of virtualenv. My gut instinct is to do either of the following:
Move the code from the src folder under ~./vitualenvs/myenv to /path/to/proj
Create a brand new Django install and add(/merge?) the folders realestate, testproject and test to my project folder?
It is a hideously complicated setup for what is a straightforward requirement: I want to have the latest Django version, and also have the code for the latest real-estate app (which I will modify extensively).
How do I solve this problem?
Related
Django newbie here.
I have an existing Django virtual environment that was setup as part of a tutorial,
using Django 2.2. and Vagrant + Virtual Box on my laptop.
There was 1 project created for the tutorial above.
I will now be starting another Django tutorial,
wherein I will be creating a few more projects.
WHat is the best way to go about this?
Should I create a new virtual environment for tutorial #2?
Or use the existing environment, that was setup for tutorial #1?
FYI - tutorial #2 uses Django 1.11
Thanks a lot!
It is always a good practice to create different virtual env for each django project. For example if you have multiple django projects that are using one virtualenv, and you want to host one of the django apps on a platform like Heroku which will require you create a requirements.txt file for python apps, so when you run pip freeze to get the requirements, you will find out that there are many packages in that env that is not required by your current project. And installing all those packages on your Heroku might make you run out of space before you know it. So try and keep the virtualenv different according to your project and keep the requirement.txt as well.
You have multiple options for package managers:
Pipenv
Poetry
Virtualenv
In general, Pipenv or Poetry are easier to use vs Virtualenv.
If you are still stuck, try using the Imagine smart compiler that handles your package installation as well as allow you to generate the code for your application logic.
Just do
npx imagine create -f Django -n myapp
make install && make run
I have to find a solution for sharing code between two big Django projects. The main things to share are models and serializers and template tags. I've came up with 3 different solutions and I need you to find pro and cons to be able to make a choice.
I'll list you the solutions I found:
git submodules
Create a repository where to store my *.py files and include them as a django app such as 'common_deps'
Even if this is the purpose of git submodules there are a bit hard to use and its easy to fall into traps.
python package
Create a python package to store my *.py files.
It seems to be the best option to me event if that means that I'll need to change my requirements.txt file on my projects on each new release.
Simple git repository
Create a new repository to store my *.py files and include them as a django app such as 'common_deps'. Then add it to my PYTHON_PATH
I need some advices, I haven't chosen yet. I'm just telling myself that git submodules seems to be a bas idea.
Tell me guys.
I will definitely go with the second option you listed - packaging your app. If you follow steps in the Packaging your app part of official Django tutorial, you'll get tar.gz file which will allow you to include your app in any project you want by simply installing (e.g. with pip) to the virtual env connected with the project or globally
I will go with python package, after all this is what it is for.
Another newbie to django here. I was wondering if it is recommended/not-recommended to run two different projects in the same virtualenv folder that have the same django version. To be more clear, is it necessary to create separate virtualenv everytime I want to start a new project when i know that i am using same django version for all projects. I am using python django on OSX.
It really depends on the situation. Suppose if your Project A need to use Pip version 17.01 to run while your project B need to use Pip version 18.01 to run. So It is not possible to use 1 virtual env to run multiple project but the downside of having multiple virtual environments is it consume much space & resource of PC.
I'm really interested in trying out the Pyramid Framework but only half of my programming time is spent on my computer here at home; I spend a considerable amount of time at school and use their computers as well, and according to the documentation for virtualenv, environments can't be moved around like projects. What I'm thinking of doing is installing a different virtualenv for Pyramid projects on the computers I use at school in addition to one at home. Would I be able to do this?
In your setup.py file at the root of your project, you just have to list all the dependencies you have in requires.
Then, with your virtualenv activated, you run
python setup.py develop
This will install missing dependencies on your current virtualenv.
Example:
requires = ['pyramid',
'WebError',
'pymongo',
'mock',
'formencode']
Use something like Dropbox to keep your source file in sync between machines.
Use pip in with virtualenv, keep a requirements.txt file that lists all the dependencies for your software.
this isn't really specific to pyramid. you need to look into version control. get a free account on github or bitbucket and push/pull your code from there.
After looking at the reusable apps chapter of Practical Django Projects and listening to the DjangoCon (Pycon?) lecture, there seems to be an emphasis on making your apps pluggable by installing them into the Python path, namely site-packages.
What I don't understand is what happens when the version of one of those installed apps changes. If I update one of the apps that's installed to site-packages, then won't that break all my current projects that use it? I never noticed anything in settings.py that let's you specify the version of the app you're importing.
I think in Ruby/Rails, they're able to freeze gems for this sort of situation. But what are we supposed to do in Python/Django?
Having multiple versions of the same package gets messy (setuptools can do it, though).
I've found it cleaner to put each project in its own virtualenv. We use virtualevwrapper to manage the virtualenvs easily, and the --no-site-packages option to make every project really self-contained and portable across machines.
This is the recommended setup for mod_wsgi servers.
You definitely don't want to put your Django apps into site-packages if you have more than one Django site.
The best way, as Ken Arnold answered, is to use Ian Bicking's virtualenv (Virtual Python Environment Builder). This is especially true if you have to run multiple versions of Django.
However, if you can run a single version of Python and Django then it might be a little easier to just install the apps into your project directory. This way if an external app gets updated you can upgrade each of your projects one at a time as you see fit. This is the structure Pinax used for external Django apps at one time, but I think it's using virtualenv + pip (instead of setuptools/distutils) now.
What we do.
We put only "3rd-party" stuff in site-packages. Django, XLRD, PIL, etc.
We keep our overall project structured as a collection of packages and Django projects. Each project is a portion of the overall site. We have two separate behaviors for port 80 and port 443 (SSL).
OverallProject/
aPackage/
anotherPackage/
djangoProject80/
settings.py
logging.ini
app_a_1/
models.py # app a, version 1 schema
app_a_2/
models.py # app a, version 2 schema
app_b_2/
models.py
app_c_1/
models.py
djangoProject443/
test/
tool/
We use a version number as part of the app name. This is the major version number, and is tied to the schema, since "uses-the-same-schema" is one definition of major release compatibility.
You have to migrated the data and prove that things work in the new version. Then you can delete the old version and remove the schema from the database. Migrating the data is challenging because you can't run both apps side-by-side.
Most applications have just one current version installed.