I spend some time for this one and propose next solution :
At first your model has only one image. in this case you dont need
to add url() method . Now model is looks like
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
class BicycleAdItemKind(MPTTModel):
def item_kind_image(self):
return '<img align="middle" src="/media/%s" height="60px" />'
% self.image
item_kind_image.allow_tags = True
# Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
n_item_kind = models.CharField(max_length=50)
parent = TreeForeignKey('self', null=True,
blank=True, related_name='children')
description = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to='Images',
null=True, blank=True)
date_inserted = models.DateTimeField(auto_now_add=True)
date_last_update = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.n_item_kind
class MPTTMeta:
order_insertion_by = ['n_item_kind']
Also view should be simple :
# Create your views here.
from django.views.generic import CreateView, ListView
from newmodel.models import BicycleAdItemKind
from newmodel.form import CreatenewmodelForm
class newmodelListView(ListView):
model = BicycleAdItemKind
template_name = 'newmodel/index.html'
class newmodelAddView(CreateView):
model = BicycleAdItemKind
template_name = 'newmodel/create.html'
form_class = CreatenewmodelForm
# please update form valid method
def form_valid(self, form, **kwargs):
BicycleAdItemKind.objects.create()
And your urls.py
from django.conf.urls import patterns, include, url
from newmodel.views import newmodelAddView, newmodelListView
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'andrelopes.views.home', name='home'),
# url(r'^andrelopes/', include('andrelopes.foo.urls')),
url(r'^newmodel/create$', newmodelAddView.as_view()),
url(r'^newmodel/$', newmodelListView.as_view()),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
You should create two templates only it is not hard. I think that
form usage is over the top and you can skip it
2013/1/9 Sergiy Khohlov <
skho...@gmail.com>: