2014-02-21 80 views
1

我是新的django.I做一個項目,用戶可以上傳image.But我想確保用戶不能上傳圖像或文件,直到管理員批准現在我該如何做到這一點,爲此準備了模型,但是由於django缺乏經驗,我無法完成它,或者你可以說我沒有線索,現在可以幫助我,如果可以的話,我在這裏給出一個非常簡單的代碼。限制上傳,直到管理員批准上傳的文件在Django

這是我上傳圖像forms.py ...

from django import forms 

class DocumentForm(forms.Form): 
    photo = forms.ImageField(
         label='Select a file' 
        ) 

,這是我的models.py ...

from django.db import models 
from django.contrib.auth.models import User 

class Photo(models.Model): 
    name = models.CharField(max_length = 100) 
    photo = models.ImageField(upload_to = 'photos', blank=False,null=True) 
    approved = models.nullBooleanField() 
    uploaded_time = models.DateTimeField(auto_now_add = True,auto_now = False) 
    description = models.CharField(max_length = 80 , blank = False , null = True) 
    approved_by = models.CharField(max_length = 100) 
    user = models.ForeignKey(User) 

這裏我做一個現場的批准爲布爾字段。

,這是視圖文件/圖片上傳...

def UserImageUpload(request): 

    if request.method == 'POST': 
     form = DocumentForm(request.POST,request.FILES) 
     if form.is_valid(): 
     if approved: 
     newdoc = Photo(photo = request.FILES['photo'],user = request.user) 
     newdoc.save() 
     else: 

    else: 
    form = DocumentForm() 

    uploaded_image = Photo.objects.all() 

    return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request)) 

,這是文件或圖片上傳模板...

{% extends 'base.html'%} 
{% block title%}User Image Upload {% endblock %} 
{%block content%} 

    <form action="" method="post" enctype="multipart/form-data"> 
     {% csrf_token %} 
     <p>{{ form.non_field_errors }}</p> 
     <p>{{ form.photo.label_tag }} {{ form.photo.help_text }}</p> 
     <p> 
      {{ form.photo.errors }} 
      {{ form.photo }} 
     </p> 
     <p><input type="submit" value="Upload" /></p> 
</form> 

{%endblock%} 

回答

1

我想應該是這樣的: 1.用戶上傳圖片,並設置爲false。 2.在管理頁面中,您可以將批准更改爲true。 3.在現場顯示圖像之前,請檢查它是否被批准。