tags:

views:

105

answers:

4

I am writing a GUI application using Django 1.1.1.

This is the views.py:

from django.http import HttpResponse

def mainpage(request):
    f=open('pages/index.html','r').readlines()
    out=''''''
    for line in file:
        out+=line

    print out
    return HttpResponse(out)

I am trying to load the contents of index.html which is inside a folder pages inside the GUI application folder.

My project's urls.py is

from django.conf.urls.defaults import *
from gui.views import *

urlpatterns = patterns('',

    (r'^/$', mainpage)   
)

When I run the server I get a 404 error for the root site. How can I load the index.html file through views?

+3  A: 

If you require just simple output of html page, this can be achieved by simply putting following into urls.py:

(r'^$', 'direct_to_template', {'template': 'index.html'})

Aleksei Potov
the print out statement never works too.. what might be the problem?
Sriram
i commented those lines from the mainpage func and just put a return HttpResponse('hello'). now too the root view is not working but localhost:8080// works
Sriram
i uncommented those again and added the return HttpResponse(out)now it says 'No such file or directory', the problem must be in opening the file... but tried the same on python interpreter and it works..
Sriram
Please correct the regex according to Daniel Hernik's answer.
muhuk
+1  A: 

For the root page don't use r'^/$', just r'^$', because this ^ means "start of the string after domain AND SLASH" (after 127.0.0.1/ if you run app on localhost). That's why localhost:8080// works for you.

Edit: check yours paths too. Do you have 'pages' directory in the same directory that views.py is?

Anyway: it seems that you are trying to do something bad and against Django architecture. Look here for tutorial on writing your first application in Django.

Daniel Hernik
thanks but at last i found my way out... if you had posted a little earlier it would have helped a bit.. haha ;)
Sriram
A: 

Your actual code in the view is incorrect. Here is my fixed up version:

from django.http import HttpResponse

def mainpage(request):
    lines=open('loader/pages/index.html','r').readlines()
    out=''''''
    for line in lines:
        out+=line

    print out
    return HttpResponse(out)

Note that in your code the line that reads from the file is:

f=open('pages/index.html','r').readlines()

You open the file and read the lines into f and then try to iterate over lines. The other change is just to get my path to the actual index file right.

You might want to read this http://docs.djangoproject.com/en/dev/howto/static-files/ if you want to serve static pages.

Johan
you were right.. but there was another problem... i needed to findout the context at which the function was called which was the urs.py n it was 1 folder up.. so i had to add the parent folder to the file path..
Sriram
correction in my comment urls.py
Sriram
A: 

Got it! ;)

It seems that the mainpage function actually runs on the urls.py (as it is imported from the views.py) file so the path I must provide is gui/pages/index.html. I still had a problem, 'type object not iterable', but the following worked:

def mainpage(request):
    f=open('gui/pages/index.html','r').readlines()
    return HttpResponse(f)

And url pattern was r'^$' so it worked on http://localhost:8080/ itself.

Sriram