Skip to content Skip to sidebar Skip to footer

Django Copied Project Folder to New Computer Need to Do Makemigrations Again

Setting up a Django evolution environment

  • Previous
  • Overview: Django
  • Next

At present that you know what Django is for, nosotros'll testify y'all how to fix and test a Django evolution environment on Windows, Linux (Ubuntu), and macOS — whatever mutual operating organization you are using, this article should give you what you need to be able to first developing Django apps.

Django development environs overview

Django makes it very easy to prepare up your ain calculator so that you can start developing web applications. This section explains what you become with the development environment, and provides an overview of some of your setup and configuration options. The remainder of the commodity explains the recommended method of installing the Django development environment on Ubuntu, macOS, and Windows, and how you can exam it.

What is the Django evolution environment?

The development environment is an installation of Django on your local computer that you tin apply for developing and testing Django apps prior to deploying them to a product environment.

The primary tools that Django itself provides are a set up of Python scripts for creating and working with Django projects, along with a simple development webserver that you can use to test local (i.e. on your figurer, not on an external spider web server) Django spider web applications on your computer's spider web browser.

There are other peripheral tools, which form part of the development environment, that we won't exist covering here. These include things like a text editor or IDE for editing code, and a source control management tool like Git for safely managing different versions of your code. Nosotros are assuming that you've already got a text editor installed.

What are the Django setup options?

Django is extremely flexible in terms of how and where information technology can be installed and configured. Django can exist:

  • Installed on different operating systems.
  • Installed from source, from the Python Package Alphabetize (PyPi) and in many cases from the host reckoner's package managing director application.
  • Configured to use i of several databases, which may also need to be separately installed and configured.
  • Run in the main system Python environment or within separate Python virtual environments.

Each of these options requires a slightly different configuration and setup. The following subsections explain some of your choices. For the residuum of the commodity, we'll show you how to setup Django on a small-scale number of operating systems, and that setup will exist causeless throughout the remainder of this module.

Annotation: Other possible installation options are covered in the official Django documentation. We link to the appropriate documents below.

What operating systems are supported?

Django web applications can be run on almost whatsoever machine that tin run the Python iii programming linguistic communication: Windows, macOS, Linux/Unix, Solaris, to name merely a few. Almost whatsoever computer should have the necessary performance to run Django during development.

In this article, we'll provide instructions for Windows, macOS, and Linux/Unix.

What version of Python should be used?

Yous can use whatsoever Python version supported by your target Django release. For Django 4.0.2 the allowed versions are Python three.8 to 3.10 (see FAQ:Installation).

The Django project recommends (and "officially supports") using the newest available supported Python release.

Where tin we download Django?

At that place are 3 places to download Django:

  • The Python Package Repository (PyPi), using the pip tool. This is the all-time way to become the latest stable version of Django.
  • Use a version from your calculator's package manager. Distributions of Django that are bundled with operating systems offer a familiar installation mechanism. Note notwithstanding that the packaged version may be quite old, and can only be installed into the system Python environment (which may non be what yous want).
  • Install from source. Y'all can become and install the latest bleeding-edge version of Django from the source. This is non recommended for beginners but is needed when you lot're ready to start contributing back to Django itself.

This article shows how to install Django from PyPi, in order to get the latest stable version.

Which database?

Django officially supports the PostgreSQL, MariaDB, MySQL, Oracle, and SQLite databases, and there are community libraries that provide varying levels of support for other popular SQL and NoSQL databases. We recommend that you select the same database for both production and development (although Django abstracts many of the database differences using its Object-Relational Mapper (ORM), in that location are still potential issues that are better to avoid).

For this commodity (and almost of this module) we will exist using the SQLite database, which stores its information in a file. SQLite is intended for use as a lightweight database and can't support a high level of concurrency. Information technology is, yet, an fantabulous option for applications that are primarily read-only.

Notation: Django is configured to utilize SQLite by default when yous start your website project using the standard tools (django-admin). Information technology's a neat choice when you're getting started because it requires no additional configuration or setup.

Installing system-wide or in a Python virtual environment?

When you lot install Python3 you go a single global environment that is shared past all Python3 code. While y'all can install whatever Python packages you like in the environment, you can only install one particular version of each package at a time.

Notation: Python applications installed into the global environment tin potentially conflict with each other (i.e. if they depend on different versions of the same parcel).

