Since I've published my little Django tag library "TagsField" I noticed many attempts to translate the page into English using BabelFish (and I know just how hideous the result tends to be :-) ). So I've found the time to translate it and am happy to invite all English-speaking Django users to give it a try.

P.S. Also if you're a native English speaker please do send me an email if you find any errors in the translation.

Комментарии: 3

  1. Phil Powell

    Great stuff Ivan - I'll certainly be taking a look at this as a possibility for using in a few upcoming applications. I've just used many-to-many in the past, but the auto-complete stuff you've done looks very useful.

  2. Luis Gustavo

    I could not figure out how to use it... You say it is just like many-to-many, could you please give an example with both many-to-many classes? I guess I am missing something. Thanks,
    Luis Gustavo

  3. Иван Сагалаев

    Ok, here's a very minimal example that should be enough to have it working. If not at least it'll give some ground to investigate a problem...

    models.py:

    from django.db import models
    from tags.models import Tag
    from tags.fields import TagsField
    
    class Article(models.Model):
      title = models.CharField(maxlength=50)
      tags = TagsField(Tag)  # Here you normaly use ManyToManyField(Tag)
    
    def __str__(self):
      return self.title
    

    For a view you can use Django's generic views but for completeness here's a pretty standard Django's view for dealing with forms:

    from articles.models import Article
    from django.form import FormWrapper
    
    def article(request, id):
      manipulator = Article.ChangeManipulator(id)
      if request.method == 'POST':
        data = request.POST.copy()
        errors = manipulator.get_validation_errors(data)
        manipulator.do_html2python(data)
        if not errors:
          manipulator.save(data)
          return HttpResponseRedirect(...)
      else:
        data = manipulator.flatten_data()
        errors = {}
      return render_to_response('article.html', {
        'form': FormWrapper(manipulator, data, errors),
      })
    

    And the template, article.html:

    <form action="./" method="post">
      <p>Title {{ form.title }}</p>
      <fieldset>
        <legend>Tags</legend>
    
        {{ form.tags }}
      </fieldset>
    </form>
    

Добавить комментарий