Create A Project
Create A Project
Whether you are on Windows or Linux, just get a terminal or a cmd prompt
and navigate to the place you want your project to be created, then use this
code −
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
Explore our latest online courses and learn new skills at your own pace.
Enroll and become a certified expert to boost your career.
Setting Up Your Project
Your project is set up in the subfolder myproject/settings.py. Following are
some important options you might need to set −
DEBUG = True
This option lets you set if your project is in debug mode or not. Debug mode
lets you get more information about your project's error. Never set it to
‘True’ for a live project. However, this has to be set to ‘True’ if you want the
Django light server to serve static files. Do it only in the development mode.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'database.sql',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
Database is set in the ‘Database’ dictionary. The example above is for SQLite
engine. As stated earlier, Django also supports −
MySQL (django.db.backends.mysql)
PostGreSQL (django.db.backends.postgresql_psycopg2)
Oracle (django.db.backends.oracle) and NoSQL DB
MongoDB (django_mongodb_engine)
Before setting any new engine, make sure you have the correct db driver
installed.
Now that your project is created and configured make sure it's working −
You will get something like the following on running the above code −
Validating models...
0 errors found
September 03, 2015 - 11:41:50
Django version 1.6.11, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Create an Application
We assume you are in your project folder. In our main “myproject” folder,
the same folder then manage.py −
You just created myapp application and like project, Django create a
“myapp” folder with the application structure −
myapp/
__init__.py
admin.py
models.py
tests.py
views.py
__init__.py − Just to make sure python handles this folder as a
package.
admin.py − This file helps you make the app modifiable in the admin
interface.
models.py − This is where all the application models are stored.
tests.py − This is where your unit tests are.
views.py − This is where your application views are.
A view function, or “view” for short, is simply a Python function that takes a
web request and returns a web response. This response can be the HTML
contents of a Web page, or a redirect, or a 404 error, or an XML document,
or an image, etc. Example: You use view to create web pages, note that you
need to associate a view to a URL to see it as a web page.
Simple View
We will create a simple view in myapp to say "welcome to my app!"
def hello(request):
text = """<h1>welcome to my app !</h1>"""
return HttpResponse(text)
In this view, we use HttpResponse to render the HTML (as you have probably
noticed we have the HTML hard coded in the view). To see this view as a
page we just need to map it to a URL (this will be discussed in an upcoming
chapter).
We used HttpResponse to render the HTML in the view before. This is not the
best way to render pages. Django supports the MVT pattern so to make the
precedent view, Django - MVT like, we will need −
A template: myapp/templates/hello.html
When linked to a URL, the page will display the number passed as a
parameter. Note that the parameters will be passed via the URL (discussed
in the next chapter).