2016-06-07 110 views
-2

我是django的新手。django項目中沒有輸出

我寫了一個叫做博客的簡單的django應用程序,每一件事都是正確的。但是當我運行時沒有超時。

這是我的代碼。

model.py

from django.db import models 

class Blog_post(models.Model): 
    title = models.CharField(max_length=250) 
    body = models.TextField(max_length=2000) 
    date = models.DateTimeField() 
    auther = models.CharField(max_length=250) 


    def __str__(self): 
     return self.title 

view.py

from django.shortcuts import render 
from .models import Blog_post 

def index(request): 
    posts = Blog_post.objects.all() 
    context = {'posts': posts} 
    return render (request, 'blog/index.html', context) 

的index.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>My Blog</title> 
</head> 
<body> 
<h1>Hello World </h1> 
    {{x}} 

    {% for post in posts %} 
     {{title}} 

    {% endfor %} 
</body> 
</html> 

urls.py

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^$', views.index, name = 'index') 
] 
+1

你是什麼意思與沒有輸出?您無法使用瀏覽器訪問該應用程序?你看不到任何登錄終端? –

+0

@BlueSun,index.html文件中有兩個錯誤。他們是{{x}}和{{title}} –

+0

謝謝,幫助 – BlueSun

回答

0

如果你想看到的博客文章標題:

更換

<!DOCTYPE html> 
<html> 
<head> 
    <title>My Blog</title> 
</head> 
<body> 
<h1>Hello World </h1> 
    {{x}} 

    {% for post in posts %} 
     {{title}} 

    {% endfor %} 
</body> 
</html> 

有了:

<!DOCTYPE html> 
<html> 
<head> 
    <title>My Blog</title> 
</head> 
<body> 
<h1>Hello World </h1> 
    {{x}} 

    {% for post in posts %} 
     {{ post.title }} 

    {% endfor %} 
</body> 
</html> 
+0

感謝您的幫助 – BlueSun