Django Tutorial - User Specific Pages/Access

Tech With Tim · Beginner ·🧠 Large Language Models ·7y ago

Key Takeaways

This video tutorial demonstrates how to restrict access to pages or information to specific users in Django, using foreign keys, view functions, and authentication/authorization techniques.

Full Transcript

hey guys and welcome back from their django tutorial now I know I said I was gonna be doing that custom user models here but the thing is to do what I wanted to do I actually don't need to use those and they're way like they're they kind of over complicated so I figured we'll just do it the prop is the proper way or the way you're supposed to do it and then if you guys need to look up how to do custom user models it's not like super hard but just go online there's a few decent tutorials for that because I don't think I'm gonna cover that in that series unless you guys really want that and you leave that as a comment down below but anyways the goal of this video is to make specific to-do lists for specific users so that when different users log into the website they see different to-do lists just like you know if you're on Facebook you're gonna see different posts like stuff like that right so it's gonna be a little bit more custom to each user and obviously they can save their own to-do lists and I'll be saved just for them and other people shouldn't be able to view that so what I'm gonna do and I need to do this is inside of our model stop pie file inside of our main application I'm going to just add a foreign key to to-do list which is user and then that way our users will have a to-do list set where we can view all of the the different to-do list with that user so to do this I need to start by importing our user model so to get that I'm gonna say from Django dot contrib dot off dot models import user with a capital u now what I'll do is just add a foreign key I'm just gonna copy this and just change the names here instead of to-do-list we'll say user and we'll do user right here so have user model stop foreign key so now we're just saying that essentially every two ulis we create will be linked to some kind of user awesome so now what we need to do is actually make some migrations in our file so that we're gonna update our database accordingly to that now you shouldn't run into any issues doing this it should just like create the migrations for you but if for some reason this doesn't work what you need to do is delete all of your database files so delete your database file delete all of the everything inside of your migrations folder in including the pie cache but not being knit top pythons and that goes for all of your migration folders so any migration folder delete everything except this in knit pie and then anywhere you see a pie cache folder delete that and that'll allow you to do the migrations after that so now that we've done that so what do we need to do next well now when we created to-do lists things are a little bit different so this is where we're creating a to-do list and what we're doing to do that is just creating new to do this and saving in the database but now we need to save the to-do list to a specific user so to do that what we're gonna do is we're gonna say response dot user dot to-do-list underscore set now I don't know if this is gonna have to be capital or not so I'll have to play with that in a second but I think it should just be like that dot create and in this case we'll give a name to our to-do list and we'll say the name is gonna be equal to n now I believe this is how you created I'm gonna look to see how we created the item yeah so that is it's a similar way so we'll test this to see if that's working but this should now be saving our to-do list to a specific user now we can still access our to-do list by doing the slash ID of those to-do lists but we're gonna probably modify this a little bit so that you're only able to access to-do lists that are yours so that'll mean we'll look at the ID and we'll see if that's in the user set and if it is then we'll show that otherwise we'll say like you don't have access to this to-do list or something but let's first just make sure that we can create them ok so another thing that I'm gonna do actually is we've created this if I go to templates we've created no we have not created but in our based on HTML sorry guys we have this slash view here right and this originally I think I have it linking to to just a test stuff but what I want to do is actually make this go to a list of all of our different to-do lists so I've created this slash you so now what I'm gonna do is I'm going to add a URL inside of our URLs here that it's gonna be slash view and what slash you will do is it'll bring us to a list of all of our to-do lists so I'm just gonna say the function name will be view the link will be viewed and the name will be view and then what we'll do is we'll create a new I guess function inside of you stop hi and we'll say define view response and then what we're gonna do is we're gonna return a render of in this case we'll say response view and main / review dot HTML and then we'll pass in nothing as the context so now we've done all that we actually need to create a view template so I'm just gonna make a new file and I'm gonna save this as view dot HTML and I'm just gonna extend this from the base template so extend space dot main / paste HTML and then we will add our blocks so we need that block for the title so I'll say block title like that and then and the block so % % and block and then inside here I guess we'll just do like view and then we can copy this and just change the name to two content so con content like that and then inside of here what I'm gonna do is I'm just gonna create a for loop that's gonna loop through all of the different to-do lists for our user and display them it'll be very very basic but we can obviously modify it later so let's add a for loop so say for and in this case TD which is stanford to do list in this case user dot to do list underscore set then what we'll do is we'll end the for loop and and for and then in here we're gonna create some links to all of our to-do lists and display them into some little paragraph tags so I'm just gonna say P and in this case we'll add the name of our to-do list nice we'll add a link here so we'll say a will say href equals in this case slash and we'll say TD dot ID and then we will end not a tag so slash a and then and the P tag / P and here we'll just put the name of our to-do list which in this case is simply going to be TD dot name alright so that should display all of our to-do lists and just like a standard on an home like list kind of form we could make it look nicer but for now we're just gonna go with that and yeah and then we should be able to click on them to access those to-do lists so let me see if this is actually working so let's run server any errors let's see no errors so let's now go back to here and say I'm going to create let's just go to the home page and let's go to view okay so if you are not writing issues wherever it's not see anything so now let's try to create a to-do list so if I say Tim create new and we run into an area you know attribute to-do list underscore scent I thought that was gonna happen so let me see now if we need to add the Capitol's to that or not so inside views to-do list underscore set maybe that's the issue I don't know we'll see though and then I guess we're gonna have to change inside of our view dot HTML file to be capitals as well now if this doesn't work I'll quickly do a little bit of research and figure this out but let's try it now and let's go back to slash create can continue has no attribute to do list underscore sets all right I'll be back in one second guys with the proper attribute name for that alright I am back and with a ton of errors that I had to fix so essentially one of the first errors is we need to go here and change this to be user dot to do list dot all because I was trying to loop through something that's not iterable cuz this is a different object so just to put this all here you don't need the brackets after and leave this as lowercase to do list and then we need to do is go to models and we need to add these two fields to our for a key so we've to say related name we're gonna find this as to do list and then no equals true now essentially the related name is gonna be the way we access this from the related object which is user so we just change that to do list we say no equals true and then what we're gonna have to do save that is make migrations you know python man shop I make migrations Python managed up hi migrate you do that you should be set and then the last thing we have to change is inside view stop hi is what I've done here is I've created a new to-do list so I said T equals to-do list name equals n exactly what we had before same thing with saving the to-do list except now I said response dot user dot to-do list dot add T and then we can still link back to that ID because that ID is perfectly valid it still exists so we can see that and everything works fine so that is the fixes that's what we need to do remember that once you change these this models file by adding the related name and the nil equals true you need to make migrations to save those changes and then rerun the server so I'll show you now what it looks like so essentially you can see I have two to-do lists say hello when I go I can add items to the to-do list so let's say like item one add item there you go we can check it we can save it all that fun stuff if I go to view um it should be showing me this but for some reason that's not happening um let's see here let me try logging out and log you back in so let's say slash logout let's say slash login let's go Tim password sign in you and there we go we can see that we have now these popping up so we have to be logged in to be seeing these if we are not logged in we are obviously not gonna see anything because we don't have any of these so now let's try to create another account and add some of our to-do lists to that account and then see if it's different than what we have before so let's say we'll be making a let's say the email is Bob detect with Tim net let's say the password is the password I've been using consistently and there we go let's hit register alright so we don't need to save that but now let's go to view and we're still seeing hello twice which is interesting to me obits because we're not logged in to that account so let's do slash login and then what we'll do here is we'll say Bob and password and login and go to view and obviously there nothing there so let's create a new to-do list let's go new list create lists let's go to view and now we can see that we have new list showing up and that is working now I'm sure this is fairly buggy but this gives you an idea of how you can add things to specific users so you can see them on the webpage now obviously we can make it so that you can't view other people's to-do list cuz for example if I go / - I can view the hello to-do list even though that's not mine so we'll quickly do that and then I guess we'll probably wrap up after that so let's go to index here now and let's quickly say we'll get the to-do list objects will get so we get the to-do list as an object and we'll just see if that to-do list is in the users list if it's not then we'll just you know get out of that we won't let this happen otherwise we'll do whatever we already have so we'll simply just say if in this case response dot user dots I guess to-do list all like that but we'll say if LS in that then this is valid otherwise so we'll tap all of this and otherwise we'll simply just return we can either give an error or we could just like not let them go to this page and to do that we can just link maybe to the home page or something if they try to do that so I mean you guys can pick what you want to do there but I guess I'll just gonna ring link to our home page here so we'll just link to home dot HTML and we'll just say in this case where she maybe that's instead of going to home let's go to view dot HTML because then that way they can view their to-do list so maybe that makes more sense so now let's try this so let's try to go to to now refresh and you can see that now it's simply bringing us a new list because well that's not our to-do list so if I go to three you can see new list is valid because that's our to-do list so we're able to view that now this is probably not the most secure way to actually like hide information just would like this if statement and all that but I mean you guys can mess with that if you want and this is a very very basic example so anyways that is kind of an int for how to add the to-do list to specific users how to see that I know the login and logout system isn't super smoothly right now but you can obviously add that on the side my main goal with these tutorials was to give you guys kind of the starting blocks on how to do things ideas on how to go about stuff I really can't show you possibly everything because it's an infinite amount of stuff but you can do with Django and with that being said I will hopefully be doing a deployment tutorial soon it probably won't be for the next few days but at some point I definitely will do one and I encourage you guys to remind me and get on me if I'm not doing a deployment one because I will do that at some point and that has kind of been it for the actual writing code and all of this of the Django tutorial so I hope you guys enjoyed if you have any questions or future videos you'd like to see please let me know in the comments down below and I guess I'll see you in the next video [Music]

