-
Notifications
You must be signed in to change notification settings - Fork 1
How to Store Image Fields
Yunus Kağan Aydın edited this page Apr 27, 2024
·
1 revision
We'll use MySQL database with a Django framework.
Create a model to represent the image data along with its metadata. We'll use a BinaryField
to store the image data.
# model.py
from django.db import models
class Image(models.Model):
name = models.CharField(max_length=100)
image_data = models.BinaryField()
Create a form to handle image uploads.
# forms.py
from django import forms
from .models import Image
class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ['name', 'image_data']
Create a view to handle image uploads.
# views.py
from django.shortcuts import render, redirect
from .forms import ImageForm
def upload_image(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('success') # Redirect to a success page
else:
form = ImageForm()
return render(request, 'upload.html', {'form': form})
Create a template to render the image upload form.
<!-- upload.html -->
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<h2>Upload Image</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
</body>
</html>
Turquiz App
- Implementation Phase 1
- Design
- Scenarios & Mockups
- Software Requirements Specification
- Forming the Team
- Lab Meeting #1
- Meeting #1
- Lab Meeting #2
- Meeting #2
- Project Plan Meeting
- Frontend Meeting #1
- Lab Meeting #3
- Meeting #3
- Meeting #4
- Lab Meeting #4
- Meeting #5
- Meeting #6
- Meeting #7
- User Scenario 1 - Explore and Register
- User Scenario 2 - Search for a Quiz & Create a Quiz
- User Scenario 3 - Take & Review a Quiz
- User Scenario 4 - Search Forum & Bookmark & Upvote & Answer
- User Scenario 5 - User Badges
- User Scenario Template
- User Stories