Sunday, April 26, 2020

Django pollapp - 2( creating the poll app)


Creating the Polls app

In the previous post we created our environment for our pollsite “project”. Now it’s time to start doing the real work.

Each application you write in Django consists of a Python package that follows a certain convention. Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.

We’ll create our poll app in the same directory as our manage.py file so that it can be imported as its own top-level module, rather than a submodule of pollsite.

To create our app, make sure you’re in the same directory as manage.py and type this command: python manage.py startapp polls

Microsoft Windows [Version 10.0.18362.778]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\vswam>cd C:\Application_django

C:\Application_django>pollapp_env\Scripts\activate

(pollapp_env) C:\Application_django>python manage.py startapp polls

(pollapp_env) C:\Application_django>

Now notice the directory structure created:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
views.py



Now we will create our first view. Open the file polls/views.py and put the following code in it:

from django.http import HttpResponse

# Create your views here.
def index(request):
            return HttpResponse("You're at the polls index.")


To call the view, we need to map it to a URL - and for this we need a URLconf.

To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py

In the polls/urls.py file include the following code:

from django.urls import path

from . import views

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

Next, we need to point the root URLconf at the polls.urls module. In pollsite/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 path, include

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


The include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing. The idea behind include() is to make it easy to plug-and-play URLs.

We have now wired an index view into the URLconf. Verify it’s working by going to http://localhost:8000/polls/ (make sure the Django server is up and running).

You should see the following message in your browser- “You're at the polls index.”

The path() function is passed four arguments, two required: route and view, and two optional: kwargs, and name. route is a string that contains a URL pattern. When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.

For example, in a request to http://localhost:8000/polls/  the URLconf will look for polls/.

When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.

Using path() argument: kwargs , arbitrary keyword arguments can be passed in a dictionary to the target view.

Using path() argument: name, naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.
Share:

0 comments:

Post a Comment