Original Description

This django tutorial covers how to restrict access to pages or information to specific users. This is extremely useful for things like blog posts where you only want specific people to be able to edit/view etc. Text-Based Tutorial: https://techwithtim.net/tutorials/django/user-specific-pages-data/ Playlist: https://www.youtube.com/watch?v=Z4D3M-NSN58&list=PLzMcBGfZo4-kQkZp-j9PNyKq7Yw5VYjq9 Want a sneak peak into my life? Follow my Instagram @tech_with_tim where I'm going to be filming a video each morning sharing my goals for the day and what I have planned: https://www.instagram.com/tech_with_tim ◾◾◾◾◾ 💻 Enroll in The Fundamentals of Programming w/ Python https://tech-with-tim.teachable.com/p... 📸 Instagram: https://www.instagram.com/tech_with_tim 🌎 Website https://techwithtim.net 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-rusci... 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 💵 One-Time Donations: https://www.paypal.com/donate/?token=... 💰 Patreon: https://www.patreon.com/techwithtim ◾◾◾◾◾◾ ⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡ Tags: - Tech With Tim - Django Tutorial - Django User Specific Access - Django User Specific Data - Python Tutorials
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches how to restrict access to pages or information to specific users in Django, using foreign keys, view functions, and authentication/authorization techniques. It provides a step-by-step guide on how to implement user-specific pages/access in Django.

