-1

I am currently working on my first django project "not following a tutorial". I'm creating a blog and trying to get all my pictures & post data to import into post. I am getting some info to import, but not all. Specifically, my images and date are not getting populated into post.

views.py --------------------------------

     def home(request):
        context= {
            'posts' : Post.objects.all(),
            'comment': Comment.objects.all(),
        }
        return render(request, 'blog/home.html', context)


    def base(request):
        return render(request, 'blog/test.html')

class ArticleDetailView(DetailView):
    model = Post
    template_name = 'blog/standard.html'

------ my html page

 {% extends 'blog/base.html' %}
{% load static %}

{% block content %}
        <section class="main-content">
            <div class="padding">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-8">
                            <div class="single-post">
                                <div class="type-standard">
                                    <article class="post type-post">
                                        <div class="top-content text-center">
                                            <span class="category"><a href="categories.html">{{ post.category }}</a></span><!-- /.category -->
                                            <h2 class="entry-title"><a href="standard.html">{{ post.title }}</a></h2><!-- /.entry-title -->
                                            <span class="time"><time>{{ post.date_posted|date:"F d, Y" }</time></span><!-- /.time -->
                                        </div><!-- /.top-content --> 

                                        <div class="entry-thumbnail"><img src="{post.image.url }"></div><!-- /.entry-thumbnail -->

                                        <div class="entry-content">

                                            {{ post.content }}

                                            <div class="post-meta">
                                                <span class="comments pull-left"><i class="icon_comment_alt"></i> <a href="#">4 Comments</a></span><!-- /.comments -->
                                                <span class="post-social pull-right">
                                                    <a href="#"><i class="fa fa-instagram"></i></a>
                                                    <a href="#"><i class="fa fa-facebook"></i></a>
                                                    <a href="#"><i class="fa fa-twitter"></i></a>
                                                    <a href="#"><i class="fa fa-pinterest-p"></i></a>
                                                </span><!-- /.post-social -->
                                            </div><!-- /.post-meta -->
                                        </div><!-- /.entry-content -->
                                    </article><!-- /.post -->
                                </div><!-- /.type-standard -->

                                <div class="author-bio media">
                                    <div class="author-avatar media-left pull-left"><img class="img-circle" src="http://demos-jeweltheme.ipunu91y.maxcdn-edge.com/sasha/images/single/au.jpg" alt="Avatar"></div><!-- /.author-avatar -->
                                    <div class="author-details media-body">
                                        <h3 class="name"><a href="#">{{post.author}}</a></h3>
                                        <p>
                                            Donec sed elementum mi. Duis mattis dolor ante, eleifend dapibus ipsum pellentesque venenatis. Nullam elementum ligula et rhoncus interdum. In malesuada 
                                        </p>
                                        <div class="social">
                                            <a href="#"><i class="fa fa-twitter"></i></a>
                                            <a href="#"><i class="fa fa-pinterest-p"></i></a>
                                            <a href="#"><i class="fa fa-facebook"></i></a>
                                            <a href="#"><i class="fa fa-google-plus"></i></a>
                                            <a href="#"><i class="fa fa-instagram"></i></a>
                                        </div><!-- /.social -->
                                    </div><!-- /.author-details -->
                                </div><!-- /.author-bio -->

                                <nav class="post-navigation" role="navigation">
                                    <div class="nav-links prev pull-left">
                                        <span class="meta-nav"><i class="fa fa-angle-left"></i></span>

                                        <article class="post type-post media">
                                            <div class="entry-thumbnail media-left pull-left">
                                                <a href="image.html"><img src="http://demos-jeweltheme.ipunu91y.maxcdn-edge.com/sasha/images/single/n1.jpg" alt="Thumb Image"></a>
                                            </div><!-- /.entry-thumbnail -->
                                            <div class="entry-content media-body"> 
                                                <h3 class="entry-title"><a href="image.html">Cruise to Alaska</a></h3>
                                                <span class="time"><time datetime="2017-12-05">May 12, 2017</time></span><!-- /.time -->
                                            </div><!-- /.entry-content -->
                                        </article>
                                    </div><!-- .nav-links -->

                                    <div class="nav-links next pull-right">
                                        <span class="meta-nav"><i class="fa fa-angle-right"></i></span>
                                        <article class="post type-post media text-right">
                                            <div class="entry-thumbnail media-left pull-right">
                                                <a href="gallery.html"><img src="http://demos-jeweltheme.ipunu91y.maxcdn-edge.com/sasha/images/single/n2.jpg" alt="Thumb Image"></a>
                                            </div><!-- /.entry-thumbnail -->
                                            <div class="entry-content media-body">
                                                <h3 class="entry-title"><a href="gallery.html">Cruising into Spring</a></h3>
                                                <span class="time"><time datetime="2017-12-05">May 12, 2017</time></span><!-- /.time -->
                                            </div><!-- /.entry-content -->
                                        </article>        
                                    </div><!-- .nav-links -->
                                </nav><!-- /.post-navigation -->

setting.py

  STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'

STATICFILES_DIRS= [
    os.path.join(BASE_DIR, 'static')
]

My URL link

urlpatterns = [
   path('', views.home, name='blog-home' ),
   path('base/', views.base, name='blog-base' ),
   path('contact/', views.contact, name='contact' ),
   path('deals/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),


]

models.py ---------------------------

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    category = models.CharField(max_length=100)
    featured = models.BooleanField()
    image = models.ImageField(default='2.jpg', upload_to='media')

    def __str__(self):
        return self.title

2 Answers2

0

You are getting all the posts from the database, but in your html you refer to "post".

To show all the posts your html should be like:

{% for post in posts %}
    <h1>{{ post.title }}</h1>
    ....
    ....
{% endfor %}
  • I am getting the post specific data though pk when the url is clicked. I updated the code above to show my url patterns – Natthan Davis May 22 '20 at 03:17
0

Your code is wrong.

 <span class="time"><time>{{ post.date_posted|date:"F d, Y" }}</time></span>

And if I'm not mistaken for the img is:

 <div class="entry-thumbnail"><img src="{{ post.image.url }}"></div>
  • Hey, thanks for the help! It worked with the date, but not with the photos. This does work on my front page, but not within the post page. – Natthan Davis May 22 '20 at 18:32
  • Do you pass the id of the post to your post page? I also don't know how you declared the post in models.py – Giorgos Kavalieratos May 23 '20 at 09:07
  • I updated my code above to include the models.py. Looking in my terminal, I am getting the get request "GET /deals/media/media/17-172367". This is weird because everything else has the right path, but my images. – Natthan Davis May 23 '20 at 15:41
  • You should also add the MEDIA_ROOT to your urls. see this: https://stackoverflow.com/questions/42355538/django-imagefield-url-on-html – Giorgos Kavalieratos May 24 '20 at 07:58