Django

Django Image Processing Webapp Part VI
In this part we will add the name of user to our imageEnhance model.
Edit Image/models.py add an extra attribute to out imageEnhance.
.................................................
from django.conf import settings
.................................................
# Create your models here.
class ImageEnhance(models.Model):
title = models.CharField(max_length=50)
image = models.ImageField(upload_to="",blank=True,null=True)
image_enhanced = models.ImageField(upload_to="",blank=True,null=True)
filter = models.CharField(max_length=50,blank=True,null=True)
likes = models.IntegerField(blank=True,null=True)
created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
#adding the author
author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
................................................
Now, edit Image/views.py to add the author.
from django.shortcuts import render
from django.http import HttpResponse
from django.core.paginator import Paginator,PageNotAnInteger, EmptyPage
from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user
from .models import ImageEnhance
from .forms import ImageForm
# Create your views here.
...............................................................
@login_required
def imageForm(request):
if request.method == 'POST':
form = ImageForm(request.POST,request.FILES)
user = get_user(request)
if form.is_valid():
#save the model instance
instance = form.save(commit=False)
instance.author = user
print("++++++++++++++++++++++",instance.author)
instance.grayscale()
instance.save()
#latest_object = ImageEnhance.objects.latest('id')
#latest_object.grayscale()
return render(request,"image_process.html",{"form":form,'object':instance,"filter":"Not Processed"})
else:
latest_object = ImageEnhance.objects.latest('id')
......................................................
else:
user = get_user(request)
form = ImageForm(initial={'author':user})
return render(request,"image_form.html",{'form':form})
Now add the author to the enhanced image if the user exists. Edit image_home.html
..................................................................
<img src="{{ im.image_enhanced.url }}" width="400" height="400">
<br>
<small class="card-text">{{im.created_at}} </small>
<br>
{% if im.author %}
<small class="card-text">Author: {{im.author}} </small>
{% endif %}
........................................................................
Output
The Login Page

Homepage after login

Uploading Image

Image after processing
Here we can see the name of the author with the processed image.

pontu
0
Tags :