My First Django Blog Project. Part 2.

in #tech3 years ago

Hey guys, I am learning Django and after learning all the basics and some advanced topics, I started to develop projects. Here is my first Django blog project.


Source

Codes Under "my_blog_project/App_Login/admin.py" file


from django.contrib import admin
from App_Login.models import UserProfile
# Register your models here.

admin.site.register(UserProfile)


Codes Under "my_blog_project/App_Login/apps.py" file


from django.apps import AppConfig


class AppLoginConfig(AppConfig):
    name = 'App_Login'


Codes Under "my_blog_project/App_Login/forms.py" file


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from App_Login.models import UserProfile

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('username','email','password1','password2')


class ProfileUpdateForm(UserChangeForm):
    class Meta:
        model = User
        fields = ('username','email','first_name','last_name')


class ProfilePic(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('profile_pic',)


Codes Under "my_blog_project/App_Login/models.py" file


from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name = 'user_profile',on_delete = models.CASCADE)
    profile_pic = models.ImageField(upload_to = 'profile_pics')


Codes Under "my_blog_project/App_Login/urls.py" file



from django.urls import path,include
from . import views

app_name = 'App_Login'

urlpatterns = [
    path('sign_up/',views.sign_up,name = 'sign_up'),
    path('login/',views.login_page,name = 'login_page'),
    path('logout/',views.logout_page,name = 'logout_page'),
    path('profile/',views.profile, name = 'profile'),
    path('update_profile/',views.update_profile,name = 'update_profile'),
    path('password/',views.password_change, name='password_change'),
    path('add_pro_pic/',views.add_pro_pic,name='add_pro_pic'),
    path('change_pro_pic/',views.change_pro_pic, name='change_pro_pic'),
]


Codes Under "my_blog_project/App_Login/views.py" file


from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm
from django.contrib.auth import login,authenticate,logout
from django.shortcuts import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from App_Login.forms import SignUpForm, ProfileUpdateForm,ProfilePic

# Create your views here.

def sign_up(request):
    form = SignUpForm()
    registered = False
    if request.method == 'POST':
        form = SignUpForm(data=request.POST)
        if form.is_valid():
            form.save()
            registered = True

    diction = {'form':form,'registered':registered}
    return render(request,'App_Login/sign_up.html',context = diction)


def login_page(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')

            user = authenticate(username=username,password=password)

            if user is not None:
                login(request,user)
                return HttpResponseRedirect(reverse('index'))
    diction = {'form':form}
    return render(request,'App_Login/login.html',context=diction)


@login_required
def logout_page(request):
    logout(request)
    return HttpResponseRedirect(reverse('index'))


@login_required
def profile(request):
    diction = {}
    return render(request,'App_Login/profile.html',context = diction)


@login_required
def update_profile(request):
    current_user = request.user
    form = ProfileUpdateForm(instance = current_user)

    if request.method == 'POST':
        form = ProfileUpdateForm(data = request.POST, instance = current_user)

        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('App_Login:profile'))
    diction = {'form':form}
    return render(request,'App_Login/update_profile.html',context=diction)


@login_required
def password_change(request):
    changed = False
    current_user = request.user
    form = PasswordChangeForm(current_user)
    if request.method == 'POST':
        form = PasswordChangeForm(current_user,data=request.POST)

        if form.is_valid():
            form.save()
            changed = True


    return render(request,'App_Login/password_change.html',context={'form':form,'changed':changed})



@login_required
def add_pro_pic(request):
    form = ProfilePic()
    if request.method == 'POST':
        form = ProfilePic(request.POST, request.FILES)
        if form.is_valid():
            user_obj = form.save(commit = False)
            user_obj.user = request.user
            user_obj.save()
            return HttpResponseRedirect(reverse('App_Login:profile'))
    return render(request,'App_Login/add_pro_pic.html',context={'form':form})


@login_required
def change_pro_pic(request):
    form = ProfilePic(instance=request.user.user_profile)
    if request.method == 'POST':
        form = ProfilePic(request.POST, request.FILES, instance = request.user.user_profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('App_Login:profile'))

    return render(request,'App_Login/add_pro_pic.html',context={'form':form})



To be continued...

Sort:  

Congratulations @sksksk! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 50 HP as payout for your posts, comments and curation.
Your next payout target is 100 HP.
The unit is Hive Power equivalent because post and comment rewards can be split into HP and HBD

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!