[英]Restricting upload until the admin approved the uploaded file in Django
i am new in django.I am doing a project where user can upload image.But i want to make sure that the user can not upload image or file until the admin approved it.now how can i do this,have prepared models for this,but for inexperience in django i can not complete it or you can say i have no clue,now help me if you can, i am giving a very simple code here. 我是django的新用户。我正在做一个项目,用户可以上传图像。但是,我想确保用户在管理员批准之前不能上传图像或文件。现在我该怎么做,为此已经准备了模型,但是由于django的经验不足,我无法完成它,或者您可以说我毫无头绪,如果可以的话,请立即帮助我,我在这里给出了一个非常简单的代码。
This is my forms.py for the upload image... 这是我的上传图片的forms.py ...
from django import forms
class DocumentForm(forms.Form):
photo = forms.ImageField(
label='Select a file'
)
and this is my models.py... 这是我的模型。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)
here i make a field for approved as a Boolean field. 在这里,我将一个字段批准为布尔字段。
and this is view for file/image upload... 这是文件/图像上传的视图...
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))
and this is the template for file or image upload... 这是文件或图像上传的模板...
{% 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%}
I think it should be like this: 1.User upload image and you set approved to false. 我认为应该是这样的:1.User上传图片并将您设置批准为false。 2.On admin page you can change approved to true.
2.在管理页面上,您可以将批准更改为true。 3.Before you display image on site you check if it is approved.
3.在现场显示图像之前,请检查图像是否已批准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.