Django不会从模型中显示文本字段

时间:2017-08-08 18:15:11

标签: python django

我有一个登录系统,您需要登录才能提交新帖子。我的新帖子"页面/表单工作正常,用户提交的内容已正确发布到数据库中,如何在主页上显示该内容时仅显示标题和副标题(即Charfields),并且没有帖子的正文(这是一个文本字段)。

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 个答案:

答案 0 :(得分: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将无声地失败任何无法在模板中评估的表达式,这就是为什么没有显示的原因。