PART-15

It’s now the time to add the reset password functionality. Without throwing all the URLs or imports or settings required, let me take you to step by step. We need a URL for a reset password, right?, So jump in the blog_project/urls.py and add the URL or link for the password reset, using the built-in PasswordResetView present in the auth view of django.contrib and specify the url and template name there.

...
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),
name='password_reset'),

As we specified the template name there, let’s go create that password_reset file (where the other files login, logout exists) in the users/templates/users folder

<h1>Trouble…


Part-14

Image for post
Image for post

Moving back to our blogapp, we had defined some views as follows

def home(request):
content={'posts':Post.objects.all()}
return render(request, 'blogapp/home.html',content)

def post(request):
return HttpResponse('<h1>A detailed post..</h1>')
def create(request):
return HttpResponse('<h1>Create a new post</h1>')
def update(request):
return HttpResponse('<h1>Update your post..</h1>')

def delete(request):
return HttpResponse('<h1>Delete a post..</h1>')

There are many more things to do here.., implement update, delete ,create new or view detailed post.Let’s use class based views here

Class based views have many built-in functionalities,let’s use them to allow users to create,read,update or delete a post using CreateView, DetailView or ListView, UpdateView and DeleteView

Import these views in the blogapp/views.py as follows,

from…


Part-13

When it comes to build forms, Django Forms can be really handy.It will automate a good amount of work as well as providing a really stable and secure functionality.

Image for post
Image for post
pic from 123RF for llustration

Django handles three distinct parts of the work involved in forms:

  1. Preparing and restructuring data to make it ready for rendering;
  2. Creating HTML forms for the data;
  3. Receiving and processing submitted forms and data from the client.

The parts 1 and 3 are usually fine for the most cases. But when it comes to the actual HTML forms rendering, sometimes it lacks some options.

In our DjangoReads project I used Bootstrap


Part-12

Image for post
Image for post
Update user profile

Let’s make the images auto resize when users upload them and add a new feature for the users to update their profile info.

So first create a UserUpdateForm in users/forms.py (as we want to show it on profile page)

class UserUpdateForm(forms.ModelForm):
class Meta:
model=User
fields=['username','email']

This lets our form know that we want to work with username and email fields,and i’ven’t added profile picand bio here, because the model for them is Profile so it’ll be in ProfileUpdateForm.So import the profile model and add this class

class ProfileUpdateForm(forms.ModelForm):
class Meta:
model=Profile
fields=['pic','bio']

UserUpdateForm will allow us to update our username…


Implement Luhn algorithm to generate valid credit card numbers with python

Credit Cards
Credit Cards

Credit card numbers follow certain patterns.

The very first number is the Major Industry Identifier (MII), which tells you what sort of institution issued the card.

  • 1 and 2 are issued by airlines
  • 3 is issued by travel and entertainment
  • 4 and 5 are issued by banking and financial institutions
  • 6 is issued by merchandising and banking
  • 7 is issued by petroleum companies
  • 8 is issued by telecommunications companies
  • 9 is issued by national assignment

The first six digits are the Issuer Identification Number (IIN). These can be used to look up where the card originated from.

  • Visa: 4*****
  • American…


part — 11

We have been adding profiles through admin page but we don’t want such on the production, instead we want the profile to be created automatically with the default image for every user(whenever a user is created). We can do this with signals in Django.

Image for post
Image for post

The signals are utilities that allow us to associate events(creating profile) with actions(user is created). We can develop a function that will run when a signal(notify) calls it.

Let me give you a basic example:

models.py

from django.db import models
from django.db.models import signals
def create_cookie(sender, instance, created, **kwargs):
print "Let's start making cookies now"
class…


Part-10

Image for post
Image for post

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,

<a href="{%url 'blog-home'%}" class="nav-item">DjangoReads</a>
<a href="{%url 'about'%}" class="nav-item">About</a>
{%if user.is_authenticated%}
<a href="{%url 'profile'%}" class="nav-item">Profile</a>
<a href="{%url 'logout'%}" class="nav-item">Logout</a>
{%else%}
<a href="{%url 'register'%}" class="nav-item">Register/Login</a>
{%endif%}

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 …


Part -9

{Login/Logout User}

Image for post
Image for post

Django have built-in views for logging in and out users,so in blog_project/urls.py let’s import them as

from django.contrib.auth import views as auth_views

Since we are importing multiple views here so I make use of as keyword ,and we have imported them then we’ll create paths for them

...
path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),name='login'),
path('logout/',auth_views.LogoutView.as_view(),name='logout'),

And now I’ll make the login template under template/users folder..

{%extends ‘blogapp/base.html’%}
...
<form method='POST'>
{%csrf_token%}
{{form|crispy}}
<button type='submit'>Submit</button>
Need An Account? <a href='{%url 'register'%}'>Sign Up</a></form>
...

Notice that I added {%url ‘register’%} for redirecting users to Sign up form,you can do this in your Sign Up form as well…


PART -8

{User Registration}

Image for post
Image for post

Let’ s create a new app inside our blog_project for handling users,(where users can create, login or logout of their account) and name it as users,and not to forget to add it in our INSTALLED_APPS in settings.py

python manage.py startapp users

Now let’s create a register view,in users/views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserCreationForm
def register(request):
form = UserCreationForm()
return render(request,'users/register.html',{'form': form})

I am using the built-in UserCreationForm to create new user or allow users to register for our site through a form which will be rendered as an html form on the…


The MVT (Model View Template) is a software design pattern. It is a collection of three important components Model, View, and Template.

Image for post
Image for post
Model-View-Template
  • The data guys and gals needed a common interface to be able to work with disparate data sources, formats, and database software

Models are like a set of tools that make working with data and databases much easier

  • The hard-core coders needed a framework that allowed them to rapidly deploy changes within the system that kept everyone happy

Views is like a framework that handles communication between the user and the database and automates many of the painful…

Atufa Shireen

Posting Django Tutorials with Beginner to Advanced Projects

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store