Forums

{% ... %} part in HTML is not seen on my web app page.

After git pull, all sources is updated correctly. In view of "File tab", I can see the source as following:

<body>
    <div class="page-header">
    {% if user.is_authenticated %}
        <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
    {% endif %}
        <h1><a href="/">DJ's NMU test webpage</a></h1>
    </div>
    <div class="content container">
        <div class="row">
            <div class="col-md-8">
            {% block content %}
            {% endblock %}
            </div>
        </div>
    </div>
</body>

but, after reloading on web tab, this page is seen without all {% %} parts as following:

<body>
    <div class="page-header">

        <h1><a href="/">DJ's NMU test webpage</a></h1>
    </div>
    <div class="content container">
        <div class="row">
            <div class="col-md-8">


            </div>
        </div>
    </div>
</body>

I can see everything on my local server. What happened on it? What does pythonanywhere make difference?

Uh. That's what's supposed to happen. The template engine is executing the blocks and determining that they have no content (the user is not authenticated and the content block is empty) and that's what it's showing to you.

That's what I know why pythonanywhere template engine thinks that block is no content. That blocks are used for template extending in Django/Python project. I am following the "Django Tutorial" and I have no problem on my local server.

Did you transfer your database to PythonAnywhere?

Absolutely!

Then you may not be using the correct path to the database (I'm assuming you're using sqlite). When you define the database connection string in your Django settings make sure you use the absolute path to the file for where the file is on PythonAnywhere.

My settings.py is

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

in "/ > home > djju1029 > my-first-blog > mysite > settings.py"

and

import os
import sys

path = '/home/djju1029/my-first-blog'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())

in "/ > var > www > djju1029_pythonanywhere_com_wsgi.py"

Isn't correct?

That looks correct assuming that the db.sqlite3 file is in BASE_DIR. Have you checked that the data that you expect is, in fact in the file?

Yes, there are....I have tested again the same source, same step with new account, but the result was same. The django template in HTML is not shown on after reloading on pythonanywhere. The following is actual source in my post_list.html:

<html>
    <head>
        <title>Django Girls blog</title>
    </head>
    <body>
        <div>
            <h1><a href="">Django Girls Blog</a></h1>
        </div>

        {% for post in posts %}
            <div>
                <p>published: {{ post.published_date }}</p>
                <h1><a href="">{{ post.title }}</a></h1>
                <p>{{ post.text|linebreaksbr }}</p>
            </div>
        {% endfor %}
    </body>
</html>

and the below is the source view of djju.pythonanywhere.com:

<html>
    <head>
        <title>Django Girls blog</title>
    </head>
    <body>
        <div>
            <h1><a href="">Django Girls Blog</a></h1>
        </div>


    </body>
</html>

I still think you have an empty database. Have you even checked the database file size of the database file on PythonAnywhere vs the file size of the database file that you're comparing against?

The file size of the database file on PythonAnywhere is:

    db.sqlite3        2016-07-15 19:06    39.0 KB

The file size of the database from my local directory is:

    -rw-r--r--  1 pi pi 39936 Jul 15 14:45 db.sqlite3

The size is same, not empty.

OK, that all looks OK. What do you get if you start a bash console, then:

cd /home/djju1029/my-first-blog
./manage.py shell

...then, in the Python shell that appears, run

from blog.models import Post
Post.objects.all().count()

from django.conf import settings
settings.DATABASES

The result is

