count the number of posts - django

I have created a website and want to count user generated posts.
I tried to do it as follows:
models.py
class Blog(models.Model):
user = models.ForeignKey(
User,
related_name='user_blogs',
on_delete=models.CASCADE
)
category = models.ForeignKey(
Category,
related_name='category_blogs',
on_delete=models.CASCADE
)
title = models.CharField(
max_length=250
)
slug = models.SlugField(null=True, blank=True)
banner = models.ImageField(upload_to='blog_banners')
description = RichTextField()
created_date = models.DateField(auto_now_add=True)
def __str__(self) -> str:
return self.title
def save(self, *args, **kwargs):
updating = self.pk is not None
if updating:
self.slug = generate_unique_slug(self, self.title, update=True)
super().save(*args, **kwargs)
else:
self.slug = generate_unique_slug(self, self.title)
super().save(*args, **kwargs)
templates
<span>Posts: <strong>{{account.user_blogs.count}}</strong></span>
but it doesn't work
what is the problem? should I rewrite the code at all?

If you're trying to count posts created by the logged-in user
<span>Posts: <strong>{{ request.user.user_blogs.count }}</strong></span>

Related

How can I filter This in django?

I am working on a Django project (like LMS) and I want to filter Lectures by Subject in the template, I do it once but now I forgot how to do it. How can I do this?!
I want to filter it by foreign-key Subject in Lecture
Here is my models.py
from django.db import models
from django.utils.text import slugify
# Create your models here.
class Subject(models.Model):
Title = models.CharField(max_length=150)
Pic = models.ImageField(upload_to='media/')
Test = models.CharField(max_length=300, null=True, blank=True)
Slug = models.SlugField(unique=True, null=True, blank=True)
def save(self, *args, **kwargs):
self.Slug = slugify(self.Title, allow_unicode=True)
super(Subject, self).save(*args, **kwargs)
def __str__(self):
return self.Title
class Lecture(models.Model):
Subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
Title = models.CharField(max_length=150)
Video = models.CharField(max_length=300)
More_Info = models.TextField()
Audio_Lecture = models.CharField(max_length=300)
Lecture_Files = models.CharField(max_length=300)
Sheet = models.CharField(max_length=300)
Slug = models.SlugField(unique=True, null=True, blank=True)
def save(self, *args, **kwargs):
self.Slug = slugify(self.Title, allow_unicode=True)
super(Lecture, self).save(*args, **kwargs)
def __str__(self):
return self.Title
and here is my views.py
from Diploma.models import Lecture, Subject
from django.shortcuts import render
# Create your views here.
def Diploma(request):
return render(request, 'Diploma/diploma.html', context={
'sub' : Subject.objects.all(),
})
def Diploma_Subject(request, slug):
subject_Lecture = Subject.objects.all()
return render(request, 'Diploma/subject.html', context={
'subj' : subject_Lecture,
'lect' : Lecture.objects.filter(I do not know what I should write here)
})
If you want to filter the Lecture model based on the slug parameter you have to do the following:
lecture = Lecture.objects.filter(Slug=slug).first()
if lecture is not None:
# lecture.Subject, lecture.Title ...
Or if you want to filter based on the slug property in Subject model you can do the following:
lectures = Lecture.objects.filter(Subject__Slug=slug)
for lecture in lectures:
# lecture.Subject, lecture.Title ...
empty:
print('There are any lectures related')
There are many things to explain here but I tried to give you a minimal repoducible example so you can custom it to achieve your goal.
Feel free to leave a comment if you have an specific doubt about understanding it.

Django: using __str__ with a foreignkey in order to return an item

I am simply trying to override the str method to return something from a foreign key. A snippet of my models is as follows:
class Product(models.Model):
name = models.CharField(max_length=120)
shortened_name = models.CharField(max_length=50)
date_of_creation = models.CharField(max_length=20, null=True)
history = models.CharField(max_length=255)
price = models.DecimalField(max_digits=9, decimal_places=2)
style = models.CharField(max_length=50)
artist = models.ForeignKey(Artist, on_delete=models.SET_NULL, null=True)
image = models.ImageField(upload_to='images', null=True)
slug = models.SlugField()
def get_absolute_url(self):
return reverse('product', args=[self.slug])
def save(self, *args, **kwargs):
self.slug = slugify(self.shortened_name)
super().save(*args, **kwargs)
def get_add_to_cart_url(self):
return reverse('add_to_cart', kwargs={
'slug': self.slug
})
class ShoppingCartOrderItem(models.Model):
item = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
def __str__(self):
return self.product.name
After adding the item to my shopping cart, in the admin panel when trying to view the specific item I get an AttributeError: 'ShoppingCartOrderItem' object has no attribute 'product'. Based on my googling so far I don't see why this isn't working. I have even tried str(self.product.name) but still the same error produces. I am unsure what I am doing wrong. Any help or suggestions is greatly appreciated!

Django Public user profile + user's posts

