Hello All,
I have created the two models to create post on desired topic, as well as to get the comments from the visitors. But, I am not able to figure out, how to connect that model to my HTML page.
I know how to create a static HTML pages for each topic,and write page content in <p></p> tags. which I have been thinking to do until now, but it seems to be a time taking and non-productive way. So, now I would like to write posts in my database model which I have registered in admin.py.
Any idea, how can I achieve this, as this would be a like generating dynamic HTML pages based on models.
I am not sure if I was able to explain my requirements. May be as I am not getting right words to express them. Please ask me questions if any confusion, I will try to answer them.
Below is my models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
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', on_delete=models.CASCADE)
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return self.email