Django博客与图片

时间:2017-02-15 08:09:41

标签: django django-templates blogs

我在我的数据库中有这个

1款

款2

款2

我想在段落之间放置一张图片。

我试过这个

<img src="where/photo/is">

class Post(models.Model):
STATUS_CHOICES = (
    ('draft','Draft'),
    ('published', 'Published'),
)

title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date ='publish')
author = models.ForeignKey(User, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
image = models.ImageField(upload_to='posts', default='path/to/my/default/image.jpg')
status = models.CharField(max_length=10, choices=STATUS_CHOICES,
    default='draft')
objects = models.Manager()
published = PublishedManager()
tags = TaggableManager()

class Meta:
    ordering = ('-publish',)

def __str__(self):
    return self.title

def get_absolute_url(self):
    return reverse('blog:post_detail',
            args=[self.slug])

注意:这是以段落中的数据获取的帖子表,中间没有图片,我想在获取此文本正文后添加它。

这是我的观点

current_site = get_current_site(request)
namesite = current_site.name
domain = current_site.domain

post = Post.published.get(slug=post)

profile = Profile.objects.get(id=1)


if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        #create Comment object but do not say to database just yet
        new_comment = comment_form.save(commit=False)
        #Assigng the current post to the comment
        new_comment.post = post
        #save the comment to the database
        new_comment.save()
        comment_form = CommentForm()
else:
    comment_form = CommentForm()

return render(request, 'blog/detail.html',
                {'post': post, 'comments': comments, 
                 'comment_form': comment_form, 'namesite': namesite, 'domain': domain, 'profile': profile })

在数据库中我使用|安全过滤器,但仍然打印与上面相同。

你知道如何进行这种行为吗?

1 个答案:

答案 0 :(得分:0)

嗨如果你想用图像创建长描述意味着你可以使用django-ckeditor link,这个包将为你提供RichTextField,这样你就可以用图像创建批量段落。