jeder bekam ichLöschfunktion müssen Benutzer in UserEdit schreiben
models.py
....
class ProductsTbl(models.Model):
.....
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
def get_image_path(instance, filename):
return '/'.join(['thing_images', instance.thing.slug, filename])
class Upload(models.Model):
thing = models.ForeignKey(ProductsTbl, related_name="uploads")
image = models.ImageField(upload_to=get_image_path) #delete or upload image for this one
def save(self, *args, **kwargs):
super(Upload, self).save(*args, **kwargs)
if self.image:
image = Image.open(self.image)
i_width, i_height = image.size
max_size = (640,480)
if i_width > 1000:
image.thumbnail(max_size, Image.ANTIALIAS)
image.save(self.image.path)
und ich bekam die views.py Funktion für das Bild löschen in der Klasse hochladen ist (models.Model) von models.py,
views.py
.....
@login_required
def delete_upload(request, id):
# grab the image
upload = Upload.objects.get(id=id)
upload.thing.useredit = request.user.username
upload.save()
# security check
# if upload.thing.user != request.user:
# raise Http404
# delete the image
upload.delete()
# refresh the edit page
return redirect('edit_thing_uploads', slug=upload.thing.slug)
, was ich tun muß, ist, wenn ich das Bild löschen Ich habe die „user“ in das „UserEdit“ zu schreiben.
jedoch ,, wenn ich nie das Bild löschen die "user" wird nicht in "UserEdit" schreiben ,, dagegen hier ist mein
edit_thing_uploads.html
{% extends 'base.html' %}
{% block title %}
Edit {{ thing.name }}'s Images - {{ block.super }} {% endblock %}
{% block content %}
<h1>Edit {{ thing.name }}'s Images</h1>
<h2>Uploaded images</h2>
{% for upload in uploads %}
<img src="{{ upload.image.url }}" alt="" />
<a href="{% url 'delete_upload' id=upload.id %}">Delete</a>
{% endfor %}
<h2>Upload a new image</h2>
<form role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
wenn ich lade ein Bild wird es „user“ in „UserEdit“ erfolgreich ,, die Funktion zum hochladen Bild in
schreibenviews.py
@login_required
def edit_thing_uploads(request, slug):
# grab the object...
thing = ProductsTbl.objects.get(slug=slug)
# double checking just for security
# if thing.user != request.user:
# raise Http404
form_class = ProductsTblUploadForm
# if we're coming to this view from a submitted form,
if request.method == 'POST':
# grab the data from the submitted form, # note the new "files" part
form = form_class(data=request.POST,files=request.FILES, instance=thing)
if form.is_valid():
thing = form.save(commit=False)
thing.useredit = request.user.username
thing.save()
# create a new object from the submitted form
Upload.objects.create(
image=form.cleaned_data['image'],
thing=thing,
)
return redirect('edit_thing_uploads', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# grab all the object's images
uploads = thing.uploads.all()
# and render the template
return render(request, 'things/edit_thing_uploads.html', {
'thing': thing,
'form': form,
'uploads': uploads,
})
jedoch ,, Ich habe die „user“ in „UserEdit“ zu lassen, wenn ich auch löschen ,,, wie kann ich es tun? Danke!