2017-08-08 73 views
0

的文本字段我有一个登录系统,您需要登录后才能提交新帖子。我的“新邮政”页面/表格工作正常,用户提交的内容在数据库中正确发布,如何在主页上显示该内容时只显示标题和副标题(即Charfields),而没有帖子的正文文本(这是一个文本字段)。Django将不会显示来自型号

inde.html

{% extends "blog/base.html" %} 
{% block body_block %} 


    <div class="left"> 
     {% if userposts %} 
      {% for posts in userposts %} 
       <div class="front-post"> 
        <h2 class="post-title">{{ posts.post_title }}</h2> 
        <h3 class="post-sub-title">{{ posts.post_sub_title }}</h3> 
        <p class="post-author">{{ post.post_author }}</p> 
        <p class="post-date">{{ post.post_date }}</p> 
        <p class="post-body">{{ post.post_body }}</p> 
       </div> 
      {% endfor %} 
     {% endif %} 
    </div> 


    <div class="right"> 
     <p>SIDE BAR</p> 
    </div> 


{% endblock %} 

views.py

from django.shortcuts import render 
from blog.forms import UserForm,UserProfileInfoForm,AddPost 
from blog.models import UserPosts 

from django.contrib.auth import authenticate,login,logout 
from django.http import HttpResponseRedirect,HttpResponse 
from django.core.urlresolvers import reverse 
from django.contrib.auth.decorators import login_required 

# Create your views here. 
def index(request): 
    post_list = UserPosts.objects.order_by('post_date') 
    post_dict = {'userposts':post_list} 

    return render(request, 'blog/index.html',context=post_dict) 

models.py

from django.db import models 
from django.contrib.auth.models import User 

# Create your models here. 
class UserProfileInfo(models.Model): 
    user = models.OneToOneField(User) 

    portfolio_site = models.URLField(blank=True) 
    profile_pic = models.ImageField(upload_to='profile_pics',blank='True') 

    def __str__(self): 
     return self.user.username 


class UserPosts(models.Model): 
    post_title = models.CharField(max_length=100,unique=True) 
    post_sub_title = models.CharField(max_length=250,unique=False) 
    post_author = '' 
    post_date = models.DateField(auto_now=True) 
    post_body = models.TextField(max_length=1000,unique=False) 

    def __str__(self): 
     return str(self.post_title) 

forms.py

from django import forms 
from django.contrib.auth.models import User 
from blog.models import UserProfileInfo,UserPosts 

class UserForm(forms.ModelForm): 
    password = forms.CharField(widget=forms.PasswordInput()) 

    class Meta(): 
     model = User 
     fields = ('username','email','password') 

class UserProfileInfoForm(forms.ModelForm): 
    class Meta(): 
     model = UserProfileInfo 
     fields = ('portfolio_site','profile_pic') 

class AddPost(forms.ModelForm): 
    class Meta(): 
     model = UserPosts 
     fields = '__all__' 
+1

它只是一个错字? 'posts.post_sub_title' vs'post.post_body' – PRMoureu

+0

可能是因为您在'for'循环中键入了'post'而不是'posts'? – Mangohero1

回答

2

注意你的命名。您的for循环变量名为posts(末尾有s),但您试图显示post.post_body。在某些地方,因为您使用的是posts.post_title,所以它工作正常。

要解决此问题,请在for循环中将posts重命名为post

{% for post in userposts %} 
    <div class="front-post"> 
     <h2 class="post-title">{{ post.post_title }}</h2> 
     <h3 class="post-sub-title">{{ post.post_sub_title }}</h3> 
     <p class="post-author">{{ post.post_author }}</p> 
     <p class="post-date">{{ post.post_date }}</p> 
     <p class="post-body">{{ post.post_body }}</p> 
    </div> 
{% endfor %} 

Django会失败默默不能在模板中,这也就是为什么,在展示什么评价任何表情。

+0

明白了,谢谢 – Garrett