Advanced ImageField for Django that provides widget to search Flickr and Google Images
django-adv-imagefield allows you to add an ImageField and provides a widget that can be used to search for images in admin itself.
pip install django-adv-imagefield
- In settings.py add:
media-field
inINSTALLED_APPS
- In settings.py set:
FLICKR_API_KEY
,GOOGLE_API_KEY
andGOOGLE_SENGINE_ID
- In your root
url.py
add one more url pattern:url(r'^api/', include('media_field.api')),
- In your
models.py
import and add to your modelMediaField
from django.db import models from media_field.db import MediaField class TestModel(models.Model): name = models.CharField(max_length=255) image = MediaField(blank=True)
This will allow using ordinary ModelForm.
6. If you want using MediaField
in django-admin, then in your app's admin.py import MediaFieldWidget
and specify it for your ModelAdmin
```
from django.contrib import admin
from .models import TestModel
from media_field.db import MediaField
from media_field.forms import MediaFieldWidget
class TestModelAdmin(admin.ModelAdmin):
formfield_overrides = {
MediaField: {'widget': MediaFieldWidget},
}
admin.site.register(TestModel, TestModelAdmin)
```
- If you want to change default widget width (100%) and image width(30%), you may specify the width in your form field like this
image = MediaField(blank=True, attrs={'width': '500px', 'image_width': '50%'})
- Go to example directory
- Run in the unpacked folder
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
python manage.py runserver
- Navigate to http://localhost:8000/admin , login and play with
TestModel
andMediaField
.