Create Blog Post Models With Django 3.1 - Django Tutorial 2020
Create Blog Post Models With Django 3.1 - Django Tutorial 2020
(;
/012#02#.%#3%-)#455%-2.#6$-../0..1-2$7#8%)#3%-)#9()*%24&0:(;#(<9()0(25(=
3*"+4"&5#20&6274&82)"#7
9%4,&:;+<02&=1>&?&:;+<02
@A42*%+#&BCBC
/(&@0#+,;())4>0@ A(5#BC#"D"D · E#@02#)(4;
In this article, we will see how to create a post model for our blog
website. where we can add authors for our website, di8erent
categories of our posts, and all stu8 related to the blog post.
As you see here, I’m working on Windows this time, I’m using
Vscode for this tutorial. And we have here our project with
everything installed and ready to go.
!"#$%#&'&(#)&'**
So, the Brst thing I’ll do is to make sure our virtual env is
activated.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
'blog', #new
]
In the project folder, We will include the blog URLs Ble, this way,
Django will redirect everything that comes into
‘http://127.0.0.1:8000/blog' to blog.urls and looks for
instructions from there.
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("pages.urls")),
path("blog/", include("blog.urls")), #new
]
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT) #For media files
def blog(request):
context = {}
!"#$%#&%+#&,-.#/0
Okay, we are ready to create our model:
User = get_user_model()
class Author(models.Model):
user = models.OneToOneField(User,
on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to="")
def __str__(self):
return self.user.username
class Category(models.Model):
title = models.CharField(max_length=20)
def __str__(self):
return self.title
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
class Post(models.Model):
title = models.CharField(max_length=200)
slug = AutoSlugField(populate_from='title')
overview = models.TextField()
date = models.DateTimeField(auto_now_add=True)
content = models.TextField()
author = models.ForeignKey(Author,
on_delete=models.CASCADE)
categories = models.ManyToManyField(Category)
published = models.BooleanField()
def __str__(self):
return self.title
1+$%&)#&.2.&+#"#3
We start with deBning our model object, we give it a simple name,
Post.
...
slug = AutoSlugField(populate_from='title')
And we use it this way, and the populate from to create a slug
based on that title.
Also, we can add an overview for the post, it will give the readers
a taste or teaser of a full blog post and it will be a text Beld, which
means there’s no character limit.
And we need date Beld to show readers and visitors when we
created that post. With the option of auto_now_add set to True,
which means the data will be added automatically the moment
you create the post.
For the content, we will use the text Beld too as well as the
overview Beld.
We will also add an image Beld to this model, so each other can
have its proBle picture.
We added the upload to option, in case you want to save images
you upload in other location instead of our default media folder.
And for that, I’ll create a model class for the Category, it will hold
a title with a max of 20 characters.
Again the str method to display the title instead of the category
object.
I think we can add a boolean Beld too, to choose whether our post
will be a draft or we can publish it.
4.2%&$.,256*7&82/#
now, to add, edit and delete the posts, authors, or categories
we’ve just created in the models Ble, we will add them to the
admin.py Ble.
admin.site.register(Author)
admin.site.register(Category)
admin.site.register(Post)
!-59/:02-5;
So, everything is ready now, The last thing we will do is adding
our models to our database. We will let Django save the changes
of our models.
using:
As you see here, this is the admin dashboard, here we can add,
edit, or delete our posts.