Django First Application

Creating First Views in Django


$ django-admin startproject mysite

Let’s look at what startproject created:

mysite/

manage.py

mysite/

__init__.py

settings.py

urls.py

wsgi.py

$ python manage.py runserver

Changing the port

By default, the runserver command starts the development server on the internal IP at port 8000.

If you want to change the server’s IP, pass it along with the port

python manage.py runserver 8080

Projects vs. apps

Apps: An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app.

Project: A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

Creating Application

$ python manage.py startapp polls

That’ll create a directory polls, which is laid out like this:

polls/

__init__.py

admin.py

apps.py

migrations/

__init__.py

models.py

tests.py

views.py

Writing First Views: -

polls/views.py

from django.http import HttpResponse

def index(request):

return HttpResponse("Hello, world. You're at the polls index.")

This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we need a URLconf.

To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

polls/

__init__.py

admin.py

apps.py

migrations/

__init__.py

models.py

tests.py

urls.py

views.py