0% found this document useful (0 votes)
25 views5 pages

Create A Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Create A Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 −

$ django-admin startproject myproject

This will create a "myproject" folder with the following structure −

myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py

The Project Structure


The “myproject” folder is just your project container, it actually contains two
elements −

 manage.py − This file is kind of your project local django-admin for


interacting with your project via command line (start the development
server, sync db...). To get a full list of command accessible via
manage.py you can use the code −
$ python manage.py help
 The “myproject” subfolder − This folder is the actual python
package of your project. It contains four files −
o __init__.py − Just for python, treat this folder as package.
o settings.py − As the name indicates, your project settings.
o urls.py − All links of your project and the function to call. A kind
of ToC of your project.
o wsgi.py − If you need to deploy your project over WSGI.

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.

You can also set others options like: TIME_ZONE, LANGUAGE_CODE,


TEMPLATE…

Now that your project is created and configured make sure it's working −

$ python manage.py runserver

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.

A project is a sum of many applications. Every application has an objective


and can be reused into another project, like the contact form on a website
can be an application, and can be reused for others. See it as a module of
your project.

Create an Application
We assume you are in your project folder. In our main “myproject” folder,
the same folder then manage.py −

$ python manage.py startapp myapp

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.

Get the Project to Know About Your Application


At this stage we have our "myapp" application, now we need to register it
with our Django project "myproject". To do so, update INSTALLED_APPS tuple
in the settings.py file of your project (add your app name) −
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)

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.

In Django, views have to be created in the app views.py file.

Simple View
We will create a simple view in myapp to say "welcome to my app!"

See the following view −

from django.http import HttpResponse

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

And now our view will look like −

from django.shortcuts import render


def hello(request):
return render(request, "myapp/template/hello.html", {})

Views can also accept parameters −

from django.http import HttpResponse

def hello(request, number):


text = "<h1>welcome to my app number %s!</h1>"% number
return HttpResponse(text)

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).

You might also like