Building a blog.

Builiding blog was fun, super fun. Django is extremely user-friendly and requires basic Python knowledge and a lot of common sense. There was a lot of satisfation of building something absolutely from scratch. Nothing like Wordpress! Knowing that makes it more exciting to put anything here.

Firstly, let's have a look on models I used in models.py:

from django.db import models
from ckeditor.fields import RichTextField

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    body = RichTextField()            #ckeditor field
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
    def snippet(self):
        return self.body[:80] + '...'  #defining snippet for \
#article list subsite so that it displays 80 char.

As you can see body field uses ckeditor field; that's the only thing that I didn't write myself. It's extremely useful and awesome widget that transforms your textfield into HTML machine, just have a look on below before/after deploying this beast. Admin site:

 

You can check ckeditor for django on github. Thank you ckeditor team !

Now let's see views.py:

def blog(request):             #Blog website with only 3 articles
    articles5 = Article.objects.all() 
    articles5 = articles5.order_by('-date')[:3]
    return render(request,'blog.html',{'articles':articles5})
def article_id(request,slug):    #Post website with full body
    articleid = Article.objects.get(slug=slug)
    return render(request, 'blogid.html', {'article':articleid})
def allarticles(request):        #Blog website with all articles listen
    articles5 = Article.objects.all()
    articles5 = articles5.order_by('-date')
    return render(request,'blog.html',{'articles':articles5})

I had to define 3 views for blog: list of 3 articles with limited body(see models), all articles and individual article view.

Now urls.py:

 urlpatterns = [
    url(r'^$', views.blog, name = 'blog'),
    url(r'^all/$', views.allarticles, name = 'allarticles'),
    url(r'^(?P<slug>[\w-]+)/$', views.article_id, name = 'article_id'),    
   ]

We are getting slug of the article that I define in admin site every time I post the article. This directs django to article.view and connects slug from url to slug from database of articles; pretty straight-forward.

Lastly templates : 

List of articles: we are extending header html website, sorry I'm not gonna post full code as it is too long.

{% for article in articles %} <!-- for loop to generate articles  -->
          <div class="post-preview">
            <a href="{% url 'article_id' slug=article.slug %}">
              <h3 class="post-title" style="font-weight:400">
                {{article.title}}
              </h2>
              <h3 class="lead">
                {{article.snippet|safe}} <!--article snippet mentioned above,
# also see that we are applying "safe" filter so that we can post HTML code -->
              </h3>
            </a>
            <p class="post-meta">Posted by
              <a href="/about/">Adam</a>
              on {{article.date}}</p>
          </div>
          <hr>
{% endfor %}
 
{% if request.path == '/blog/all/' %}      <!-- requests current path and generates button 
#"show all" for only 3 article view. 
#We don't need two templates as we are getting this sorted in views.py (see above) -->

{% else %}
          <div class="clearfix">
            <a class="btn btn-secondary float-right" href="/blog/all/">Show All &rarr;</a>
          </div>
        </div>
      </div>
    </main>    
{% endif %}
</body>

{% endblock %}

Article ID:

<div class="post-preview">
              <h3 class="post-title" style="font-weight:400">
                {{article.title}}
              </h2>
              <h3 class="lead" style="text-align:left">
                {{article.body|safe}}
               
              </h3>
            </a>
            <p class="post-meta">Posted by
              <a href="/about/">Adam</a>
              on {{article.date}}</p>
          </div>

 

That would be everything. Please get in touch if you have any questions .