Skip to content

Commit

Permalink
category added to this project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abolfazlms committed May 18, 2024
1 parent 3cc3586 commit 4885a0a
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 10 deletions.
4 changes: 3 additions & 1 deletion blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from blog.models import Post
from blog.models import Post, Category

# Register your models here.
class PostAdmin(admin.ModelAdmin):
Expand All @@ -11,4 +11,6 @@ class PostAdmin(admin.ModelAdmin):
list_filter = ('status','author')
#ordering = ['created_date']
search_fields = ['title','content']

admin.site.register(Category)
admin.site.register(Post, PostAdmin)
18 changes: 18 additions & 0 deletions blog/migrations/0005_post_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-05-18 17:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_post_author'),
]

operations = [
migrations.AddField(
model_name='post',
name='image',
field=models.ImageField(default='blog/default.jpg', upload_to='blog/'),
),
]
25 changes: 25 additions & 0 deletions blog/migrations/0006_auto_20240518_2108.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.25 on 2024-05-18 17:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0005_post_image'),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
],
),
migrations.AddField(
model_name='post',
name='category',
field=models.ManyToManyField(to='blog.Category'),
),
]
10 changes: 7 additions & 3 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name

class Post(models.Model):
author = models.ForeignKey(User,on_delete=models.SET_NULL,null=True)#models.ForeignKey(User,on_delete=models.CASCADE)
#image
image = models.ImageField(upload_to='blog/',default='blog/default.jpg')
title = models.CharField(max_length = 255)
content = models.TextField()
#tags
#category
category = models.ManyToManyField(Category) # it's equal to category = models.ManyToManyField(Category,null=True)
counted_views = models.IntegerField(default = 0)
status = models.BooleanField(default = False)
published_date = models.DateTimeField(null = True)
Expand All @@ -21,5 +26,4 @@ class Meta:

def __str__(self):
return self.title


2 changes: 1 addition & 1 deletion blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
app_name = 'blog'
urlpatterns = [
path('', blog_view, name = 'index'),
path('<int:pid>', blog_single, name = 'single'),
path('<int:pid>/', blog_single, name = 'single'),
# path('post-<int:pid>', test, name = 'test'),
]
Binary file added media/blog/cat-widget3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/blog/d1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/blog/default.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified requirements.txt
Binary file not shown.
11 changes: 6 additions & 5 deletions templates/blog/blog-home.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ <h4 class="content-title mx-auto text-uppercase">Food</h4>
<div class="single-post row">
<div class="col-lg-3 col-md-3 meta-details">
<ul class="tags">
<li><a href="#">Food,</a></li>
<li><a href="#">Technology,</a></li>
<li><a href="#">Politics,</a></li>
<li><a href="#">Lifestyle</a></li>
<!-- {%for cat in post.category.all%}
<li><a href="#">{{cat.name}}</a></li>
{%endfor%} -->
{{post.category.all|join:", "}}

</ul>
<div class="user-details row">
<p class="user-name col-lg-12 col-md-12 col-6"><a href="#">{{post.author.get_full_name}}</a>
Expand All @@ -111,7 +112,7 @@ <h4 class="content-title mx-auto text-uppercase">Food</h4>
</div>
<div class="col-lg-9 col-md-9 ">
<div class="feature-img">
<img class="img-fluid" src="{% static 'img/blog/feature-img1.jpg' %}" alt="">
<img class="img-fluid" src="{{post.image.url}}" alt="">
</div>
<a class="posts-title" href="{% url 'blog:single' pid=post.id%}">
<h3>{{post.title}}</h3>
Expand Down

0 comments on commit 4885a0a

Please sign in to comment.