Hi guys, I am writing this blog to help people starting with Django. This tutorial inclined on basic of Django so that any one cat start with. If anyone has some other doubt than free to ask me by making a comment or email me.
Django is python framework for web development, Before Jango I only know Flask(Other python Framework), it is very much similar to a simple python programming. For learning Flask, you only have to read very first page of there documentation And you ready to build. whereas Django is the vast and versatile framework. It is very nicely written so to make your code look cleaner.
Start with basic installation
Install using pip:
pip install django
confirm install by typing:
django-admin --version
1.9
No error uptill here, means you have succefully install django
Make directory django_project
by typing command:
mkdir django_project
Now let’s do some Django.
Creating a project:
From command line cd into directory and run following command:
django-admin startproject mysite
This will create Django project name “mysite”.
Let’s look at what startproject created:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
- The outer mysite/ root directory is just a container for your project.
manage.py
is a command line utility that interacts with Django project in various ways.- The inner mysite/ directory is the actual Python package for your project.
- mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
- mysite/settings.py: Settings/configuration for this Django project. It contains configuration related to database setup, template static file location etc.
- mysite/urls.py: The URL declarations for this Django project;
Development server
python manage.py runserver
In your web browser type http://127.0.0.1:8000/
. You will probably be going to see the nice light blue window showing a success message.
Django comes with lightweight development server so that you can boost development of your application. But we suggest not to used for production.
Congratulate guys you have successfully installed Django
Now next part is adding application and basic functioning of files present.
Creating polls app:
For creating your first app type command:
python manage.py startapp polls
This command will generate polls directory with follwing file:
polls/
init.py
admin.py
apps.py
migrations/
init.py
models.py
tests.py
views.py
Writing views
“Views.py” as by name is responsible what user will view. Views are written in file views.py .
polls/views.py
This is simplest view in Django. Views are called when they map with URL - for this we need to URLconf.
To create a URLconf in the polls directory, create a file called urls.py. Your app directory will look like:
polls/
init.py
admin.py
apps.py
migrations/
init.py
models.py
tests.py
urls.py
views.py
In the polls/urls.py file include the following code:
polls/urls.py
The next step is to point the root URLconf at the polls.urls
module. In, mysite./urls.py
, add an import for django.conf.urls.include and insert an include() in the “urlpatterns” list, so you have:
mysite/urls.py
Now again runserver, And go to link http://localhost:8000/polls/ in your browser.
you are done with basic django installation now for more learning got Django Official tutorial part 2.
For any suggestion bugs and doubt feel free to comment or email.