14:55 ~ $ 
14:55 ~ $ cd my-first-blog
14:55 ~/my-first-blog (master)$ source myvenv/bin/activate
(myvenv) 14:55 ~/my-first-blog (master)$ ./manage.py shell
Python 3.5.1 (default, Dec 18 2015, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from blog.models import Post
>>> Post.objects.all().count()
0
>>> from django.conf import settings
>>> settings.DATABASES
{'default': {'PORT': '', 'USER': '', 'HOST': '', 'PASSWORD': '', 'TEST': {'NAME': None, 'COLLATION': None, '
CHARSET': None, 'MIRROR': None}, 'OPTIONS': {}, 'ENGINE': 'django.db.backends.sqlite3', 'ATOMIC_REQUESTS': F
alse, 'CONN_MAX_AGE': 0, 'NAME': '/home/djju1029/my-first-blog/db.sqlite3', 'AUTOCOMMIT': True, 'TIME_ZONE':
 None}}
>>>

OK, so that's saying pretty explicitly that there are no posts in your database. I take it from what you've said before that you're seeing posts on the page when you run your website on your local machine. Is that right? If so, all I can imagine is that you've uploaded the wrong SQLite file.

Yes, I am seeing all posts on the page by my local machine. I have pushed everything to github and pull all from github to maintain same source. Before using django template in the HTML, every posts is shown as same as my local server. But after using django template, the django template part is not shown. Is it problem of .gitignore file? the .gitignore is:

*.pyc
__pycache__
myvenv
db.sqlite3
/static
.DS_Store

When you did Post.objects.all().count() , it is showing that you have no posts. Can you double check if the db.sqlite3 is empty?

@djju1029 your gitignore would have prevented the db.sqlite3 file from being committed, so that may be the source of the problem.

@ EndenDragon, as I wrote above, the db.sqlite3 file is 39.0KB as same as in my local server. I think db.sqlite3 isn't empty because the local server webpage has no problem be shown.

@glenn, do you mean I have to get rid of db.sqlite3 from the .gitignore? The items of .gitignore are untracked and submitted entirely always when I commit as I know, aren't they?

@glenn, Yes I have compared source by source, one by one, the source in PythonAnywhere Files tab by the source in my local directory. No difference. But as you can see above, the django template parts {% %} are entirely not rendered in only Web seeing from Web tab, in any browser such as firefox, IE, and chrome.

I think you local server is not using that file, but a different one. First find which file is being used on your local machine.

About gitignore: If you want to commit the the file into git, then you need to remove it from the gitignore

Just a note -- the Django Girls tutorial, which is looks like you're following, deliberately says that you should put the SQLite database in .gitignore. This is specifically so that the database you have on your local machine is different to the one on the deployed site.

The reasoning behind that is that your local machine is where you build new features and experiment with layouts and code, so the posts you put into your database there will be test posts that you design to exercise the new features as you develop them. The deployed site on PythonAnywhere, by contrast, is a live publicly-visible site, so it's meant to have posts for other people to come and read.

So, the idea is that when you deploy, you get a fresh database which you can put new public posts into using the Django admin interface. The database library code creates a new empty database for you when you first visit the website, and Django populates it with the structure of the tables for your site when you run the "migrate command".

I think the fact that the file size is the same on your local machine as it is on PythonAnywhere is a red herring. Database management systems tend to create files of specific sizes for reasons related to their own internal data management code. So it's entirely possible that an empty database that only contains the data structures required by Django for your website would be exactly the same size on disk as a database with the data structures plus a few short posts.

I had this exact issue on the same tutorial. Deleting the sql database entry from .gitignore fixed it.

I double checked, and the tutorial doesn't tell you to do this, so I don't know how they wanted the issue to be fixed...

The "correct" way of getting it to work is to keep the sqlite database in gitignore, and recreate a new set of data on the server side (at PythonAnywhere). This is intentional. Re-read Giles' explanation above about why this is good/"correct".

On the other hand, whatever floats your boat :D

After reading all these insights and implementing them what worked for me is I deleted the db.sqlite3(40KB) file on PythonAnywhere's file set. and uploaded the db.sqlite3 file I had on my local machine (the size was 136KB) and voila the site just worked with all the data I had added to my html.

Right, that will work. But normally people keep separate databases locally and on PythonAnywhere, at least for live sites with real users -- this allows you to (for example) have test blog content on your local machine that you've entered in order to debug problems, while having the real blog available on the public Internet.