If y'all install Django into the default/global environment then you volition simply be able to target one version of Django on the computer. This can exist a trouble if you desire to create new websites (using the latest version of Django) while still maintaining websites that rely on older versions.

As a event, experienced Python/Django developers typically run Python apps within independent Python virtual environments. This enables multiple different Django environments on a single computer. The Django developer team itself recommends that you use Python virtual environments!

This module assumes that y'all've installed Django into a virtual environment, and nosotros'll show you how beneath.

Installing Python 3

In lodge to use Django you lot will have to install Python on your operating system. If you're using Python 3 and then you will as well demand the Python Package Index tool — pip3 — which is used to manage (install, update, and remove) Python packages/libraries used by Django and your other Python apps.

This section briefly explains how you lot tin check what versions of Python are present, and install new versions every bit needed, for Ubuntu Linux 20.04, macOS, and Windows 10.

Note: Depending on your platform, you may likewise exist able to install Python/pip from the operating organisation'south ain parcel manager or via other mechanisms. For well-nigh platforms, you lot can download the required installation files from https://www.python.org/downloads/ and install them using the advisable platform-specific method.

Ubuntu 20.04

Ubuntu Linux 20.04 LTS includes Python 3.8.10 past default. Y'all can confirm this by running the following control in the fustigate terminal:

However, the Python Package Index tool (pip3) you'll need to install packages for Python 3 (including Django) is not bachelor by default. You lot can install pip3 in the bash terminal using:

                                      sudo                    apt                    install                    python3-pip                                  

Note: Python 3.viii is the oldest version supported by Django 4.0. While Django recommend you update to the latest version, you don't demand to use the latest version for this tutorial. If you want to update Python, then there are instructions on the Internet.

macOS

macOS "El Capitan" and other more recent versions practise not include Python three. Y'all tin confirm this by running the post-obit commands in the zsh or bash last:

                  $ python3 -V   python3:                    command                    non found                                  

You can hands install Python iii (along with the pip3 tool) from python.org:

  1. Download the required installer:
    1. Become to https://www.python.org/downloads/macos/
    2. Download the well-nigh recent supported version that works with Django iv.0.ii. (at time of writing this is Python three.x.2).
  2. Locate the file using Finder, and double-click the parcel file. Following the installation prompts.

You tin can now confirm successful installation by checking for the Python 3 version as shown beneath:

You tin can similarly check that pip3 is installed past listing the available packages:

Windows ten

Windows doesn't include Python by default, but you tin easily install information technology (along with the pip3 tool) from python.org:

  1. Download the required installer:
    1. Go to https://www.python.org/downloads/windows/
    2. Download the most contempo supported version that works with Django iv.0.two. (at time of writing this is Python 3.10.two).
  2. Install Python past double-clicking on the downloaded file and post-obit the installation prompts
  3. Be sure to check the box labeled "Add Python to PATH"

Yous can then verify that Python iii was installed by inbound the following text into the control prompt:

The Windows installer incorporates pip3 (the Python package manager) by default. Y'all can listing installed packages as shown:

Note: The installer should fix everything you need for the above control to work. If however you lot get a message that Python cannot be found, you may have forgotten to add it to your organization path. Yous can do this by running the installer again, selecting "Modify", and checking the box labeled "Add Python to environment variables" on the second page.

Using Django inside a Python virtual surroundings

The libraries we'll use for creating our virtual environments are virtualenvwrapper (Linux and macOS) and virtualenvwrapper-win (Windows), which in plow both use the virtualenv tool. The wrapper tools creates a consistent interface for managing interfaces on all platforms.

Installing the virtual environment software

Ubuntu virtual environment setup

After installing Python and pip you can install virtualenvwrapper (which includes virtualenv). The official installation guide tin exist found hither, or follow the instructions beneath.

Install the tool using pip3:

                                      sudo                    pip3                    install                    virtualenvwrapper                                  

So add together the following lines to the stop of your shell startup file (this is a hidden file name .bashrc in your dwelling directory). These ready the location where the virtual environments should live, the location of your development projection directories, and the location of the script installed with this bundle:

                                      export                    WORKON_HOME                    =                    $Home/.virtualenvs                    consign                    VIRTUALENVWRAPPER_PYTHON                    =/usr/bin/python3                    consign                    VIRTUALENVWRAPPER_VIRTUALENV_ARGS                    =                    ' -p /usr/bin/python3 '                    consign                    PROJECT_HOME                    =                    $HOME/Devel                    source                    /usr/local/bin/virtualenvwrapper.sh                                  

