Tell me how to transfer this code from Html to models - django

There is a code in html {{post.author.posts.count}} that counts the number of posts by the author.
I have such a question, how to transfer it to views.py
def post_detail(request, post_id):
post = get_object_or_404(Post, id=post_id)
context = {
'post': post,
}
return render(request, 'posts/post_detail.html', context)
class model
class Post(models.Model):
text = models.TextField(verbose_name='Текст')
pub_date = models.DateTimeField(
auto_now_add=True,
verbose_name='Дата публикации'
)
group = models.ForeignKey(
Group,
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name='posts',
verbose_name='Группа',
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='posts',
verbose_name='Автор',
)
def __str__(self):
return self.text
I need to find an existing post, find its author and sort all posts by this author and calculate the total number of posts

You can not pass values form HTML to views in Django, but you can create #property to count all posts of the specific author.
you can try like this
models.py
class Post(models.Model):
text = models.TextField(verbose_name='Текст')
pub_date = models.DateTimeField(
auto_now_add=True,
verbose_name='Дата публикации'
)
group = models.ForeignKey(
Group,
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name='posts',
verbose_name='Группа',
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='posts',
verbose_name='Автор',
)
def __str__(self):
return self.text
views.py
def Demoview(request,post_id):
get_post = Post.object.get(id=post_id)
author_of_post = get_post.author
all_post_of_author= Post.object.filter(author__id=get_blog.author.id)
count_all_post_of_author= Post.object.filter(author__id=get_blog.author.id).count()
context = {
'author_of_post':author_of_post,
'all_post_of_author':all_post_of_author,
'count_all_post_of_author':count_all_post_of_author,
}
return render(request,'post.html',context)

Happened. Here is the code that came out.
def post_detail(request, post_id):
post = get_object_or_404(Post, id=post_id)
author = post.author
post_count = Post.objects.filter(author=author).count()
context = {
'author': author,
'post_count': post_count,
'post': post,
}
return render(request, 'posts/post_detail.html', context)

Related

Djnago Form getting Error while edit record

I am getting Issue while edit a record based on CHatquestion ID, if option is null then i need to add a record based on same chatquestion id, if chatqustion id exist in option it will work,
i am trying to multiple way to solve this issue but still can't find solution.
Models.py # thease are all 3 models
class Problem(models.Model):
Language = models.IntegerField(choices=Language_CHOICE, default=1)
type = models.CharField(max_length=500, null=True, blank=True)
def __str__(self):
return self.type
class ChatQuestion(models.Model): # Eding record based on chatquestion id
question = RichTextField(null=True, blank=True)
problem_id = models.ForeignKey(
Problem,
models.CASCADE,
verbose_name='Problem',
)
def __str__(self):
return self.question
is_first_question = models.BooleanField(default=False)
class Option(models.Model):
option_type = models.CharField(max_length=250, null=True, blank=True)
question_id = models.ForeignKey(
ChatQuestion,
models.CASCADE,
verbose_name='Question',
null=True,
blank=True
)
problem=models.ForeignKey(
Problem,
models.CASCADE,
verbose_name='Problem',
null=True,
blank=True
)
next_question_id = models.ForeignKey(ChatQuestion, on_delete=models.CASCADE, null=True, blank=True,
related_name='next_question')
def __str__(self):
return self.option_type
forms.py
class EditQuestionForm(forms.ModelForm):
class Meta:
model = ChatQuestion
fields =('question','problem_id')
class EditOptionForm(forms.ModelForm):
class Meta:
model = Option
fields =('option_type',)
views.py
def question_edit(request,id=None):
if id is not None:
queryset = get_object_or_404(ChatQuestion,pk=id)
queryset1=get_object_or_404(Option,question_id=queryset )
else:
queryset = None
queryset1 = None
if request.method=="POST":
form = EditQuestionForm(request.POST ,instance=queryset)
form1=EditOptionForm(request.POST, instance=queryset1)
if form.is_valid() and form1.is_valid():
question=form.cleaned_data['question']
option_type=form1.cleaned_data['option_type']
if id:
queryset.question=question
queryset.save()
queryset1.option_type=option_type
queryset1.save()
messages.success(request,'Sucessful')
return redirect('/fleet/list_chatbot')
else:
print(form.errors)
messages.error(request,'Please correct following',form.errors)
elif id:
form = EditQuestionForm(instance=queryset)
form1=EditOptionForm(instance=queryset1)
if not queryset1:
form1=EditOptionForm()
else:
form = EditQuestionForm()
form1=EditOptionForm()
context={
'form':form,
'form1':form1
}
return render(request,'chatbot/question_edit.html',context=context)

