Part-10

let’s change the navigation items for the authenticated and anonymous users,for the anonymous users the navbar will show login and register urls,whereas for authenticated users it’ll show logout url.So to do this

In base.html,

You can get information about the currently logged in user in templates with the {{ user }} template variable (this is added to the template context by default when you set up the project).{{ user.is_authenticated }} template variable determine whether the user is eligible to see specific content.

As you can see, we use if-else-endif template tags to conditionally display text based on whether {{ user.is_authenticated }} is true.

Note that i’ve added a profile link which shows the details of the current logged in user on their profile page.And to make it work , add it’s url pattern in blog_project/urls.py as below

and in views.py

and then create the html file profile page in users/templates/users/

note that the user.username renders the current user’s username from User model

now go to the /profile and you’ll see the username there,but when you logout and go to /profile you’ll still be able to access the page without the username on the page.So any anonymous user can access that page with /profile in the url.

To solve that let’s make it necessary for the users to be logged in to access the profile page with the login_required decorator.

So let’s go back where we have our profile view and add the login_required decorator above it

Now go back to the browser and reload the page , do you see

Django’s looking for login at /accounts/login,whereas we have it at /login so let’s tell Django where our login page is in settings.py at the bottom of the file add this line

and now if you reload the server you’ll be redirected to login page.Try adding some content to your profile page.

Now that we have created the profile page, we want to show the details of the user on profile page.So we’ll create a Profile model to store user’s bio and profile picture and not just that we’ll use a one-to-one relationship (between User Model and Profile Model) so that we can extend our user model with profile data and they are linked with each other and that the profile data is not related to authentication.

So in our users/models.py

Note that on_delete=models.CASCADE means that if user is deleted than the profile will be deleted as well,but if profile is deleted , user won’t be deleted.Register the model in the users/admin.py by importing Profile model

Now if you try to run your server you will get an error saying that:

Cannot use ImageField because Pillow is not installed.

and so we’ll need to install the Pillow library as

And then make migrations and migrate, run your server.

Go to admin page and add profiles by adding an image and bio for a user and only bio for another user.

Let’s query through our users model now to get the profile data.

(In InteractiveConsole)

You might notice that pics are stores in a folder named profile_pics in your root directory (where our manage.py file located).So if multiple models were saving images then they might clutter up our root directory.so let’s change the settings to where the images should be saved.

In settings.py

MEDIA_ROOT is where our uploaded files will be located just as before we had profile_pics,but this time it will upload the files to media directory which is present in the BASE_DIR and MEDI_URL is how we are going to access the pics in the browser.

Remove the earlier profiles and profile_pics directory and run the server again to make new profiles for the users.And notice where the pics are uploaded.(Add the default.jpg in the media directory)

And now as the django docs says,

As since it applies to development mode only we’ll add this to our urlpatterns as below

And now let’s update our profile.html to show the profile pic on the page as below

Just to let users add pictures to their posts, i’ll update my blogapp/models.py as

and then in home.html

and then make migrations

Next Up..

Posting Django Tutorials with Beginner to Advanced Projects