如何使用django提供私人图像

时间:2015-02-21 20:15:08

标签: python django django-models photo-gallery django-staticfiles

我打算创建一个摄影网站。我遇到了涉及提供文件的轻微问题。

我曾尝试使用静态文件,但很快意识到要获取照片所有人都要做的就是找到正确的网址,这样就不行了。

我需要一种方法,只能在付款后才允许访问照片,但我不知道如何执行此操作。

此外,我无法找到任何有关如何正确执行此操作的教程。

到目前为止我的代码:

views.py

from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, HttpResponseRedirect, render_to_response
from paypal.standard.forms import PayPalEncryptedPaymentsForm
from django.core.urlresolvers import reverse
import Photography_Site.settings as settings
from datetime import datetime
from Photo_Webapp import models
from paypal.standard.models import ST_PP_COMPLETED
from django.views.decorators.csrf import csrf_exempt
from paypal.standard.ipn.signals import valid_ipn_received
import os

def index(request):
    pic_01_path = models.Photo.objects.get(picture_id="pic0112015.2.15").display_image#.name)#.replace("./", settings.MEDIA_ROOT + "\\")

paypal_dict = {
    "business": settings.PAYPAL_RECEIVER_EMAIL,
    "amount": "0.01",
    "currency_code": "GBP",
    "item_name": "picture01",
    "invoice": "unique-%s" % (str(datetime.now())),
    "notify_url": "http://127.0.0.1:8000/notify/",
    "return_url": "http://127.0.0.1:8000/return/",
    "cancel_return": "http://127.0.0.1:8000/cancel/",

}
valid_ipn_received.connect(show_me_the_money)

# Create the instance.
form = PayPalEncryptedPaymentsForm(initial=paypal_dict)
context = {"form": form, 'img': pic_01_path}
return render_to_response("Photo_Webapp/index.html", context)


@csrf_exempt
def download(request):
    img = models.Photo.objects.get(picture_id="pic0112015.2.15").full_picture
    context = {'img': img}
    return render_to_response("Photo_Webapp/download_page.html", context)

@csrf_exempt
def paypal_view(request):
    valid_ipn_received.connect(show_me_the_money)

    context = {}
    return render_to_response("Photo_Webapp/paypal.html", context)

@csrf_exempt
def notify(request):
    valid_ipn_received.connect(show_me_the_money)

    context = {}
    return render_to_response("Photo_Webapp/notify.html", context)


@csrf_exempt
def cancel(request):
    valid_ipn_received.connect(show_me_the_money)

    context = {}
    return render_to_response("Photo_Webapp/cancel.html", context)

@csrf_exempt
def return_view(request):
    valid_ipn_received.connect(show_me_the_money)

    context = {}
    return render_to_response("Photo_Webapp/return.html", context)

models.py:

from django.db import models
from django.conf import settings
from django.core.files.storage import FileSystemStorage

private_media = FileSystemStorage(location=settings.PRIVATE_MEDIA_ROOT,
                              base_url=settings.PRIVATE_MEDIA_URL,
                              )

class Gallery(models.Model):
    name = models.CharField(max_length=100)
    #type = models.DecimalField(max_digits=6,
                                  #decimal_places=2)
    def __unicode__(self):
        return self.name


class Photo(models.Model):
    picture_name = models.CharField(max_length=100)
    picture_id = models.CharField(max_length=1000)
    date_time_added = models.CharField(max_length=300)
    price = models.DecimalField(max_digits=6,
                                  decimal_places=2)
    display_image = models.ImageField(upload_to="./imgs",
                                  blank=True, null=True)
    location_took = models.CharField(max_length=1000)
    description = models.TextField()
    gallery = models.ForeignKey(Gallery)
    full_picture = models.ImageField(storage=private_media)

    def __unicode__(self):
        return self.picture_name

1 个答案:

答案 0 :(得分:0)

如果我理解您的需求,只有经过身份验证的用户才能访问照片。

由于您专门寻找教程,Tango With Django教程涵盖了用户身份验证。

目前,该教程并不完全与Django 1.7保持同步,但它会让您足够接近,您可以搜索网络来修复任何错误并让您的教程项目保持最新并正常工作。我刚刚使用Python 3.4和Django 1.7完成了它。谷歌和堆栈溢出应该是你忠实的伙伴。

相关问题