Django Photologue Upload Photo Example

泄露秘密 提交于 2019-12-11 04:38:52

问题


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 -


回答1:


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.




回答2:


One thing missing from the answer above is you have to set the success_url in the CreateView.as_view. For example: CreateView.as_view(model=Photo, success_url='/')

Otherwise you'll receive a ImproperlyConfigured error.



来源:https://stackoverflow.com/questions/20304387/django-photologue-upload-photo-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!