forked from yahyaaly151989/Mastering_Django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2de359d
commit 6a71c24
Showing
13 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
Binary file modified
BIN
+87 Bytes
(110%)
Module05/bookmarks/bookmarks/__pycache__/urls.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Module05/bookmarks/images/templates/images/image/create.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.