I want to retrieves all Post objects which have at least one followUser whose user=request.user

FieldError at /en/account/profile/follow_list/
Cannot resolve keyword 'followidf' into field. Choices are: author, author_id,
This spanning can be as deep as you’d like.
It works backwards, too. While it can be customized, by default you refer to a “reverse” relationship in a lookup using the lowercase name of the model.
This example retrieves all Post objects which have at least one followUser whose user=request.user
class Post(models.Model):
title = models.CharField(max_length=250)
excerpt = models.TextField()
author = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='blog_posts')
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
favourite = models.ManyToManyField( User, related_name='favouriteUser', default=None, blank=True)
avatar = models.ImageField( upload_to=user_directory_path, default='users/avatar.png')
bio = models.TextField(max_length=5500, blank=True)
fullName = models.CharField(max_length=221, blank=True, default=rand_slug(5) )
dr = models.BooleanField( default=False )
slug = models.SlugField(max_length=250, unique=True, blank=True, default=rand_slug(8))
class followUser(models.Model):
folPr = models.ForeignKey(Profile, related_name='followfp',on_delete=models.CASCADE, default=None, blank=True)
follUser = models.ForeignKey(User, related_name='followidf',on_delete=models.CASCADE, default=None, blank=True)
vote = models.BooleanField(default=True)
publish = models.DateTimeField(default=timezone.now)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.follUser.username
# login_required
def follow_list(request):
new2 = Post.objects.filter( followidf__user =request.user )
return render(request, 'accounts/follow_list.html', {'new': new2 } )
try this code
#login_required
def follow_list(request):
follow = Profile.objects.filter( followfp__follUser= request.user)
user =[]
for d in follow:
user +=[d.user]
print(d.user.id)
new = Post.objects.filter( author__in = user)
return render(request, 'accounts/followList.html', {'new': new } )
try this maybe id_list = User.objects.prefetch_related('followidf_set).filter(id=request__user__id).values_list('id',flat=True).all() Post.objects.filter(user__id__in=id_list)
Post.objects.filter( followidf__user__author =request.user )
or,
Post.objects.filter( folluser__user__author =request.user )
you can try this if you getting author as request.user.
or can change this part __author , what you getting as request.user .
this code work but I don't want used loop
#login_required
def follow_list(request):
follow = Profile.objects.filter( followfp__follUser= request.user)
user =[]
for d in follow:
user +=[d.user]
print(d.user.id)
new = Post.objects.filter( author__in = user)
return render(request, 'accounts/followList.html', {'new': new } )

how to add values to a ManyToManyField

I have the following model that has a field called who_can_see and I don't know how to add data to this filed nor the type of data (dictionary, array...)
#modles.py
class Author(models.Model):
name = models.CharField(max_length=200)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
description = models.CharField(max_length=300)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_date = models.DateTimeField(default=timezone.now)
#🔴
who_can_see = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name='tweet_user', blank=True)
def __str__(self):
return self.title
you can see here where i add the 🔴, I am trying to who_can_see= [author, payload["who_can_see"]] so I expected the field who_can_see to be an array but when I make the POST request i got 500 error
#api_view(["POST"])
#csrf_exempt
#permission_classes([IsAuthenticated])
def add_book(request):
print({"request.data": request.data})
payload = request.data
user = request.user
try:
author = Author.objects.get(id=payload["author"])
book = Book.objects.create(
title=payload["title"],
description=payload["description"],
added_by=user,
author=author,
# who_can_see= [author, payload["who_can_see"]] # 🔴
)
serializer = BookSerializer(book)
return JsonResponse({'books': serializer.data}, safe=False, status=status.HTTP_201_CREATED)
except ObjectDoesNotExist as e:
return JsonResponse({'error': str(e)}, safe=False, status=status.HTTP_404_NOT_FOUND)
except Exception:
return JsonResponse({'error': 'Something terrible went wrong'}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
You should first create the book and then add the who_can_see property. Like the example on the documentation
You are almost good in your case. It would look like :
def add_book(request):
print({"request.data": request.data})
payload = request.data
user = request.user
try:
author = Author.objects.get(id=payload["author"])
book = Book.objects.create(
title=payload["title"],
description=payload["description"],
added_by=user,
author=author,
)
book.who_can_see.add(author)
serializer = BookSerializer(book)
return ...
EDIT : I realized that your model is not coherent with your view. Why do you link who_can_see to AUTH_USER_MODEL and not Author ? According to your view, your model should be :
who_can_see = models.ManyToManyField(Author,related_name='tweet_user', blank=True)

Get current post's category

So as the title says, i need to get the current post's category to use it in a "related posts" section, more precisely what to put in cat_posts = Post.objects.filter(Category=????)
(don't mind the comments variable since i removed part of my PostView from this post)
here's my code
views.py
def PostView(request, slug):
template_name = 'post-page.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
cat_posts = Post.objects.filter(Category=Post.Category)
cat_posts = cat_posts.order_by('-Date')[:3}
return render(request, template_name, {'post': post,
'cat_posts':cat_posts})
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return str(self.name)
class Post(models.Model):
title = models.CharField(max_length=120)
Category = models.CharField(max_length=120, default='None')
Thumbnail = models.ImageField(null=True, blank=True, upload_to="images/")
Text = RichTextField(blank=False, null=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
Overview = models.CharField(max_length=400)
Date = models.DateTimeField(auto_now_add=True)
main_story = models.BooleanField(default=False)
def __str__(self):
return str(self.title)
def get_absolute_url(self):
# return reverse('about', args=(str(self.id)))
return reverse('home')
You can obtain this with the post.Category (so the post *object, not the Post class):
def PostView(request, slug):
template_name = 'post-page.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
cat_posts = Post.objects.filter(
Category=post.Category
).order_by('-Date')[:3]
return render(
request,
template_name,
{'post': post, 'cat_posts':cat_posts}
)
It is however better to work with a ForeignKey [Django-doc] to the Category than a CharField: if you later change the name of the category, then your posts are no longer pointing to a valid category.
Note: normally the name of the fields in a Django model are written in snake_case, not PerlCase, so it should be: category instead of Category.

Django slug two in one confusion

I'm trying to display a blog as follows:
domain.com/blog/category/categoryA
domain.com/blog/category/categoryA/post-one
At the moment (1) works successfully, and (2) works partially, so it displays like:
domain.com/blog/post-one
How can I set post category - posts that belong to that category before display post slug?
My urls.py:
url(r'^(?P<slug>[-\w]+)/$', views.post_detail, name='post_detail'),
url(r'^category/(?P<category_slug>[-\w]+)/$', views.list_of_post_by_category, name='list_of_post_by_category'),
My views.py
def list_of_post_by_category(request,category_slug):
categories = Category.objects.all()
post = Post.objects.filter(status='published')
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
post = post.filter(category=category)
template = 'blog/category/list_of_post_by_category.html'
context = {'categories': categories, 'post': post}
return render(request, template, context)
def list_of_post(request):
post = Post.objects.filter(status="published")
template = 'blog/post/list_of_post.html'
context = {'post': post}
return render(request, template, context)
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
template = 'blog/post/post_detail.html'
context = {'post': post}
return render(request, template, context)
My models.py:
class Category(models.Model):
name = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def get_absoulte_url(self):
return reverse('blogCMSApp:list_of_post_by_category', args=[self.slug])
def __str__(self):
return self.name
# Posts
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published')
)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique=True)
content = models.TextField()
seo_title = models.CharField(max_length=250)
seo_description = models.CharField(max_length=160)
author = models.ForeignKey(User, related_name='blog_posts', on_delete=models.PROTECT)
published = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=9, choices=STATUS_CHOICES, default='draft')
def get_absoulte_url(self):
return reverse('blogCMSApp:post_detail', args=[self.slug])
def __str__(self):
return self.title
You need to look for that pattern in urls.py, and then you need to build the correct URL for the post links:
url(
r'^category/(?P<category_slug>[-\w]+)/(?P<slug>[-\w]+)/$',
views.post_detail,
name='post_detail'
),
url(
r'^category/(?P<category_slug>[-\w]+)/$',
views.list_of_post_by_category,
name='list_of_post_by_category'
),
And in the Post model:
def get_absoulte_url(self):
return reverse(
'blogCMSApp:post_detail',
args=[self.category.slug, self.slug]
)