Writing your first Django app, part 1
Throughout this tutorial, we’ll walk you through the creation of a basic
poll application.
It’ll consist of two parts:
You can tell Django is installed and which version by running the following command:
Let’s look at what
These files are:
Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser.
By default, the
It’ll consist of two parts:
- A public site that lets people view polls and vote in them.
- An admin site that lets you add, change, and delete polls.
You can tell Django is installed and which version by running the following command:
$ python -m django --version
Creating a project
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.
From the command line,
cd
into a directory where you’d like to store your
code, then run the following command:$ django-admin startproject mysite
This will create a
mysite
directory in your current directory.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. Its name doesn’t matter to Django; you can rename it to anything you like. manage.py
: A command-line utility that lets you interact with this Django project in various ways.- The inner
mysite/
directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g.mysite.urls
). 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.mysite/urls.py
: The URL declarations for this Django project; a “table of contents” of your Django-powered site. URL dispatcher.mysite/wsgi.py
: An entry-point for WSGI-compatible web servers to serve your project.
The development server
Change into the outer
mysite
directory, if
you haven’t already, and run the following commands:$ python manage.py runserver
or
$./manage.py runserver
(Note : don't need to prefix python each time)
You’ve started the Django development server, a lightweight Web server written purely in Python.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.
It worked!
Changing the port
runserver
command starts the development server
on the internal IP at port 8000.
localhost:8000
If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
If you want to change the server’s IP, pass it along with the port. So to
listen on all public IPs (useful if you want to show off your work on other
computers on your network), use:
To create your app, make sure you’re in the same directory as
Let’s write the first view. Open the file
In the
You have now wired an
Go to http://localhost:8000/polls/ in your browser, and you should see the
text “Hello, world. You’re at the polls index.”, which you defined in the
If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
$ python manage.py runserver 8080
$ python manage.py runserver 0.0.0.0:8000
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.
Creating the Polls app :
Django comes with a utility that automatically generates
the basic directory structure of an app, so you can focus on writing code
rather than creating directories.
manage.py
and type this command:$ python manage.py startapp polls
or
$ django-admin startapp polls
Projects vs. 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.
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.
This directory structure will house the poll application.
polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
Write your first view :
polls/views.py
and put the following Python code in it:
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
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
under polls app .In the
polls/urls.py
file include the following code:
polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
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
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
The
include()
function allows referencing other
URLconfs. Note that the regular expressions for the
include()
function doesn’t have a $
(end-of-string
match character) but rather a trailing slash. Whenever Django encounters
include()
, it chops off whatever part of the URL
matched up to that point and sends the remaining string to the included URLconf
for further processing.
The idea behind
include()
is to make it easy to
plug-and-play URLs Since polls are in their own URLconf
(polls/urls.py
) .
When to use
include() :
You should always use
include()
when you include other URL patterns.
admin.site.urls
is the only exception to this.index
view into the URLconf. Lets verify it’s
working, run the following command:$ python manage.py runserver
index
view.
The
url()
function is passed four arguments, two
required: regex
and view
, and two optional: kwargs
, and name
.
0 comments:
Post a Comment