[python web framework] Getting Started with django

Programming 2014. 11. 21. 11:17 by touchsoul

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

[python web framework] Getting Started with django

OS: Ubuntu 14.10
Python: 2.7.8

Setup django

$ sudo apt-get install python-django

$ django-admin --version

Create Project

$ django-admin startproject [PROJECT_NAME]

Config DB(**already install mysql)

$ vim settings.py
    # insert into settings.py #
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',    # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': [DB_NAME],                        # Or path to database file if using sqlite3.
            'USER': [USER_ID],                         # Not used with sqlite3.
            'PASSWORD': [USER_PASSWORD]                # Not used with sqlite3.
            'HOST': 'localhost',                       # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '3306',                          # Set to empty string for default. Not used with sqlite3.
        }
    }
    # insert into settings.py #
$ sudo apt-get install python-mysqldb

Create webapp

$ python manage.py startapp webapp

$ vim views.py
    from django.shortcuts import render
    from django.http import HttpResponse

    def main_page(request):

      output ='''

        <html>

        <head><title>%s</title></head>

        <body>

                <h1>%s</h1>

                <p>%s</p>

        </body>

        </html>

        ''' % ( 'Django TEST',

                'Welcome to Django',

                'Hello World!!'

        )

      return HttpResponse(output)
    # insert into views.py #
$ vim urls.py
    # insert into urls.py #
    from django.conf.urls import patterns, include, url
    from django.contrib import admin
    admin.autodiscover()

    urlpatterns = patterns('',
        (r'^$','webapp.views.main_page'),
    )
    # insert into urls.py #

Start server

$ python manage.py runserver 0.0.0.0:8000

Confirm

execute browser url [localhost:8000]


See Site: http://rocksea.tistory.com/66

Nav