Tutorial: Writing our first views
Posted: March 21st, 2010 | Author: Adam | Filed under: Django, Python, programming | No Comments »So far in the tutorial we’ve started our project, setup the database, enabled the admin interface, created models for our blog, and we just did a brief tutorial on Django’s ORM and DB-API. Now let’s look at getting data out of our database and onto the web page using views.
This section will be a bit short because we won’t really be using templates, which we’ll cover in the next section. We will just be looking at how the urls.py file and the views.py file interact and how we can use what we learned in our ORM tutorial not in the shell but, in our application.
So remember our urls.py file from the admin interface tutorial?
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
It’s pretty sparse and doesn’t do much. Let’s make a home page
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from main.views import *
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^$', home),
)
We’ve added two lines here. The first “from main.views import *” is importing all the functions that are in the views file for the main application in our blog project (right now there are no applications). The second line I’ve added, “(r’^$’, home),”, is a tuple that I’ve added to the urlpatterns variable. This variable is where Django looks to find all the URL’s our project uses. The first part of the tuple is a regex expression for basically, nothing. It’s what happens when you just hit the domain of our site (like simpledjango.com). The second part of the tuple is the function “home”. If you goto your development server at http://127.0.0.1:8000you’ll see a Name Error. This function isn’t defined so lets go to the views.py in the main application (blog/main/views.py) and add the function home.
# Create your views here.
def home(request):
pass
This is what my views.py file looks like now. When we refresh our development server you’ll see a new error.
ValueError at /
The view blog.main.views.home didn’t return an HttpResponse object.
Django requires response object to be returned from every view. Let’s import the HttpResponse object and have our function return it.
# Create your views here.
from django.http import HttpResponse
def home(request):
return HttpResponse()
If we refresh our page again we’ll see the error is gone and we now have a blank page. Let’s type something in the HttpResponse object and see what happens
# Create your views here.
from django.http import HttpResponse
def home(request):
return HttpResponse("testing, testing 1 2 3 ")
Refresh the page and we’ll see “testing, testing 1 2 3″ on our page. Anything that is passed into the HttpResponse object instance will essentially be passed along to our browser.
Let’s get some data out of our database and pass it along. Let’s try to display all the categories that we have
# Create your views here.
from django.http import HttpResponse
from main.models import Category
def home(request):
cats = Category.objects.all()
return HttpResponse(cats)
The first difference you’ll see is the “from main.models import Category”. Here we are importing the model Category that we created in our models tutorial so we can use it in our view here. Then just like in the DB-API tutorial we are storing all the categories in a variable called cats then passing that into the HttpResponse object instantiation. When we refresh our home page we’ll see all of our categories listed out, although in a quite ugly fashion.
So as you can see getting data from our database back to the web browser is really just the same thing as using it via the DB-API in the shell. I said this would be a short tutorial since the focus was really just bringing together the concepts of using the database via the ORM and taking it out of the shell into our web application.
The next tutorial will talk about how we can really use the information in the DB on web pages using templates. (Coming Soon)
Leave a Reply