Skip to content

Commit

Permalink
Customizing behavior for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Oct 8, 2023
1 parent 2de359d commit 6a71c24
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 2 deletions.
Binary file modified Module05/bookmarks/bookmarks/__pycache__/urls.cpython-311.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions Module05/bookmarks/bookmarks/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
path('images/', include('images.urls')),
]

if settings.DEBUG:
Expand Down
Binary file modified Module05/bookmarks/db.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
39 changes: 39 additions & 0 deletions Module05/bookmarks/images/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django import forms
from .models import Image
from django.utils.text import slugify
import requests
from django.core.files.base import ContentFile

class ImageCreateForm(forms.ModelForm):
class Meta:
model = Image
fields = ['title', 'url', 'desciption']
widgets = {
'url': forms.HiddenInput
}

def clean_url(self):
url = self.cleaned_data['url']
valid_extensions = ['jpg', '.jpeg', 'png']
extension = url.rsplit('.', 1)[1].lower()

if extension not in valid_extensions:
raise forms.ValidationError("The given URL doesnot match a valid image extension.")

return url

def save(self, force_insert=False, force_update=False, commit=True):
image = super().save(commit=False)
image_url = self.cleaned_data['url']
name = slugify(image.title)
extension = image_url.rsplit('.', 1)[1].lower()
image_name = f"{name}.{extension}"
response = requests.get(image_url)
image.image.save(image_name, ContentFile(response.content), save=False)

if commit:
image.save()
return image



13 changes: 13 additions & 0 deletions Module05/bookmarks/images/templates/images/image/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}

{% block title %}Bookmark an image{% endblock title %}

{% block content %}
<h1>Bookmark an image</h1>
<img src="{{request.GET.url}}" alt="image" class="image-preview">
<form method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Bookmark it!">
</form>
{% endblock content %}
6 changes: 6 additions & 0 deletions Module05/bookmarks/images/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from. import views

urlpatterns = [
path('create/', views.image_create, name='create')
]
21 changes: 19 additions & 2 deletions Module05/bookmarks/images/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import ImageCreateForm

# Create your views here.
@login_required
def image_create(request):
if request.method == 'POST':
form = ImageCreateForm(data=request.POST)
if form.is_valid():
cd = form.cleaned_data
new_image = form.save(commit=False)
new_image.user = request.user
new_image.save()
messages.success(request, "Image created successfully.")
return redirect(new_image.get_absolute_url())
else:
form = ImageCreateForm(data=request.GET)

return render(request, 'images/image/create.html', {'section': 'images', 'form': form})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added Module05/bookmarks/z.py
Empty file.

0 comments on commit 6a71c24

Please sign in to comment.