Django Photologue上传照片示例 [英] Django Photologue Upload Photo Example

查看:846
本文介绍了Django Photologue上传照片示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Django-Photologue,我可以在Django管理员网站上传文件并创建画廊。我一直在搜索下面的文档,了解如何创建照片上传表单,以便我的用户可以在我的网站上创建一个图库,但找不到一个简单的例子让我开始。我还设置了他们的示例应用程序,但是如何通过从视图/模板发布来上传和创建图库是不是很有帮助。



文件:
https ://django-photologue.readthedocs.org/en/2.7/
https://code.google.com/p/django-photologue/



有人可以提供一个如何创建上传的简单示例表单提交照片和创建一个图库供Django-Photologue使用(不使用管理网站)?



谢谢 -

解决方案

这是非常简单的, Photologue 具有其模型内的所有相关逻辑。



例如,要设置照片上传,可以使用CBV:



urls.py

  from django.views.generic import PhotoView 
from photologue.models import照片


urlpatterns = patterns('',
url(r' ^ photologue / photo / add / $',CreateView.as_view(model = Photo),
name ='add-photo'),
(r'^ photologue /',include('photologue.urls')),
...
/ pre>

在您的模板中,请记住设置 enctype 属性来处理文件。



templates / photologue / photo_form.html

 < form action =method =postaccept-charset = utf-8enctype =multipart / form-data> {%csrf_token%} 
{{form.as_p}}
< input type =submitname =submitvalue = 提交 >
< / form>

这基本上都是你需要的..可以看到,我们不使用任何自定义逻辑,一切都封装在照片模型中,而CBV则是其余的。



同样适用于Gallery,只需将模型替换为 Gallery 你很好去
显然,如果你需要一些自定义,你也可以这么做,但这不在范围之内,因为你没有指定需要处理的用例。


I've installed Django-Photologue and I can upload files and create galleries in my Django admin site. I've been searching the documentation below for examples on how to create a photo upload form so my users can create a gallery on my site but can't find a simple example to get me started. I've also setup their example application but it wasn't very helpful in terms of how to upload and create Galleries by POSTing from Views/Templates.

Docs: https://django-photologue.readthedocs.org/en/2.7/ https://code.google.com/p/django-photologue/

Can someone please provide a simple example of how I can create an upload form for submitting photos and creating a gallery for use with Django-Photologue ( not using just admin site)?

Thanks -

解决方案

This is quite simple, Photologue has all the relevant logic inside its models.

For example to setup photo upload, you can use CBV:

urls.py

from django.views.generic import CreateView
from photologue.models import Photo


urlpatterns = patterns('',
    url(r'^photologue/photo/add/$', CreateView.as_view(model=Photo),
        name='add-photo'),
    (r'^photologue/', include('photologue.urls')),
    ...

In your template, remember to set enctype attribute, to handle files.

templates/photologue/photo_form.html

<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="submit" value="Submit">
</form>

That's basically all you need.. as you can see, we don't use any custom logic, everything is encapsulated inside Photo model and the CBV does the rest.

The same applies to Gallery, just replace the model with Gallery and you're good to go. Obviously if you need some customization, you can do it as well, but that's outside the scope, since you didn't specify what use case you need to handle.

这篇关于Django Photologue上传照片示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