What i want is that when user fills up the registration form and upload his pic , in want that pic to be show on index.html page but its not showing up there so please help me guys
all the images are been stored in money/prifile_images folder
this is my index.html code
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Bank{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'money/styles.css' %}" rel="stylesheet">
</head>
<body id="bg" style="background-image: url('{% static "money/bg.jpg"%}')"; style="background-image: url('{% static "money/bg.jpg"%}')";>
<div class="indexdiv">
<h1 class="indextitle">Gautam Bank</h1>
<div class="indexsign">
{% if user.is_authenticated %}
Signed in as <strong>{{ user.username }}</strong>.
{% else %}
Not signed in.
{% endif %}
</div>
<ul class="nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'balance' %}">Balance</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'send' %}">Send Money</a>
</li>
<li class="nav-item">
<a class="nav-link history-link" href="{% url 'history' %}">Transaction History</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'loan' %}">Apply for Loan</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Logout</a>
</li>
</ul>
<hr>
</div>
<div class="indeximg">
{% if user_profile and user_profile.image %}
<img height="138" width="152" src="{{ user_profile.image.url }}" alt="User Profile Image">
{% else %}
<img height="138" width="152" src="{% static 'money/pic.jpg' %}" alt="Default Image">
{% endif %}
</div>
{% block body %}
{% endblock %}
</body>
</html>
views.py
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from .models import User,Transaction,BankAccount, UserProfile
from django.urls import reverse
from django.db import IntegrityError
from django.core.exceptions import ValidationError
from django.http import HttpResponse,HttpRequest, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from decimal import Decimal
@login_required
def index(request):
# Fetch the user profile
user_profile = UserProfile.objects.get(user=request.user)
return render(request, "money/index.html", {"user": request.user,"user_profile": user_profile})
def balance(request):
return render(request,"money/balance.html")
def send(request):
return render(request,"money/sendmoney.html")
def history(request):
return render(request,"money/history.html")
def loan(request):
return render(request,"money/loan.html")
def send_money_api(request):
if request.method =="POST":
amount = request.POST.get("amount")
to_whom_mcode = request.POST.get("towhom")
recieveruser= User.objects.get(userprofile__mcode = to_whom_mcode)
recieveracc=BankAccount.objects.get(user=recieveruser)
try:
senderacc = BankAccount.objects.get(user=request.user)
# Check if sender is trying to send money to themselves
if senderacc.user == recieveruser:
return JsonResponse({"success": False, "message": "Invalid recipient MCODE."})
# Convert amount to Decimal before performing subtraction
amount_decimal = Decimal(amount)
if senderacc.balance >= amount_decimal > 0:
senderacc.balance -= amount_decimal
recieveracc.balance += amount_decimal
senderacc.save()
recieveracc.save()
# Create a single Transaction record for the transaction
Transaction.objects.create(sender=request.user, receiver=recieveruser, amount=float(amount))
return JsonResponse({"success" : True})
else:
return JsonResponse({"success": False, "message": "Insufficient balance."})
except (User.DoesNotExist, BankAccount.DoesNotExist):
return JsonResponse({"success": False, "message": "Invalid recipient mcode."})
return JsonResponse({"success": False, "message": "Invalid request method."})
def history(request):
# Retrieve the user's sent and received transactions
# Get transactions where the user is either the sender or the receiver
transactions = Transaction.objects.filter(sender=request.user) | Transaction.objects.filter(receiver=request.user)
return render(request, 'money/history.html', {'transactions': transactions})
def login_in(request):
if request.method =="POST":
name = request.POST['name']
password = request.POST['password']
user = authenticate(request,username=name, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request,"money/login.html",{
"message":"Invalid Name or Password"
})
else:
return render(request, "money/login.html")
def register(request):
if request.method == "POST":
print(request.FILES) # Add this line to print request.FILES
name = request.POST['name']
email = request.POST['email']
date = request.POST['date']
mobile = request.POST['mobile']
address = request.POST['address']
tcode = request.POST['tcode']
# Use request.FILES for file uploads (like images)
image = request.FILES['image']
if 'image' in request.FILES:
image = request.FILES['image']
else:
return render(request, "money/register.html", {
"message": "Please select an image."
})
password = request.POST['password']
cpassword = request.POST['cpassword']
# Check if passwords match
if password != cpassword:
return render(request, "money/register.html", {
"message": "Password must match."
})
try:
# Create a user instance
user = User.objects.create_user(name, email, password)
user.save()
# Authenticate the user
auth_user = authenticate(request, username=name, password=password)
# Log in the user
login(request, auth_user)
# Create a BankAccount instance for the user
bank_account = BankAccount(user=request.user, balance=100.00)
bank_account.save()
# Create a user profile instance
profile = UserProfile(
user=request.user, # Associate with the authenticated user
date_of_birth=date,
mobile_number=mobile,
address=address,
mcode=tcode,
image=image,
)
profile.save()
# Redirect to the index page
return HttpResponseRedirect(reverse("index"))
except IntegrityError:
# Handle duplicate user creation
return render(request, "money/register.html", {
"message": "User with this email already exists."
})
except ValidationError:
return render(request, "money/register.html", {
"message": "Invalid data submitted."
})
else:
return render(request, "money/register.html")
urls.py
from django.contrib import admin
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("",views.login_in,name="login"),
path("register",views.register,name="register"),
path("index", views.index,name="index"),
path("history",views.history, name="history"),
path("loan",views.loan,name="loan"),
path("balance",views.balance,name="balance"),
path("send", views.send, name="send"),
path('send_money_api/', views.send_money_api, name='send_money_api'),
path('history/', views.history, name='transaction_history'),
]
# Serve uploaded media files during development
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
read my comment also