Annotation: The VIRTUALENVWRAPPER_PYTHON and VIRTUALENVWRAPPER_VIRTUALENV_ARGS variables indicate to the normal installation location for Python3, and source /usr/local/bin/virtualenvwrapper.sh points to the normal location of the virtualenvwrapper.sh script. If the virtualenv doesn't work when you examination it, ane matter to cheque is that Python and the script are in the expected location (and so alter the startup file appropriately).

You can notice the correct locations for your system using the commands which virtualenvwrapper.sh and which python3.

Then reload the startup file past running the following control in the terminal:

At this point you should see a agglomeration of scripts being run equally shown beneath:

                  virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/premkproject virtualenvwrapper.user_scripts creating /domicile/ubuntu/.virtualenvs/postmkproject                    ... virtualenvwrapper.user_scripts creating /habitation/ubuntu/.virtualenvs/preactivate virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postactivate virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/get_env_details                                  

At present you tin can create a new virtual environment with the mkvirtualenv command.

macOS virtual environment setup

Setting up virtualenvwrapper on macOS is almost exactly the aforementioned every bit on Ubuntu (once more, you can follow the instructions from either the official installation guide or below).

Install virtualenvwrapper (and bundling virtualenv) using pip every bit shown.

                                      sudo                    pip3                    install                    virtualenvwrapper                                  

Then add the post-obit lines to the stop of your shell startup file (these are the same lines equally for Ubuntu). If y'all're using the zsh shell and so the startup file will be a hidden file named .zshrc in your dwelling directory. If you lot're using the bash shell and then it will be a hidden file named .bash_profile. You lot may need to create the file if it does not nevertheless be.

                                      export                    WORKON_HOME                    =                    $HOME/.virtualenvs                    export                    VIRTUALENVWRAPPER_PYTHON                    =/usr/bin/python3                    export                    PROJECT_HOME                    =                    $HOME/Devel                    source                    /usr/local/bin/virtualenvwrapper.sh                                  

Notation: The VIRTUALENVWRAPPER_PYTHON variable points to the normal installation location for Python3, and source /usr/local/bin/virtualenvwrapper.sh points to the normal location of the virtualenvwrapper.sh script. If the virtualenv doesn't work when you test it, one thing to check is that Python and the script are in the expected location (and then modify the startup file appropriately).

For example, one installation test on macOS ended up with the following lines being necessary in the startup file:

                                          export                      WORKON_HOME                      =                      $Abode/.virtualenvs                      export                      VIRTUALENVWRAPPER_PYTHON                      =/Library/Frameworks/Python.framework/Versions/iii.7/bin/python3                      export                      PROJECT_HOME                      =                      $HOME/Devel                      source                      /Library/Frameworks/Python.framework/Versions/3.seven/bin/virtualenvwrapper.sh                                      

You can discover the right locations for your system using the commands which virtualenvwrapper.sh and which python3.

So reload the startup file past making the following phone call in the terminal:

At this point, you lot may run into a agglomeration of scripts being run (the aforementioned scripts as for the Ubuntu installation). You should now be able to create a new virtual environment with the mkvirtualenv command.

Note: If you tin can't find the startup file to edit in the finder, you tin can also open up this in the final using nano.

Bold you're using bash, the commands look something like this:

                                          cd                      ~                      # Navigate to my home directory                      ls                      -la                      #Listing the content of the directory. YOu should encounter .bash_profile                      nano                      .bash_profile                      # Open the file in the nano text editor, within the terminal                      # Ringlet to the end of the file, and re-create in the lines above                      # Employ Ctrl+10 to leave nano, choose Y to save the file.                                      

Windows ten virtual environment setup

Installing virtualenvwrapper-win is even simpler than setting up virtualenvwrapper considering you don't demand to configure where the tool stores virtual surroundings information (there is a default value). All y'all need to do is run the following command in the command prompt:

                  pip3                    install                    virtualenvwrapper-win                                  

Now you can create a new virtual environment with the mkvirtualenv command

Creating a virtual environment

Once you've installed virtualenvwrapper or virtualenvwrapper-win and so working with virtual environments is very like on all platforms.

