How To Determine The Correct User To Edit Posts - Django Blog #16

  Рет қаралды 22,652

Codemy.com

Codemy.com

Күн бұрын

Пікірлер: 66
@Codemycom
@Codemycom 4 жыл бұрын
▶️ Watch Entire Django Blog Playlist ✅ Subscribe To My KZbin Channel: bit.ly/3bWN6wj bit.ly/2IGzvOR ▶️ See More At: ✅ Join My Facebook Group: Codemy.com bit.ly/2GFmOBz ▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt! Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN
@cnslpsbly
@cnslpsbly 4 жыл бұрын
I just want to add that you can actually use a "and" statement in "if", so you can have only one statement like this: {% if user.is_authenticated and post.author.id == user.id %}. This saves up a bunch of lines and is more readable imho
@greetingsgentlemen.8179
@greetingsgentlemen.8179 4 жыл бұрын
thanks!
@georgesmith3022
@georgesmith3022 4 жыл бұрын
post.author == user will work as well, however he should protect the views also, otherwise somebody can call the url directly
@cnslpsbly
@cnslpsbly 4 жыл бұрын
@@georgesmith3022 even if they call the views they'd have to spoof the sesión to bypass this mechanism
@georgesmith3022
@georgesmith3022 4 жыл бұрын
@@cnslpsbly could you elaborate please? is this because of the csrf token in the forms? is implementing permissions in the views redundant or is it a good practice?
@cnslpsbly
@cnslpsbly 4 жыл бұрын
@@georgesmith3022 Django stores the session in a cookie, with it, it can tell which user is requesting which view. Checking user == post.author means checking the session user vs the post user. Django will only render the part of the html template that corresponds to the {if user = post.author } {else} so there's no way to hack this from the front end. Best practice is do this kinds of checks on the templates (backend) so you can't just change things on the front end.
@Cecileeeeeeeeeeeeeee
@Cecileeeeeeeeeeeeeee Жыл бұрын
Hey ! Thank you so much for your videos and especially this one, I finally succeeded in doing something that I had been stalling for days, searching for the solution in vain, and boom ! Thanks to you, it finally works! Thank you from the bottom of my heart for these explanations, it finally made sense!
@Codemycom
@Codemycom Жыл бұрын
Very welcome!
@darwinsterritory
@darwinsterritory 3 жыл бұрын
John, thank you for your intelligence, charisma, and your lessons, I watch you from Russia and I want to say: Спасибо!
@Codemycom
@Codemycom 3 жыл бұрын
My pleasure!
@pentestPY
@pentestPY 12 күн бұрын
I have to say, I didnt understood the category tutorial and skipping it for now, later on I will try again to see what is what... Now I am doing how to determine the correct user for editing thre blogpost
@AttentionModeHumbleEst2022
@AttentionModeHumbleEst2022 3 жыл бұрын
explaining and letting the student try to figure it out is much better than writing code and letting the student copy it. It's much more fun and easier to remember it this way.
@Codemycom
@Codemycom 3 жыл бұрын
lol
@gamerstrim
@gamerstrim 4 жыл бұрын
Fantastic! More video with django!! :)
@Codemycom
@Codemycom 4 жыл бұрын
More to come!
@fabiof.deaquino4731
@fabiof.deaquino4731 4 жыл бұрын
Thanks for sharing such a great content!
@Codemycom
@Codemycom 4 жыл бұрын
Glad you enjoy it!
@greetingsgentlemen.8179
@greetingsgentlemen.8179 4 жыл бұрын
Thank you so much! This was really helpful!
@Codemycom
@Codemycom 4 жыл бұрын
Glad you liked it!
@yao8978
@yao8978 3 жыл бұрын
I have added a Post with category that starts with a capital letter such as "Python", but in the "Python" category page exists no Post. How to solve it?
@pentestPY
@pentestPY 12 күн бұрын
This I understood thank you....
@chethanmgowda8718
@chethanmgowda8718 4 жыл бұрын
Thank you for sharing a video plz do add the post which consists a image of the post or related to the post if any!
@Codemycom
@Codemycom 4 жыл бұрын
Ok will probably do that
@TheIvanlisi
@TheIvanlisi 4 жыл бұрын
Hi!! Love your page. your video helped me very much to develop my python skills, So thank you. In my blog project, I need to communicate with the database. When a user creates a post the program should take a special key from the post add to it a new key and the new key to the database. The thing is because we used class in views instead of def I can't pass variable to the form. Maybe you can make a video that explains how to communicate with the database and add special keys to the database?
@dimensionalilusions4520
@dimensionalilusions4520 3 жыл бұрын
I like how you start with very first entry
@Codemycom
@Codemycom 3 жыл бұрын
Glad to hear it
@LavYash
@LavYash 4 жыл бұрын
Is this approach better or should we use the UserPassesTextMixin to achieve this? Please confirm.
@Codemycom
@Codemycom 4 жыл бұрын
Better? Use whatever method you want.
@LavYash
@LavYash 4 жыл бұрын
@@Codemycom Well, better might not have been the best choice of word as it depends on use case.. But, I do wanted to understand the difference between the two approach. I believe the HTTP response header would be different but is there any difference in terms of security or other parameters?
@hamzazahir1884
@hamzazahir1884 3 жыл бұрын
There is another loophole. If I open the Delete page and logout and instead of refreshing, I press the delete button as shown at 12:25 . It will still delete the post even while logged out. Any solution to this problem?
@TomislavMiletic
@TomislavMiletic 2 жыл бұрын
Thank you :) Is your paid course valid for 2023 / Django 4?
@Codemycom
@Codemycom 2 жыл бұрын
Yep..I have like a dozen Django courses at Codemy.com get total lifetime membership for $49 (all my courses, one time fee) using coupon code: chatgpt for the next couple days
@shreyamahindrakar7048
@shreyamahindrakar7048 3 жыл бұрын
How testimonials is managed by admin panel?
@johndwick792
@johndwick792 4 жыл бұрын
I want to determine the correct user by views and decorators. does anybody have any example of determining the correct user using views and decorators?
@pranavt.j.1157
@pranavt.j.1157 3 жыл бұрын
What if I want to make a blog post with users and editors. The users can make the post but only the people from the editors team can edit any blog? Should I make 2 models -> Users and Editors?
@Codemycom
@Codemycom 3 жыл бұрын
That's probably a good way to do it
@pranavt.j.1157
@pranavt.j.1157 3 жыл бұрын
@@Codemycom I want to make the website so that the website has users, and these users will go to a dashboard where they can send their blog to the editors. Now there are the editors who have access to another dashboard (another dashboard in the sense, they will be redirected to another page like "/editor"). The editors can review the blogs made by users and then publish it directly to the blog page. I have React as my Frontend and I will be using Django REST Framework to build an API. My initial approach was to make a single model Users(I'll just make a custom user model building upon the default User model by using AbstractUser). I'll add a Boolean field is_editor. Now in the Frontend when I get the API I'll use is_editor to redirect to "/editor". The problem is I'm a beginner in Django and is there any problem with this approach?
@Codemycom
@Codemycom 3 жыл бұрын
@@pranavt.j.1157 Give it a try :-)
@jumamakorere8597
@jumamakorere8597 3 жыл бұрын
Thank you!
@Codemycom
@Codemycom 3 жыл бұрын
You're welcome!
@sakibshabir1785
@sakibshabir1785 4 жыл бұрын
how to make the current logged in user see only his blogs??
@RinkumoniKhanikar
@RinkumoniKhanikar 4 жыл бұрын
Instead of all the blogs, how to make the current logged in user see only his blogs?
@RinkumoniKhanikar
@RinkumoniKhanikar 4 жыл бұрын
I found a solution We need to create a foreign key in the model That key should be referencing to the auth_user model of django In that foreign key we need to create a related_name value With that related name we can access blogs with the current user logged in
@azimsonny6036
@azimsonny6036 4 жыл бұрын
the admin should have the edit and delete permission.
@Codemycom
@Codemycom 4 жыл бұрын
The admin already has that permission on the Django admin dashboard, but if you wanted to give them that permission on the webpage itself, you could easily do so with an elif statement on the code from this video.
@delllatitude299
@delllatitude299 4 жыл бұрын
@@Codemycom or even in view.py file
@Codemycom
@Codemycom 4 жыл бұрын
@@delllatitude299 but why?
@delllatitude299
@delllatitude299 4 жыл бұрын
@@Codemycom no just a suggestion. we can also control this thing in view.py file to prevent and allow different users to access specific info or page.
@meldrickfernandes965
@meldrickfernandes965 3 жыл бұрын
Love this Video sir I got it it is small post.author not big Post.author.id I used it for my website
@kamgoogle7115
@kamgoogle7115 4 жыл бұрын
does anyone have a problem with is_authenticated always return false even though it return the same id both author id and user id, still jump to false. it was working until this video
@kamgoogle7115
@kamgoogle7115 4 жыл бұрын
i work around this with just post.author.id == user.id
@freekeys
@freekeys 4 жыл бұрын
Sir it better to do this stuff in views.py, like if str(request.user) == post.author: update post or "Do something" else: "Display message"
@Codemycom
@Codemycom 4 жыл бұрын
We're using classed based views, so that's not easily done.
@freekeys
@freekeys 4 жыл бұрын
@@Codemycom 🤔 really? I didn't know that interesting thanks sir
@nooobgamer01
@nooobgamer01 4 жыл бұрын
❤❤❤
@Codemycom
@Codemycom 4 жыл бұрын
:-)
@namankhurana
@namankhurana 4 жыл бұрын
Are you going to show who to make it so that in the author section you can only send p[posts as the person logged in?
@Codemycom
@Codemycom 4 жыл бұрын
Already did that: kzbin.info/www/bejne/inKrYWR_rpp0q6c
@spreadhysteria3650
@spreadhysteria3650 4 жыл бұрын
How to solve this problem without using JS. remove author from forms.py, fields and widget. add this line of code in the views.py class AddPostView(CreateView): model = Post form_class = PostForm template_name = 'add_post.html' def form_valid(self, form): form.instance.author = self.request.user return super().form_valid(form)
@Codemycom
@Codemycom 4 жыл бұрын
I do that very thing later on in the playlist
@bishalsonar1950
@bishalsonar1950 4 жыл бұрын
Sir, please share your linkedin profile
@Codemycom
@Codemycom 4 жыл бұрын
www.linkedin.com/pub/john-elder/55/a31/493
@shawnbeans7389
@shawnbeans7389 4 жыл бұрын
Noice
@sireno5977
@sireno5977 2 жыл бұрын
@Codemycom
@Codemycom 2 жыл бұрын
:-)
How To Restrict Who Can Make Blog Posts - Django Blog #17
14:48
Codemy.com
Рет қаралды 18 М.
Creating Blog User Logins With Authentication - Django Blog #9
19:14
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Blog Category Pages - Django Blog #13
16:13
Codemy.com
Рет қаралды 29 М.
Like or Unlike Tweets - Django Wednesdays Twitter #16
15:40
Codemy.com
Рет қаралды 3,2 М.
Custom User Model | Explore Django
14:01
Code With Stein
Рет қаралды 44 М.
Create Blog Like Button - Django Blog #18
19:35
Codemy.com
Рет қаралды 54 М.
Django Authentication & User Management - Full Tutorial
2:20:03
Tech With Tim
Рет қаралды 193 М.
Update and Edit Blog Post - Django Blog #6
14:53
Codemy.com
Рет қаралды 45 М.
Python Socket Programming Tutorial
49:43
Tech With Tim
Рет қаралды 1 МЛН
How To Upload Images With Django - Django Blog #26
17:59
Codemy.com
Рет қаралды 79 М.
Django Tutorial - Creating Custom User model in Django website #4
17:47