Generic Views for newforms
Two common generic views to be used with newforms in similar way django.views.generic.create_update views, but adapted to the newforms library.
- newforms
- view
- generic
Two common generic views to be used with newforms in similar way django.views.generic.create_update views, but adapted to the newforms library.
I needed this to work around a poorly configured redirecting service.
This is a very simple script to do a **simple** migration from plone content to django. ATNewsItems and PloneArticles are imported into the django model *Article* (with Foreignkey to the django models *File* and *Image*). ATDocuments are imported into the django model *Page*. **Usage** 1. Make sure that the Python version running Zope can import django 2. The django database should be writeable from within the Zope environment 3. Set the shell variable *DJANGO_MODULE_SETTING* to the settings file of the django project you want to import your Plone content to 4. Start the Zope server in the debug modus: $ bin/zopectl debug Then import the python module above, and pass the site you want to migrate to the start function: import simple_migration mysite = app.mysite simple_migration.start(mysite) Found 1253 objects Saved Test Document Type: ATNewsItem ... Hope it helps someone. Used for the migration of **Plone-2.5.3** content in a **Python-2.4.4** environment.
In some cases we need to know if we were opened via https from template. Usage: {% ifsecure %}using https{% else %}not using https{% endifsecure %} If you use fastcgi fastcgi_param HTTPS must exists.
This is a quick and dirty way to import blog entries and comments from Movable Type. Sorry I don't have time to document it better. Hopefully it will help you get started if you're looking to move from MT to Django.
This function can be util for transform pattern lists like these to strings: >>> list_to_pattern([42, 43, 44, 45]) '42-45' >>> list_to_pattern([15, 49, 50, 51, 52]) '15,49-52' >>> list_to_pattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) '0-13' You can use also the pattern to list function at [http://www.djangosnippets.org/snippets/495/](http://www.djangosnippets.org/snippets/495/)
Sometimes, when views are particularly complex, it's useful to send the different request methods to different functions. If you have to do this frequently, the repetition gets tiring. This helper class can be used to simplify that.
Makes sure the value a user entered into a a text-based field is automatically trimmed during form cleaning / validation. The 'field' parameter is expected to be a newforms.fields.Field _instance_.Only modifies str and unicode descending values, and passes everything else on untouched. Example: form = form_for_model(Person) make_trimming(form.fields['name'])
Simple middelware that listens for redirect responses, store the request's query log in the session when it finds one, and starts the next request's log off with those queries from before the redirect.
Make sure we don't create an infinite loop with a self-referencing foreign key. Many times I have needed category models that reference themselves, providing a flexible way to make children categories, grandchildren categories, etc. If you chain the slugs (or even ids) there's a chance you could end up with an infinite loop. Assumes a required, unique slug field ('slug') and an optional self-referencing foreign key ('parent'). All_data doesn't give you the object's ID, so we will find it via the unique slug. If either is not present we pass -- if there's no parent chosen it's not a problem, and if there is no slug present the submission will fail on that validation instead. It is worth noting that if the user changes the slug field on submission AND picks a bad parent it will not be caught. Infinite loop cases: 1. A references A 2. A tries to reference B, which is currently referencing A
Example for provided django-tagging url snippet: {% paginator 4 image_tag_paged tag=tag page %} links then equals {% url image_tag_paged tag=tag,page=n %}
A variation on a theme, inspired by [snippet 39][39] and [snippet 119][119]. The intent is to provide a more generic and simple mechanism for combining [Markdown][markdown] with [Pygments][pygments]. Common scenarios could include blogging or commenting. Snippet 119 seemed too specific and perhaps not as efficient, needing to process the HTML twice to accomplish it's ends. The one snag in the implementation is the need to use a tag other than `code` as a wrapper. See the comments for details. You will need the [BeautifulSoup][soup] module installed. Sample usage: from django.db import models class Blog(models.Model): '''Bare bones blogging model''' title = models.CharField(maxlength=255) slug = models.SlugField(maxlength=255, prepopulate_from=('title',)) pub_date = models.DateTimeField() # the cooked view, cached for quick retrieval blog = models.TextField() # the raw markdown-encoded text, saved for subsequent edits markdown = models.TextField() def save(self): from datetime import datetime if not self.id and not self.pub_date: self.pub_date = datetime.now() self.blog = pygmented_markdown(self.markdown) super(Blog, self).save() [39]: http://www.djangosnippets.org/snippets/39/ [119]: http://www.djangosnippets.org/snippets/119/ [soup]: http://www.crummy.com/software/BeautifulSoup/ [markdown]: http://www.freewisdom.org/projects/python-markdown/Installation [pygments]: http://pygments.org/
You have an update form with many fields and want to populate it with data coming from multiple models.
Originally posted on [skam.webfactional.com](http://skam.webfactional.com/blog/2007/07/16/babel-integration-django/) This is a very simple middleware that uses babel (http://babel.edgewall.org) for accessing locale data in request objects through request.LOCALE attribute. It also provides a function to get locale data outside views. settings.py: MIDDLEWARE_CLASSES = ( ... cut ... 'django.middleware.locale.LocaleMiddleware', 'middleware.locale.BabelMiddleware', ... cut ... )
This tag solves for me a simple need: generate random and compact codes for some of my applications. Codes are not guaranteed to be unique. **Usage** {% randomCode "num" %} **Example**: {% randomCode "8" %} generates a random 8 characters code. Create a file on your favorite template tags folder called randomcode.py and paste the code. Don't forget to include {% load randomcode %} in your templates
3110 snippets posted so far.