Now you can create a new virtual environs with the mkvirtualenv command. Every bit this command runs you'll meet the surround beingness set up (what you run across is slightly platform-specific). When the command completes the new virtual environment will exist active — you lot tin run across this because the beginning of the prompt will be the name of the environs in brackets (below we testify this for Ubuntu, but the concluding line is similar for Windows/macOS).

                  $ mkvirtualenv my_django_environment  Running virtualenv with interpreter /usr/bin/python3                    ... virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/t_env7/bin/get_env_details                    (my_django_environment)                    ubuntu@ubuntu:~$                                  

Now you're inside the virtual environment you lot can install Django and first developing.

Note: From now on in this article (and indeed the module) please assume that any commands are run within a Python virtual environment like the i we set up above.

Using a virtual environment

There are just a few other useful commands that you should know (there are more in the tool documentation, but these are the ones yous'll use regularly):

  • deactivate — Exit out of the current Python virtual environment
  • workon — List available virtual environments
  • workon name_of_environment — Activate the specified Python virtual environment
  • rmvirtualenv name_of_environment — Remove the specified surroundings.

Installing Django

Once you lot've created a virtual environment, and called workon to enter it, you can use pip3 to install Django.

You lot can exam that Django is installed by running the post-obit control (this but tests that Python can observe the Django module):

                                      # Linux/macOS                    python3 -m django --version                    four.0.2                    # Windows                    py -three -k django --version                    4.0.2                                  

Note: If the higher up Windows control does not show a django module present, endeavour:

In Windows Python 3 scripts are launched by prefixing the command with py -3, although this can vary depending on your specific installation. Try omitting the -3 modifier if you encounter whatsoever problems with commands. In Linux/macOS, the control is python3.

Alarm: The rest of this module uses the Linux command for invoking Python three (python3) . If y'all're working on Windows replace this prefix with: py -3

Testing your installation

The above test works, but information technology isn't very much fun. A more than interesting test is to create a skeleton project and run across information technology working. To do this, first navigate in your command prompt/concluding to where you lot want to store your Django apps. Create a folder for your test site and navigate into it.

                                      mkdir                    django_test                    cd                    django_test                                  

You can then create a new skeleton site chosen "mytestsite" using the django-admin tool as shown. After creating the site you tin navigate into the binder where you will notice the main script for managing projects, called manage.py.

                  django-admin startproject mytestsite                    cd                    mytestsite                                  

We can run the development web server from inside this folder using manage.py and the runserver command, equally shown.

                  $ python3 manage.py runserver Watching                    for                    file                    changes with StatReloader Performing system checks...  System cheque identified no issues                    (                    0                    silenced).  Yous have                    18                    unapplied migration(due south). Your project may not work properly                    until                    you apply the migrations                    for                    app(southward): admin, auth, contenttypes, sessions. Run                    'python manage.py drift'                    to use them. March 01,                    2022                    - 01:nineteen:sixteen Django version                    4.0.two, using settings                    'mytestsite.settings'                    Starting development server at http://127.0.0.1:8000/ Quit the server with Command-C.                                  

Note: The above command shows the Linux/macOS command. You tin can ignore the warnings near "eighteen unapplied migration(south)" at this point!

In one case the server is running y'all can view the site by navigating to the post-obit URL on your local spider web browser: http://127.0.0.1:8000/. Y'all should see a site that looks like this:

The home page of the skeleton Django app

Summary

Yous now have a Django development environment up and running on your computer.

In the testing department yous also briefly saw how we can create a new Django website using django-admin startproject, and run it in your browser using the evolution web server (python3 manage.py runserver). In the next commodity, we aggrandize on this process, edifice a unproblematic only complete web awarding.

Run across also

In this module

  • Django introduction
  • Setting up a Django development environment
  • Django Tutorial: The Local Library website
  • Django Tutorial Role 2: Creating a skeleton website
  • Django Tutorial Part 3: Using models
  • Django Tutorial Role 4: Django admin site
  • Django Tutorial Role v: Creating our domicile folio
  • Django Tutorial Part 6: Generic listing and item views
  • Django Tutorial Part 7: Sessions framework
  • Django Tutorial Part viii: User hallmark and permissions
  • Django Tutorial Part 9: Working with forms
  • Django Tutorial Part 10: Testing a Django spider web application
  • Django Tutorial Function 11: Deploying Django to production
  • Django web awarding security
  • DIY Django mini web log

richardsondustrads.blogspot.com

Source: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment

Post a Comment for "Django Copied Project Folder to New Computer Need to Do Makemigrations Again"