Setting up the Environment
To set up the environment for development in Django, you have to install and set up Python, the Django and a Database System. Since Django is used for development of web apps, you also have to set up a server.
- Installation of Python :-
The code for Django is designed 100% for Python. This means that you will have to install Python in your system. For those using the latest versions of either Linux or Mac OS, the Python will have already been installed for you. To verify this, just open your command prompt and then type the command “python”. If python has already been installed, you should see something similar to this:
$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
If you don’t get the above result, make sure that you download and install the latest version of Python to your system.
- Installation of Django :-
This can easily be done, but the steps involved are determined by the kind of operating system you are using. Please note that Python is a platform that is platform independent. This is why there is only a single package for Django, because it can work on all the platforms.
- Installation on Unix/Linux and Mac OS :-
In these operating systems, the Django framework can be installed in the following two ways:
- Using the package manager of the OS, or use “pip” or “easy_install”, if they have already been installed.
- Manual installation by use of the archive you had downloaded before.
We will only explore the second option, as the first one will depend on the distribution of the OS you are using. If you choose to follow the first method, be careful with the version of the Django that you install. It is best to install the latest version of the framework.
You can then extract and install the package. The following commands are necessary:
$ tar xzvf Django-x.xx.tar.gz
$ cd Django-x.xx
$ sudo python setup.py install
To test your installation, just execute the command given below:
$ django-admin.py –version
If you see the current version of Django displayed on the screen, you will know that everything has been set up as intended.
- Installation on Windows :-
Our assumption is that you already have your Python and Django Archive installed on your system. Begin by verifying your path.
On certain versions of the Windows OS, you should first check that the PATH variable has been set to the following:
C:\Python27\;C:\Python27\Lib\site-packages\django\bin\
However, the above will be determined by the version of Python that you are using. The extraction can be done as follows:
c:\>cd c:\Django-x.xx
It is now time for you to install the Django. For the command for doing this to be executed, you must have administrative privileges over the system. Here is the command:
c:\Django-x.xx>python setup.py install
The installation can be verified by running the following on the cmd:
c:\>django-admin.py –version
The current version of the Django should be displayed on the screen.
For the case of setup of the database, there is a specific way to do it for any database. There are several tutorials online explaining how to do this. Django also comes pre-configured with a web server. This can be used for the purpose of developing and testing web applications.
- Creation of the Project :-
For both Linux and Windows users, launch your terminal or cmd and then navigate to where you need your project to be located. Just execute the command given below:
$ django-admin startproject project1
The above command will create a project named “project1” and this will have the structure given below:
Project1/
manage.py
project1/
__init__.py
settings.py
urls.py
wsgi.py
- Structure of the Project :-
The “Project1” folder is in the project container and it is made up of two elements:
- manage.py- this file is responsible for facilitating you to perform an interaction with your project via the command line. If you need to view the list of commands that can be accessed via this file, you can execute the following command:
$ python manage.py help
“Project1” subfolder- this represents the actual Python package for the project. It has four files:
__init__.py :- this is for Python and it should be treated as a package.
settings.py :- this has the settings for your project.
urls.py :- this has the links to your project as well as the functions to call. It is just a kind of ToC for the project.
wsgi.py :- this is needed if you want to perform a deployment of your project via the WSGI.
- Setting Up the Project :-
The project has been set up in the folder named “project1/settings.py”. Let’s discuss some of the important aspects that you may have to set up:
DEBUG = True
With the above option, you will be in a position to set the project to either be in debug mode or not. In debug mode, you will be in a position to get more information regarding the errors of a project. This should not be set to “True” when the project is live. To enable your Django light server serve static files, this has to be set to “True”. This should only be done in the development mode.
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: ‘database.sql’,
‘USER’: ”,
‘PASSWORD’: ”,
‘HOST’: ”,
‘PORT’: ”,
}
}
In the example given above, we have used the SQLite engine. Before you can set up any new engine, you should always ensure that you have correctly installed the necessary db driver.
Since you have created and configured the project, it is a good idea to check to make sure that it is working. This can be done as shown below:
$ python manage.py runserver
👈Episode 1(D). Episode 3 (D)👉
Share This Post
PRINT THIS POST
No comments:
Post a Comment
If you have any doubts. Please let me know.