Sorry , re-post code… error github delete casual.
My code.
models.py:
from django.db import models
from stdimage import StdImageField
from django.urls import reverse
from django.utils.translation import gettext as _
import os
from . utils import path_and_rename, resize_and_autorotate
from django.conf import settings
from datetime import *
class Album(models.Model):
id = models.AutoField(primary_key=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('Author') )
title = models.CharField(max_length=255, verbose_name=_('Title'))
content = models.TextField(verbose_name=_('Content'))
slug = models.SlugField(null=False, unique=True, max_length=255, verbose_name=_('Slug'))
status = models.BooleanField(default=0, verbose_name=_('Status'))
created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created'))
updated = models.DateTimeField(auto_now=True, verbose_name=_('Updated'))
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('album', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
class Meta:
ordering = ['id']
class Photo(models.Model):
id = models.AutoField(primary_key=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('Author'))
content = models.CharField(max_length=255, verbose_name=_('Content'))
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='ALBUM')
photo = StdImageField(upload_to=path_and_rename, blank=True, render_variations= resize_and_autorotate, variations={
'large': (640, 480),
'thumbnail': (320, 220, True),
}, delete_orphans=True, verbose_name=_('Photo'))
order = models.IntegerField(default=0)
status = models.BooleanField(default=0, verbose_name=_('Status'))
created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created'))
updated = models.DateTimeField(auto_now=True, verbose_name=_('Updated'))
def delete(self, *args, **kwargs):
os.remove(os.path.join(settings.MEDIA_ROOT, self.photo.name))
super().delete(*args, **kwargs)
def save(self, *args, **kwargs):
super(Photo, self).save(*args, **kwargs)
def __str__(self):
return f'{self.id}'
class Meta:
ordering = ("order",)
Views.py
class VediAlbum_sortable(DetailView):
model = Photo
context_object_name = 'photo'
queryset = Photo.objects.filter(status=True)
def get(self, request, album_slug):
orgs = Album.objects.filter(slug__iexact = album_slug)
if not orgs:
return render(request, 'photo_404.html')
else:
photo = Photo.objects.filter(album__slug=album_slug, status=1)
album = Album.objects.filter(slug__isnull = False, slug=album_slug).order_by('created')
return render(request, 'photo_sortable.html', {'photo': photo, 'album': album})
urls.py
from django.urls import path
from . views import *
urlpatterns = [
path('', ListaAlbum.as_view(), name='album'),
path('<slug:album_slug>/', VediAlbum.as_view(), name='VediAlbum'),
path('sortable_album/<slug:album_slug>/', VediAlbum_sortable.as_view(), name='VediAlbumSort'),
]
photo_sortable.html:
{% extends "master_admin.html" %}
{% load static %}
{% block title %} Edit : {% for albumData in album %} {{albumData.title}} {%endfor%} {% endblock %}
<div class="row">
{% block _content %}
<div id="photo">
<div class="row" id="sortable">
{% for photoData in photo %}
<div class="col-md-4 mb-3">
<div class="card photo_card">
<div class="card-header">
</div>
<div class="card-body photo_gallery">
<p class="text-center"><a href="{% get_media_prefix %}{{photoData.photo.large }}" class="big" ><img src="{% get_media_prefix %}{{photoData.photo.thumbnail }}" class="figure-img img-fluid rounded big" alt="{{photoData.content}}" title="{{photoData.content}}"></a></p>
</div>
<div class="card-footer">
{{photoData.content}}
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% block _footer %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#sortable").sortable({
update: function(event, ui) {
var serial = $('#sortable').sortable('serialize');
$.ajax({
url: "", // ?? How to insert url ??
type: "post",
data: serial
});
},
}).disableSelection();
});
</script>
{% endblock %}
Idea insert url:
$.ajax({
url: "", // ?? How to insert url ??
type: "post",
data: serial
});
How to saving order position in db.?
My error log:
[Sun Aug 07 20:48:08.204887 2022] [wsgi:error] [pid 26874] [remote ::1:36024] Forbidden (CSRF token missing or incorrect.): /photo/sortable_album/i-fiori-del-mio-giardino/
Browser console:
Failed to load resource: the server responded with a status of 403 (Forbidden)
Idea ? Solved problem ?
Thanks.