In the last tutorial we designed a project. Project holds the whole infrastructure, it can contain multiple apps. A weblog, an eshop page, poll app could all be an app in our newly created project. The examples are endless.
After project creation to create an app you can use
The structure of the polls app should be as below:
All the poll application logic resides here.
We try first by creating a view that return a sample http response. For writing view for your polls application you have to put it inside of
Yes! That's it. We have created view in django. But for calling this view from outside you have to map a route to it. In my app I don't have the file
The next step is to bind the newly defined route in polls to your project urls route. So for now go to
The first url says that give every route that starts with
Now run your project with the command below and see the result:
The final step is to open your browser and point it to
Voila! Good job everybody :)
If you go to base path you will see the error below (see the picture):
#django #url #include #views #pattern #django_part3
After project creation to create an app you can use
startapp
. For now we create a poll app:$ python manage.py startapp polls
$
The structure of the polls app should be as below:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
All the poll application logic resides here.
We try first by creating a view that return a sample http response. For writing view for your polls application you have to put it inside of
views.py
:from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Yes! That's it. We have created view in django. But for calling this view from outside you have to map a route to it. In my app I don't have the file
urls.py
so create it in polls and map as below:from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
url
is django module to implement the routing functionality.The next step is to bind the newly defined route in polls to your project urls route. So for now go to
saturn/urls.py
:from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
The first url says that give every route that starts with
polls
to polls.urls
. You can see that we have used include
to reference URL configuration. When it reaches /polls
it removes polls
and send the remaining of the URL to polls
.Now run your project with the command below and see the result:
$ python manage.py runserver
The final step is to open your browser and point it to
/polls
and see the result:http://127.0.0.1:8000/polls/
Voila! Good job everybody :)
If you go to base path you will see the error below (see the picture):
#django #url #include #views #pattern #django_part3