There are two very important things you need to do to get started with Django:
- Install Django (obviously); and
- Get a good understanding of the Model-View-Controller (MVC) design pattern.
The first, installing Django is really simple and detailed in the first part of this chapter. The second is just as important, especially if you are a new programmer or coming from using a programming language that does not clearly separate the data and logic behind your website from the way it is displayed. Django’s philosophy is based on loose coupling, which is the underlying philosophy of MVC. We will be discussing loose coupling and MVC in much more detail as we go along, but if you don’t know much about MVC, then you best not skip the second half of this chapter, because understanding MVC will make understanding Django so much easier.
Before you can start learning how to use Django, you must first install some software on your computer. Fortunately, this is a simple three step process:
If this does not sound familiar to you don’t worry, in this blog I assume you have never installed software from the command line before and will lead you through it step by step.
I have written this section for those of you running Windows. While there is a strong *nix and OSX user base for Django, most new users are on Windows. If you are using Mac or Linux, there are a large number of resources on the Internet; with the best place to start being Django’s own installation instructions.
For Windows users, your computer can be running any recent version of Windows (Vista, 7, 8.1 or 10). This chapter also assumes you’re installing Django on a desktop or laptop computer and will be using the development server and SQLite to run all the example code in this book. This is by far the easiest, and best way to setup Django when you are first starting out.
If you are using Windows, I recommend that you try out Visual Studio for all your Django development. Microsoft have made a significant investment in providing support for Python and Django programmers. This includes full IntelliSense support for Python/Django and incorporation of all of Django’s command line tools into the VS IDE.Best of all it’s entirely free. I know, who would have expected that from M$??, but it’s true!See Appendix G for a complete installation guide for Visual Studio Community 2015, as well as a few tips on developing Django in Windows.
Django itself is written purely in Python, so the first step in installing the framework is to make sure you have Python installed.
Django version 1.8 LTS works with Python version 2.7, 3.3, 3.4 and 3.5. For each version of Python, only the latest micro release (A.B.C) is supported.
If you are just trialing Django, it doesn’t really matter whether you use Python 2 or Python 3. If, however, you are planning on eventually deploying code to a live website, Python 3 should be your first choice.
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
Unless you have a very good reason to use Python 2 (e.g. legacy libraries), Python 3 is the way to go.
NOTE: All of the code samples in this book are written in Python 2.7
Assuming Python is not installed in your system, we first need to get the installer. Go to https://www.python.org/downloads/ and click the big yellow button that says “Download Python 2.x.x”.
At the time of writing, the latest version of Python is 3.5.1, but it may have been updated by the time you read this, so the numbers may be slightly different.
Once you have downloaded the Python installer, go to your Downloads folder and double click the file “python-2.7.x.msi” to run the installer. The installation process is the same as any other Windows program, so if you have installed software before, there should be no problem here, however the is one extremely important customization you must make.
Do not forget this next step as it will solve most problems that arise from incorrect mapping of pythonpath (an important variable for Python installations) in Windows.
By default, the Python executable is not added to the Windows PATH statement. For Django to work properly, Python must be listed in the PATH statement. Fortunately, this is easy to rectify:
- In Python 2.7.x, When the installer opens the customization window, the option “Add python.exe to Path” is not selected, you must change this to “Will be installed on local hard drive” as shown in Figure 1-1.
- In Python 2.7.x you make sure “Add Python 2.7 to PATH” is checked before installing (Figure 1-1).
Figure 1-1: Add Python to PATH (Version 2.7.x).
After the successful installation of Python we have to install django. Before going to the installation make sure the internet connection is enabled in your system, because while installing djago it will download the file from the internet. To install django go to the command prompt in your system, in CMD type the following instruction.
Step 1: The starting window of command prompt
Step 1: you have to change your project directory to the python directory
Step – 3 after changed your directory, have to give the following command.
The output will be something similar to this:
e:\python>pip install django
Downloading django version x.xx.xx
100% |################################| 1.2MB 198kB/s
Installing collected packages: django
You don’t need to understand exactly what this command does right now; put briefly pip is the Python package manager. It’s used to install Python packages: pip is actually a recursive acronym for “Pip Installs Packages”. Pip is important for the next stage of our install process, but first we need to make sure we are running the latest version of pip (8.1.2 at the time of writing), which is exactly what this command does.
Step – 4 after the successful installation of django , to check the version of django installed type the below command
This step is not necessary in order to complete any of the examples in this blog. Django comes with SQLite installed by default. SQLite requires no configuration on your part.
Once you’ve installed Python, Django and (optionally) your database server/library, you can take the first step in developing a Django application by creating a project.
A project is a collection of settings for an instance of Django. If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project: a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
To start a new project from django follow the follow command
You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like “django” (which will conflict with Django itself) or “test” (which conflicts with a built-in Python package).
Let’s look at what startproject created:
These files are:
- The outer projectname/root directory. It’s just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
- py. A command-line utility that lets you interact with your Django project in various ways.
- The inner projectname/ It’s the Python package for your project. It’s the name you’ll use to import anything inside it (e.g. mysite.urls).
- projectname/py. An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docsif you’re a Python beginner.).
- projectname/py. Settings/configuration for this Django project. Appendix D will tell you all about how settings work.
- projectname/py. The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in Chapters 2 and 7.
- projectname/py. An entry-point for WSGI-compatible web servers to serve your project. See Chapter 13 for more details.
Now, edit projectname/settings.py. It’s a normal Python module with module-level variables representing Django settings. First step while you’re editing settings.py, is to set TIME_ZONE to your time zone. Note the INSTALLED_APPS setting at the top of the file. That holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects. By default, INSTALLED_APPS contains the following apps, all of which come with Django:
- contrib.admin– The admin site.
- contrib.auth– An authentication system.
- contrib.contenttypes– A framework for content types.
- contrib.sessions– A session framework.
- contrib.messages– A messaging framework.
- contrib.staticfiles– A framework for managing static files.
After the above we have to create our own application inside the project folder
After the above you can create your own application by the following command:
These applications are included by default as a convenience for the common case. Some of these applications makes use of at least one database table though, so we need to create the tables in the database before we can use them. To do that, run the following command:
python manage.py migrate
The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your settings.py file and the database migrations shipped with the app (we’ll cover those later). You’ll see a message for each migration it applies.
Let’s verify your Django project works.
Change into the outer projectname directory, if you haven’t already, and run the following commands:
You’ve started the Django development server, a lightweight Web server written purely in Python. We’ve included this with Django so you can develop things rapidly, without having to deal with configuring a production server – such as Apache – until you’re ready for production.
Now’s a good time to note: don’t use this server in anything resembling a production environment. It’s intended only for use while developing.
Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Welcome to Django” page in pleasant, light-blue pastel (Figure 1-3). It worked!
Figure 1-2: Django’s welcome page Automatic reloading of runserver The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.
Hope this blog inspires you to embrace change and grow. Whether you are an XPLORER or not, feel free to glance through our blogs and help us guide and support you in your voyage to success. Happy Learning!
- # VLSI
- #Advanced JAVA
- #Big DATA
- #C Programming
- #Cloud Computing
- #CORPORATE Training
- #DATA MINING
- #Embedded systems
- #Ethical Hacking
- #INSTITUTIONAL Training
- #Java Script
- #LAB View
- #MAT LAB
- #Mongo DB
- #PCB Designing
- #Placement Training
- #R Programming
- #Raspberry Pi
- #RETAIL Training
- #Soft Skills
- #software testing
- #Web Designing
- #Windows Server
- #Word press
- #XPLORE IT CORP
- 10 Easy Ways to Facilitate Web Development CoursesPHP
- 7 Essential Ways to Learn Android Course:ANDROID
- Best Internships and inplant training institute in Coimbatore (Xplore IT Corp)Courses
- Five Ways to Learn Internet of Things (IOT) EffectivelyCourses
- Six Ways to Learn Artificial Intelligence Training EffectivelyCourses