Django Introduction and Installation
What is Django?
Django is a free and open source web application framework, written in Python.
A web framework is a set of components that helps you to develop websites faster and easier.
Django give you ready-made components you can use.
Frameworks exist to save you from having to reinvent the wheel and help alleviate some of the overhead when you’re building a new site.
What happens when someone requests a website from your server?
When a request comes to a web server it's passed to Django which tries to figure out what actually is requested. It takes a webpage address first and tries to figure out what to do. This part is done by Django's urlresolver (note that a website address is called a URL - Uniform Resource Locator - so the name urlresolver makes sense). It is not very smart - it takes a list of patterns and tries to match the URL. Django checks patterns from top to the bottom and if something is matched then Django passes the request to the associated function (which is called view).
In the view function all the interesting things are done: we can look at a database to look for some information. The view can check if you are allowed to do that, Then the view generates a response and Django can send it to the user's web browser.
Virtual environment
Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect any others you're also developing. Neat, right?
All you need to do is find a directory in which you want to create the virtualenv; your home directory
we will be using a new directory djangoproject from your home directory:
mkdir djangoproject
cd djangoproject
We will make a virtualenv called myvenv. The general command will be in the format:
python3 -m venv myvenv
myvenv is the name of your virtualenv. You can use any other name, but stick to lowercase and use no spaces. It is also good idea to keep the name short as you'll be referencing it a lot!
Start your virtual environment by running:
$ source myvenv/bin/activate
NOTE: sometimes source might not be available. In those cases try doing this instead:
$ . myvenv/bin/activate
You will know that you have virtualenv started when you see that the prompt in your console is prefixed with (myvenv).
When working within a virtual environment, python will automatically refer to the correct version so you can use python instead of python3.
OK, we have all important dependencies in place. We can finally install Django!
Installing Django
Now that you have your virtualenv started, you can install Django using pip. In the console, run
pip install django~=1.9.0
or
easy_install django==1.9
That's it! You're now (finally) ready to create a Django application!
You can tell Django is installed and which version by running the following command:
$ python -c "import django; print(django.get_version())"
or
django-admin --version
python --version
Django is a free and open source web application framework, written in Python.
A web framework is a set of components that helps you to develop websites faster and easier.
Django give you ready-made components you can use.
Frameworks exist to save you from having to reinvent the wheel and help alleviate some of the overhead when you’re building a new site.
What happens when someone requests a website from your server?
When a request comes to a web server it's passed to Django which tries to figure out what actually is requested. It takes a webpage address first and tries to figure out what to do. This part is done by Django's urlresolver (note that a website address is called a URL - Uniform Resource Locator - so the name urlresolver makes sense). It is not very smart - it takes a list of patterns and tries to match the URL. Django checks patterns from top to the bottom and if something is matched then Django passes the request to the associated function (which is called view).
In the view function all the interesting things are done: we can look at a database to look for some information. The view can check if you are allowed to do that, Then the view generates a response and Django can send it to the user's web browser.
Virtual environment
Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect any others you're also developing. Neat, right?
All you need to do is find a directory in which you want to create the virtualenv; your home directory
we will be using a new directory djangoproject from your home directory:
mkdir djangoproject
cd djangoproject
We will make a virtualenv called myvenv. The general command will be in the format:
python3 -m venv myvenv
myvenv is the name of your virtualenv. You can use any other name, but stick to lowercase and use no spaces. It is also good idea to keep the name short as you'll be referencing it a lot!
Start your virtual environment by running:
$ source myvenv/bin/activate
NOTE: sometimes source might not be available. In those cases try doing this instead:
$ . myvenv/bin/activate
You will know that you have virtualenv started when you see that the prompt in your console is prefixed with (myvenv).
When working within a virtual environment, python will automatically refer to the correct version so you can use python instead of python3.
OK, we have all important dependencies in place. We can finally install Django!
Installing Django
Now that you have your virtualenv started, you can install Django using pip. In the console, run
pip install django~=1.9.0
or
easy_install django==1.9
That's it! You're now (finally) ready to create a Django application!
You can tell Django is installed and which version by running the following command:
$ python -c "import django; print(django.get_version())"
or
django-admin --version
python --version
0 comments:
Post a Comment