First django app

Posted under » Django on 29 April 2021

This is from the official django polls app tutorial v.3.2 but I make it shorter and adjust it to my taste. I testing it with version 3.15 and it worked for me except for the The display decorator part.

You should an app name that is unique. You can't create an app called 'admin' or 'auth' which is already existing.

In this case, I will start an app called polls. You start the polls app by

$ python manage.py startapp polls

It will create a few files. One of them is the apps.py file which you don't have to touch but you will need to if you change the app name or folder.

Next we create a view by editing the view.py file which is the index file.

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

We assign the the url to the view by creating a url file 'polls/urls.py'

from django.urls import path

from . import views

urlpatterns = [
    path('', 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.urls.include and insert an include() in the urlpatterns list, so you have:

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

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

If for some reason, you don't see the http://mysite/polls/ you have to restart daemon and apache.

Next we create Django models using migrate.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS