1

I could not find TEMPLATE_DIR in settings.py, to tell where my template files are.

Also, when I go to my index.html (that you can see below), parsing does not work (for exapmle {{ title }}, which you can see below in views.py ).

Why does it ( {{title }} ) not work?

I think that I am so close to make it work, but I can't.

Also, I could not find any related topic in SO.

SO, What I want to see is this:

enter image description here


But I see this:

enter image description here


index.html looks like:

enter image description here


index.html source:
<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<div class="container">
   <h1>First Blog </h1>
   <h2>{{ title }}</h2>
   <h3>Posted on {{ date }} by {{ author }}</h3>
   <p>{{ body }}</p>
</div>

</body>
</html>


view.py seems like this:
# Create your views here.

from django.shortcuts import render_to_response

from blog.models import posts

def home(request):
    return render_to_response('index.html' {'title :'My First Post'})
~


models.py seems like this:
from django.db import models

# Create your models here.

class posts(models.Model):

    author = models.CharField(max_lenght = 30)
    title = models.CharField(max_lenght = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

Here is what I did before:

Python 2.7.2 installed

user@domain.com [~]# python -V
Python 2.7.2

Django 1.5 installed

user@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import get_version
>>> get_version()
'1.5'

I had problems about creating my project (django-admin.py startpriject mysite),

username@domain.com [~/www/domain/]# django-admin.py startproject mysite
-bash: django-admin.py: command not found

it was a PATH issue, and solved at the end, thanks to SO, I have successfully created my project.

Then I modified models.py like this (to create a simple blog):

from django.db import models

# Create your models here.

class posts(models.Model):

    author = models.CharField(max_lenght = 30)
    title = models.CharField(max_lenght = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

MySQL-python 1.2.4 installed

username@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

I have created a mysql database and a user, added user to the database and gave all the privileges (done all in bluehost frontend panel).

I had problems about database update,

python manage.py syncdb

thanks to SO, that has been also solved.

user@domain.com [~/www/domain/mysite]# python manage.py syncdb 
Creating tables ... 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_groups 
Creating table auth_user_user_permissions 
Creating table auth_user Creating table django_content_type 
Creating table django_session 
Creating table django_site 

You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no):

Why does it ( {{title }} ) not work?

What should I change?

Should I re-install something?

Community
  • 1
  • 1
Eniac
  • 55
  • 7
  • 2
    `return render_to_response('index.html' {'title :'My First Post'})` doesn't look right. Please try `return render_to_response('index.html', {'title': 'My First Post'})`. – alecxe Mar 29 '13 at 09:53
  • oh, I replaced the line, but nothing has changed. `# Create your views here. from django.shortcuts import render_to_response from blog.models import posts def home(request): return render_to_response('index.html', {'title': 'My First Post'})` – Eniac Mar 29 '13 at 10:06
  • 1
    Did you start the server? It looks like the directories are just served by Apache, not by the Django backend. – Matthias Mar 29 '13 at 10:25
  • I think this might be my problem. I do not know, in which point should I start the server. I changed my code as @AlexanderAfanasiev said, started server like this: `python manage.py runserver 0.0.0.0:8000`, and now, I am not sure where to go and check. My directory is like this `domain.com/mysite/` (main project) and `domain.com/mysite/blog/` (the app), and my index.html is in `domain.com/mysite/blog/templates/index.html`, which url should I go? – Eniac Mar 29 '13 at 10:38

3 Answers3

1

As Matthias says in the comment, it doesn't look as if you're actually invoking Django at all - those are normal Apache directory indexes (so naturally Django won't be parsing the templates, since it's not even being called).

Since you're just starting out, you should not be using Apache at all, but start up the local development server (./manage.py runserver) as explained in the tutorial.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Now I understand what you mean. Those are normal Apache directory indexes. What should I do to invoke Django? Where should I seek for problems? – Eniac Mar 29 '13 at 11:22
  • 1
    Since (according to your comment above) you've started the server on port 8000, you should go to http://domain.com:8000. – Daniel Roseman Mar 29 '13 at 11:24
0

Try this:

Imports:

from django.shortcuts import render_to_response, RequestContext

Views.py:

 return render_to_response('index.html',  {'title' :Your_variable_name}, context_instance=RequestContext(request))

Call like this in template:

{{ title }}
tshepang
  • 12,111
  • 21
  • 91
  • 136
pynovice
  • 7,424
  • 25
  • 69
  • 109
  • I have changed my views.py as you explained, but nothing has changed. I go to my browser like this 'http://www.domain/mysite/blog/templates/' and I see my 'index.html'. When I go http://www.domain/mysite/ I see 'Index of /mysite'. Am I doing wrong? – Eniac Mar 29 '13 at 10:15
  • Your installation, everything is fine. In your settings.py, you have to give your full path of template directory in TEMPLATE_DIRS. Another thing, use django development server. It's easy to use. After you are abit confident, use apache server with mod_wsgi module to deploy your Django in the production server. – pynovice Mar 29 '13 at 10:58
  • I found TEMPLATE_DIRS and wrote `TEMPLATE_DIRS = ( "blog/templates",`, it has no effect, as I expected. I think my problem is, as @Matthias and @Daniel Roseman said, the directories are just served by Apache. I can't invoke Django, and at this point, I have no idea. And everyone is telling that I should work local, is it really hard to make Django work on a production server, or it is because that I am a newbie? – Eniac Mar 29 '13 at 11:18
  • No, it's not that hard to use the Django in the production server. But first get familiar with the Django itself. Use Django development server and run locally. Give the full path of template directory in TEMPLATE_DIRS and run the development server. Everything will work fine. – pynovice Mar 29 '13 at 12:09
0

You should use the render shortcut.

views.py

from django.shortcuts import render

def my_view(request):
    return render(request, 'index.htm', {'title': 'Hey!'})

This has the added advantage of adding the request instance into the context which forces the use of RequestContext. Read about it here: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render

krak3n
  • 948
  • 6
  • 13