I hope you're well. I'm beginning with Django.
I'd like to create - like facebook - a public profile. I've already created a UserProfileUpdateView with country, adresse, image, ...
When a user post something I'd like to have a link to his public profile (country, adresse, image, ... + posts):
class UserPostView(ListView):
template_name = 'user_post.html'
model = Post
context_object_name = 'posts'
def get_context_data(self, **kwargs):
context = super(UserProfileView, self).get_context_data(**kwargs)
context['userprofile'] = UserProfile.objects.get(user=self.request.user)
return context
def get_queryset(self):
return Post.objects.filter(user=self.kwargs['pk'])
A - I'd like to display the public profile link with username (which is unique) and not with a number. Does anyone has an idea about how I can solve this?
path('<int:pk>/',UserPostView.as_view(),name="user_posts"),
UserProfile (user app)
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
street = models.CharField(null=True,blank=True,max_length=300)
number_street = models.CharField(null=True,blank=True,max_length=20)
street_detail = models.CharField(null=True,blank=True,max_length=300)
town = models.CharField(null=True,blank=True,max_length=60)
zipcode = models.CharField(null=True,blank=True,max_length=20)
country = models.CharField(null=True,blank=True,max_length=60)
image = models.ImageField(null=True,blank=True,default='user/user-128.png', upload_to='user/')
slug = models.SlugField(editable=False)
def save(self, *args,**kwargs):
self.slug = slugify(self.user.username)
super(UserProfile, self).save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 200 or img.width > 200:
new_size = (200, 200)
img.thumbnail(new_size)
img.save(self.image.path)
def __str__(self):
return self.user.username
Post (nutriscore app)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
url_image = models.URLField(max_length=200, default=None)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = unique_slugify(self, slugify(self.title))
super().save(*args, **kwargs)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
If the user in the Post model is a foreign key and is from django.contrib.auth.models.User, then first scan the User table from username and then scan the UserProfile table using the id as follows
user = User.objects.filter(username=name).values()
user_id = user[0]['id'] # You get the id of the user
userprofile = UserProfile.objects.filter(user_id=userprofile).values() # Scan the UserProfile table using the id obtained above
user_post = Post.objects.filter(user = user_id) # post by authenticated user

Slugify self.title + random numbers

I hope you're well. I've two questions for you:
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
url_image = models.URLField(max_length=200, default='SOME STRING')
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
name_site = models.CharField(max_length=200, default='NA')
url_site = models.URLField(max_length=200, default='https://exemple.fr/')
content = models.TextField()
I. I want my title (unique=False) because I have some similar titles. So is it possible to save my slug (editable=False) with slugify with something like that:
slug_str = "%s %s" % (self.title, 4 random numbers like that 0476)
If anyone has a better idea, I'm interested in
Thanks a lot :) have a good holidays and take care
Here are a couple functions that I use. You pass in the model instance and the desired title into unique_slugify which will continue trying to add random strings until one is created that doesn't already exist.
import random
import string
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def unique_slugify(instance, slug):
model = instance.__class__
unique_slug = slug
while model.objects.filter(slug=unique_slug).exists():
unique_slug = slug
unique_slug += random_string_generator(size=4)
return unique_slug
I usually use it by overriding the model save method.
class YourModel(models.Model):
slug = models.SlugField(max_length=200, unique=True)
title = models.CharField(max_length=200)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = unique_slugify(self, slugify(self.title))
super().save(*args, **kwargs)
in my opinion, it doesn't make sens two posts may have the same title even with two different slugs, it even confuses readers and even worst it's very bad for SEO. i suggest you to avoid this path and try to review the logic
in models.py
class Post(models.Model):
title = models.CharField(_('title'), max_length=200,
unique=True, # title should be unique
help_text=_('The title of the entry.')
)
slug = models.SlugField(_('slug'), max_length=200,
unique=True, null=True, blank=True,
help_text=_(
'If blank, the slug will be generated automatically '
'from the given title.'
)
)
[..]
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title) # here you don't need to add random numbers since the title is already unque
super().save(*args, **kwargs)
in admin.py
class PostAdmin(admin.ModelAdmin):
list_display = ('title', .. )
readonly_fields = ('slug', .. ) # Add slug to read_only fields to make it not editable if you want

How to display two not associated models on one page via admin interface?

I have two models, not connected by ForeignKey. I would to display them on one page in admin interface, in the way like inlines in Django.
I can't associate PostFile with Post, because uploaded medias should be available to every Post which was and will be created in future. I have no idea what I can do if inlines are unavailable.
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published')
)
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique_for_date='publish_date', blank=True)
author = models.ForeignKey(User, related_name='blog_posts', on_delete=models.CASCADE, blank=True)
content = MarkdownxField()
publish_date = models.DateTimeField(default=timezone.now)
creation_date = models.DateTimeField(auto_now_add=True)
last_update_date = models.DateField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
tags = models.ManyToManyField(Tag)
objects = models.Manager()
class Meta:
ordering = [
'-publish_date'
]
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[
self.publish_date.strftime('%Y'),
self.publish_date.strftime('%m'),
self.publish_date.strftime('%d'),
self.slug
])
#https://github.com/neutronX/django-markdownx/issues/83
def formatted_markdown(self):
return markdownify(self.content)
class PostFile(models.Model):
file_name = models.CharField(max_length=200, blank=True)
file_object = models.FileField(upload_to='post_files/')
file_upload_date = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
if not self.file_name:
self.file_name = self.file_object.name
super(PostFile, self).save(*args, **kwargs)
def file_url(self):
return self.file_object.url
def __str__(self):
return self.file_name
I would to receive in output a Post model admin page, where on the bottom I have listed all PostFile objects. This will give me easy and fast acces to PostFile-object url on media folder. I don't want each time to open new tab, and to go to PostFile model admin page to check url of object.