15. Blog app : views and urls
If we want to display other than Django's default page, we should do something with the file, views.py.
from django.shortcuts import HttpResponse # Create your views here. def hello(request): name = "Bogo" html = "<html><body>Hi %s from views.py. </body></html>" %name return HttpResponse(html)
We have views to display. Then, how we map to this view from url of the user's request? In other words, which view function should be called?
The urls.py does mapping from the user's url to the proper view.
from django.conf.urls import patterns, include, url #from django.contrib import admin #admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'DjangoTestProject.views.home', name='home'), # url(r'^blog/', include('blog.urls')), #url(r'^admin/', include(admin.site.urls)), url(r'^hello/', 'blog.views.hello'), )
First, we imported url, then we use url() function. What it does is basically matching hello/ and maps it to blog.views.hello.
Run Django's web server:
$ python mage.py runserver
Then, type in the url, we get:
Note that we used the simples html display which is a just a string. If we want to display more complicated one, we need to use template. So, let's move on to the next chapter!
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization