2016-05-16 35 views
0

每一個我刪除功能需要編寫用戶進入USEREDIT

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) 

和我的views.py功能,它是刪除類上傳圖片( models.Model)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) 

我所要做的是當我刪除圖像時,我必須將「user」寫入 「useredit」。

然而,,當從來沒有我刪除了 「用戶」 不會寫入 「USEREDIT」 ,,對比度的圖像,這是我

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 %} 

當我上傳一張圖片時,它會將「用戶」寫入「useredit」,成功上傳圖片的功能

views.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, 
    }) 

不過,,我不得不讓「用戶」到「USEREDIT」當我還刪除,,,我該怎麼辦呢?謝謝!

回答

0

我解決問題

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.thing.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)