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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top