how can I get all post of users in django [closed]

2024/7/5 11:10:33

I want to get users posts but I don't know how to do it. Which I neded to use relationships. I would be appreciate if you could help me

User = a --> post 1, post 2 post 3

User = b --> post4 , post 3, post 5

I want that my project behaves like this.so, I want to query posts according to user

Answer

So the answer depends on your model setup.

For example, if your models.py looks like this:

class Post(models.Model)user = models.ForeignKey(User)content = models.CharField(max_length=500)...def __str__(self):return self.user

Then if you wanted all of the posts of the user that is logged in, you would use this code, for example, in your request you would use filter to filter out the appropriate posts that match a certain parameter (in this case user):

def index_page(request):logged_in_user = request.userlogged_in_user_posts = Post.objects.filter(user=user)return render(request, 'index.html', {'posts': logged_in_user_posts})

Then, for example, you could manipulate this queryset in your index.html file as such:

<div>
{% for post in posts %}Username: {{ post.user.username }}Post: {{ post.content }}<br>{% endfor %}
</div>
https://en.xdnf.cn/q/120708.html

Related Q&A

Python 3: Getting IndexError: list index out of range on shuffle method

Im building a blackjack command line game and Ive run into a snag. The shuffle feature on my deck class object keeps coming up with IndexError: list index out of range on line 29. It is a sporadic bug…

How to encrypt a file

Just trimmed this down big timeI have an overall assignment that must read a file, encrypt it and then write the encrypted data to a new file.what ive tried is this:filename=input("Enter file name…

Any Idea on how Should I analyze this Algorithm? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Syntax error ; multiple statements found while

Why is there this syntax error Multiple statements found while compiling a single statement given when I run this code? Answer and help will be super appreciated for this python newbie here

No module named PyPDF2._codecs, even after already installed

I have installed PyPDF2==2.3.0, but I still get the error below when I import PyPDF2. The error message is:ModuleNotFoundError: No module named PyPDF2._codecs

rearding regex (python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

While Loop Guessing Number Game - Python

Im trying to make a guess the number between 1-10 game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start aga…

Calculating distance between word/document vectors from a nested dictionary

I have a nested dictionary as such:myDict = {a: {1:2, 2:163, 3:12, 4:67, 5:84}, about: {1:27, 2:45, 3:21, 4:10, 5:15}, apple: {1:0, 2: 5, 3:0, 4:10, 5:0}, anticipate: {1:1, 2:5, 3:0, 4:8, 5:7}, an: {1:…

Is there anything wrong with the Python code itself? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python Iterate Over String

So Ive got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters a…