Can some one help explain the following page to me http://nullege.com/codes/search/zinnia.models.Entry.objects.create
How do I create a entry in zinnia with a form.html file and a view function in django
I know it's basic python playing around with classes
the hellow world can be found here http://django-blog-zinnia.com/blog/
For your first question:
params = {'title': 'My entry 1', 'content': 'My content 1',
'tags': 'zinnia, test', 'slug': 'my-entry-1',
'status': PUBLISHED}
self.entry_1 = Entry.objects.create(**params)
self.entry_1.authors.add(self.authors[0])
self.entry_1.categories.add(*self.categories)
self.entry_1.sites.add(*self.sites)
Here Entry.objects.create(**params)
is equivalent to
`Entry.objects.create(title='My entry 1', content='My
conten 1', tags='zinnia, test', slug='my-entry-1',
status='PUBLISHED'),
which creates a new Entry with title "My entry 1" and content "My content 1"
and saves to the database. The base entry class looks something like this:
class EntryAbstractClass(models.Model):
"""Base Model design for publishing entries"""
STATUS_CHOICES = ((DRAFT, _('draft')),
(HIDDEN, _('hidden')),
(PUBLISHED, _('published')))
title = models.CharField(_('title'), max_length=255)
image = models.ImageField(_('image'), upload_to=UPLOAD_TO,
blank=True, help_text=_('used for illustration'))
content = models.TextField(_('content'))
excerpt = models.TextField(_('excerpt'), blank=True,
help_text=_('optional element'))
tags = TagField(_('tags'))
categories = models.ManyToManyField(Category, verbose_name=_('categories'),
related_name='entries',
blank=True, null=True)
related = models.ManyToManyField('self', verbose_name=_('related entries'),
blank=True, null=True)
slug = models.SlugField(help_text=_('used for publication'),
unique_for_date='creation_date',
max_length=255)
authors = models.ManyToManyField(User, verbose_name=_('authors'),
related_name='entries',
blank=True, null=False)
so lines like self.entry_1.authors.add(self.authors[0]
will relate
self.athors[0]
to self.entry_1
through ManyToManyField
.
As for your second question, yes you can create a form.html
and a view
function to add a new entry, but zinnia is designed to be used with Django
Admin interface to manage contents. It would also make your life much easier. To
use it you will need to enable 'django.contrib.admin',
in INSTALLED_APPS
in
your settings.py
and also the urls.py
files.
After Django admin is enabled, you can simply go to example.com/admin/
to
create new entries.
Now, if for some reason you do not or cannot use the Django admin, here is what
the views.py
would look like to add new entries:
#views.py
from . import forms as entryform
from zinnia.models.entry import Entry
def add_entry(request):
form = entryform.EntryForm(request.POST or None)
if not (request.method == 'POST' and form.is_valid()):
return render_to_response("forms.html", {'form': form})
title = request.POST['title']
content = request.POST['content']
#...many more..
Entry.objects.create(title=title, content=content)
return render_to_response('success.html', {'form': form,})
Note that you would also need to create a forms.py
to validate the submitted form.
As for your forms.html
you would need to write a form that contains all the
inputs you need such as "title" and "contents" and have it POST to this
add_entry
view.