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
Related
Having trouble creating a Django repository on Github that everyone can use? I tried pushing the entire virtual environment at first, found out that it doesn't work like that, then I tried just pushing the Django folder, but even the manage.py is looking for a path that only exists on my PC.
How can I push the Django project, so that my group members can pull it, work on it, test it, and be able to push their changes themselves? Do we all have to be using the same virtual environment?
What was missing:
python manage.py migrate w
I tested this a couple times pushing it and pulling it from my PC and laptop.
I'm going to push the Django project onto our repository in a bit so we can start working on stuff. I'm still learning all this so this might not be entirely correct—do correct me if I'm wrong—but this worked for me. (I pushed a Django project onto my git from my laptop, cloned it onto my PC, and ran it.)
Create a folder; call it whatever you want
Install your virtual environment into this folder
I'm using venv, but all VEs should work (for venv, python -m venv .; the dot installs it in the current directory)
When you clone the repository, clone it in this same folder; or, if the repository doesn't exist yet, create it here; don't go into the folder yet
After it's cloned, activate your virtual environment
Once your VE is active, go into our repo folder (DroneWebsite)
Go into the src folder
pip install django==3.2.7
python manage.py migrate
It should be good to go! To test:
python manage.py runserver
I am creating a new Django project through PyCharm, I already have Virtualenv selected instead of conda, so do I have to create a virtual environment through the terminal and install all libraries? I am not planning to upload this site on domain or server, it's for my learning, will upload it on Heroku or similar thing to show my practice and work to people.
You don't have to install it again. Check in your PyCharm settings where the virtual environment has been installed, then all you have to do is:
. path/to/virtualenv/bin/activate
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
Assuming the django project repository is on github and I have had no interaction with it previously.
So: I cd to a new directory on my computer.
I clone the repository.
If the django project is using postgresql, do I have to have postgresql installed on my local machine?
Do I have to be running in a virtual environment to use a specific interpreter?
Thanks Peter
Database
You can actually use another database on your local copy if you choose, although in general it's a good idea to use the same database locally.
If you're going to be using postgres locally, yes you'll need to install it and then create your local database. Once you have your local database setup, you'll need to change some config values of your DATABASES property in settings.
Packages
Your project will also have some dependencies which should be listed in a requirements.txt file at the root directory. If it is not, you'll need to find out which packages need to be installed via pip freeze in the production console.
Virtual Env
You should use a virtual environment, but it's not completely necessary to get your project up and running. Virtualenvs allow you to have different installs and runtimes for different projects.
Other
Every project is different, and there will most likely be some other things that pop up. However, this should get you going in the right direction.
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