Hello World django

Here are simple steps to create hello world web application in python using django framework

  1. Step 1: Install virtual environment and create virtual environment for your project
    pip install virtualenvwrapper-win
    
    mkvirtualenv myproject
  2. pip install virtualenv
  3. virtualenv name_to_your_env
    
  4. name_to_your_env\Scripts\activate
  5. After activation 
    
    $ django-admin.py startproject HelloWorld  $ cd HelloWorld 
    $ ls HelloWorld manage.py
  6. $ python manage.py runserver 
    Validating models... 
    0 errors found ... 
    Django version 1.6.5, using settings 'HelloWorld.settings' 
    Starting development server at http://127.0.0.1:8000/ 
    Quit the server with CONTROL-C.
  7. $ django-admin startapp HelloWorldApp
    $ ls
    HelloWorld HelloWorldApp manage.py
  8. edit settings.py under HelloWorld project directory
    # Application definition
    INSTALLED_APPS = ( ‘django.contrib.admin’,
    ‘django.contrib.auth’,
    ‘django.contrib.contenttypes’,
    ‘django.contrib.sessions’,
    ‘django.contrib.messages’,
    ‘django.contrib.staticfiles’,
    ‘HelloWorldApp’, )
  9. modify the urls.py which is under the project directory, HelloWorld
    from django.conf.urls import patterns, include, url 
    from HelloWorldApp.views import foo 
    
    #from django.contrib import admin 
    #admin.autodiscover() 
    
    urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'HelloWorld.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
    #url(r'^admin/', include(admin.site.urls)), 
    url(r'HelloWorldApp/$', foo), 
    )
  10. modify views.py under app directory, HelloWorldApp
    # Create your views here. 
    from django.http import HttpResponse 
    def foo(request): 
        return HttpResponse("Hello World!")
  11. Run the server
    python manage.py runserver

     

  12. Hit this URL on browser
    http://localhost:8000/HelloWorldApp/

 

Ref Links:

  • https://docs.djangoproject.com/en/1.11/howto/windows/
  • http://www.bogotobogo.com/python/Django/Python_Django_hello_world.php
  • https://stackoverflow.com/questions/18684231/python-setup-command-not-found
  • https://stackoverflow.com/questions/8074955/cannot-import-name-patterns
  • https://scotch.io/tutorials/build-your-first-python-and-django-application
  • http://jinja.pocoo.org/docs/2.10/templates/