Saturday, 6 January 2018

Writing your first Django app - PART 1

Background

Django is a python based web framework that let's you create webapps quickly and with less code. It's free and opensource. For more details on the framework itself visit -
In this post we will create a sample app in Django python framework.


Setup

This tutorial assumes you are using Django 2.0, which supports Python 3.4 and later.

Install python3 and pip3 -
  • sudo apt-get install python3-pip
Next install Django python framework using pip3 -
  • sudo pip3 install Django 
You can see the installed version of python and django in various ways. Some are given in screenshot below -


Creating a Django project

Create a skeleton of your Django app using following command -
  • django-admin startproject djangodemo
You should see a directory getting created with name djangodemo. Inside this you should have manage.py file and another directory with same name djangodemo. This inner directory named djangodemo is actually a python package. Outer directory is just a holder with manage.py file. manage.py file is used to give you command line tasks to interact with your django project. You can see the version of you django framework used with following command -
  •  python3 manage.py version 



Directory structure is as follows -



 Some other pointers other than ones mentioned above -
  • __init__.py tells python this directory should be considered as a package.
  • This also means your inner djangodemo directory is a python package.
  • settings.py: Your Django app settings go here.
  • urls.py: URLs used in your Django project go here.
  • wsgi.py: This is an entry-point for WSGI-compatible web servers that can serve your project.
 Now that you have created your project let's run it with following command -
  • python3 manage.py runserver


Ignore the warnings for now.

NOTE : You don't have to restart the server everytime you make changes to code. Django handles it. Just refresh the pages.

Open -
 You should see installation successful message as follows -



NOTE : By default your server will run on port 8000. But you can change it as follows -
  • python manage.py runserver 8080

Creating Django App

A project is collection of apps and it's configurations needed for a website to run. Apps are modules that run in your project. A project can have multiple apps. Similarly a app can be part of multiuple projects. Apps can be at any python paths.

You can create a app as follows -
  • python3 manage.py startapp testapp
I like to put all apps in a directory called apps in your actual python package directory. You can do that as follows -



Creating your webpage

Go to your apps directory and edit view.py to add following content -

from django.http import HttpResponse

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


 Next in the same directory create a file called urls.py and add following content to it -



from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]


Finally go to your project directory  - djangodemo/djangodemo and edit urls.py file to have following content -

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('test/', include('djangodemo.apps.testapp.urls')),
    path('admin/', admin.site.urls),
]


Next in apps directory inside djangodemo directory create a file called __init__.py. You can do this using -
  • touch __init__.py
Now simply run your server and visit -
to see your site.


Understanding : First we created an app called testapp. It should have some default files like view.py. view.py stores all your views. Here we added a new view called index and mapped it inside a urls.py file to the root url ("") at the app level. Next we mapped this to urls.py at our project level for '/test'. include maps the url provided and forwards rest the included module. In this case it will check url has 'test/' and forward the rest which is - "" to the urls.py in the testapp where we have mapped request view to "". So request view gets rendered.

NOTE : Note how we added a __init__.py file in apps directory. This is to ensure python recognizes this directory as a package. So that we could use djangodemo.apps.testapp.urls in the urls.py of project.

That's it you created your 1st django project and app. We will see some more details about this in next post. Thanks.

Related Links


No comments:

Post a Comment

t> UA-39527780-1 back to top