Key Takeaways
  1. Add a foreign key to the to-do list model to link it to the user model
  2. Create migrations to update the database
  3. Save to-do lists to a specific user using the user's to-do list set
  4. Modify the to-do list access to only show lists belonging to the current user
  5. Define a view function to display a list of to-do lists for a user
  6. Create a new file called view.html to render the list of to-do lists
  7. Use a for loop to iterate through the user's to-do lists and display them as links
💡 Using foreign keys and view functions in Django can help implement user-specific pages/access, while authentication and authorization techniques ensure that only authorized users can access specific pages or information.

Related AI Lessons

How We Translate 300-Page Books Using Claude Without Hitting Token Limits
Learn how to translate long documents using Claude without hitting token limits by breaking them into overlapping chunks
Dev.to · 龚旭东
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Learn to build a Human-in-the-Loop (HITL) Feedback RAG system using embeddings, retrieval, and reranking to improve model performance
Medium · AI
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Learn to build a Human-in-the-Loop (HITL) Feedback RAG system using embeddings, retrieval, and reranking to improve LLM performance
Medium · LLM
A simple way to test model fallbacks with RouterBase
Learn to test model fallbacks with RouterBase using a simple fallback wrapper and OpenAI-compatible API surface
Dev.to · routerbasecom
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →