DEV Community

Cover image for Django create first project
toebes618
toebes618

Posted on

Django create first project

Django is a Python module for web development.

If you want to make web apps with Python, you can choose the Django module. It comes with many things like a model-template-view architecture and many tools.

In this article we'll cover Django management tools and how to use Django to create a project.

Before playing with Django, you should learn Python

Django Management Tools

You can install the Django framework on your computer, but you can just as easy get your Python Django app online.

After you install Django, you should now have the management tools available django-admin.py. We can use django-admin.py to create a project.

We can look at the introduction of the django-admin.py command:

[user@linux ~]# django-admin.py
Usage: django-admin.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Type 'django-admin.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    cleanup
    compilemessages
    createcachetable

Create your first project

Use django-admin.py to create helloWorld projects:

django-admin.py startproject HelloWorld

Once the creation is complete, we can view the directory structure of the next project:

[user@linux ~]# cd HelloWorld/
[user@linux HelloWorld]# tree
.
|-- HelloWorld
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py

Catalog description:

  • HelloWorld: Containers for the project.
  • manage.py: A practical command-line tool that lets you interact with the Django project in a variety of ways.
  • HelloWorld/init.py: An empty file that tells Python that the directory is a Python package.
  • HelloWorld/sets.py: Settings/configuration of the Django project.
  • HelloWorld/urls.py: URL statement for the Django project; A Django-driven website.
  • HelloWorld/wsgi.py: An entrance to a WSGI-compatible Web server to run your project.

Next, we go into the HelloWorld directory and enter the following command to start the server:

python manage.py runserver 0.0.0.0:8000

0.0.0.0 allows other computers to connect to the development server, with 8000 as the port number. If not, the port number defaults to 8000.

Enter your server's ip and port number in the browser, and if it starts normally, the output is as follows:

django hello

View and URL configuration
Create a new view.py file in the HelloWorld directory under the helloWorld directory you created earlier and enter the code:

from django.http import HttpResponse

def hello(request):
 return HttpResponse("Hello world ! ")

Next, bind the URL with the view function. Open the urls.py file, delete the original code, copy and paste the following code into the urls.py file (Django 1.6 Use "from django.conf.urls.defaults import"):

from django.conf.urls import url
from . import view

urlpatterns = [
    url(r'^$', view.hello),
]

The entire directory structure is as follows:

$ tree
.
|-- HelloWorld
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py              
|   |-- urls.pyc
|   |-- view.py              
|   |-- view.pyc             
|   |-- wsgi.py
|   `-- wsgi.pyc
`-- manage.py

When you're done, start the Django development server and open the browser with a browser access and access:

django hello world app

If you want, you can put your app online

Top comments (1)

Collapse
 
mohsin708961 profile image
{{7*7}}

Awesome