Skip to content

Commit 98d5861

Browse files
committed
added example app
1 parent 36b9d49 commit 98d5861

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Features
1515
Usage
1616
-----
1717

18+
Refer to the `example` app included in the package for a working example.
19+
1820
### Server Side
1921

2022
In your form, use the `AjaxClearableFileInput` on your `FileField` or `ImageField`.

example/__init__.py

Whitespace-only changes.

example/forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django import forms
2+
3+
from ajax_upload.widgets import AjaxClearableFileInput
4+
5+
from example.models import Product
6+
7+
8+
class ProductForm(forms.ModelForm):
9+
class Meta:
10+
model = Product
11+
widgets = {
12+
'image': AjaxClearableFileInput
13+
}

example/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.db import models
2+
3+
4+
class Product(models.Model):
5+
name = models.CharField(max_length=255)
6+
description = models.TextField()
7+
image = models.ImageField(upload_to='examples/', blank=True)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<link href="{{ STATIC_URL }}ajax_upload/css/ajax-upload-widget.css" rel="stylesheet" type="text/css"/>
4+
5+
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
6+
<script src="{{ STATIC_URL }}ajax_upload/js/jquery.iframe-transport.js"></script>
7+
<script src="{{ STATIC_URL }}ajax_upload/js/ajax-upload-widget.js"></script>
8+
9+
<form action="" method="POST" enctype="multipart/form-data">
10+
<h1>{% if product %}Editing Product: {{ product.name }}{% else %}Add Product{% endif %}</h1>
11+
{% csrf_token %}
12+
{{ form.as_p }}
13+
<input type="submit" value="Save"/>
14+
</form>
15+
<script>
16+
$(function() {
17+
AjaxUploadWidget.autoDiscover();
18+
});
19+
</script>
20+
</html>

example/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.conf.urls.defaults import patterns, url
2+
3+
4+
urlpatterns = patterns('example.views',
5+
url(r'^add/$', 'add_edit_product', name='example-add-product'),
6+
url(r'^edit/(?P<product_id>\d+)/$', 'add_edit_product', name='example-edit-product'),
7+
)

example/views.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.core.urlresolvers import reverse
2+
from django.shortcuts import get_object_or_404, render, redirect
3+
4+
from example.forms import ProductForm
5+
from example.models import Product
6+
7+
8+
def add_edit_product(request, product_id=None):
9+
if product_id:
10+
product = get_object_or_404(Product, id=product_id)
11+
else:
12+
product = None
13+
14+
if request.method == 'POST':
15+
form = ProductForm(instance=product, data=request.POST, files=request.FILES)
16+
if form.is_valid():
17+
product = form.save()
18+
return redirect(reverse('example-edit-product', args=[product.id]))
19+
else:
20+
form = ProductForm(instance=product)
21+
22+
return render(request, 'example/product.html', dictionary={
23+
'form': form,
24+
'product': product,
25+
})

0 commit comments

Comments
 (0)