Strip html comments middleware
Middleware for stripping all html comments from the response content before returning it to the client. This will also strip inline javascript with htmlcomments put around it!
- middleware
- comments
Middleware for stripping all html comments from the response content before returning it to the client. This will also strip inline javascript with htmlcomments put around it!
Newforms are made to make any kind of customizations easy. Sometimes standard methods of rendering HTML/XML/other content of the django.newforms is not enough to completely satisfy all web design needs so you may want to present forms in own templates. Using templates to render output as HTML or XML is straighforward, and in many cases easier then using standard approach. Step by step usage guide: 1. Create file in your project yourproject/utils/newforms.py and place Class TemplatedForm there 2. Create newforms subdirectory in templates dir, and post 2 templates there (form.html, field.html) from the documentation code for TemplatedForm class 3. Inherit from TemplatedForm in your form class to use this form.
This is a minor modification to [Upload a file using newforms](http://www.djangosnippets.org/snippets/95/) as posted by [mboersma](http://www.djangosnippets.org/snippets/95/). I altered the ZipUploadForm by removing lines 33 - 34: if 'zip_file' in self.clean_data: zip_file = self.clean_data['zip_file'] and adding a return statement to clean_zipfile, which returns the validated zip file. I also added the line: zip_file.clean = clean_zipfile so that when the full_clean() in called on the form, clean_zipfile will automatically run. All other code (the view & template) remain the same. ** Disclaimer ** I'm not *that* familiar with newforms, so please forgive me if some the explanation as to why this works is incorrect ;) Who knows, maybe the first guy had it right.
create_object and update_project modified to handle newforms (including FileFields). In addition, I have added some extras: 1. extra_fields - this is a dict or callable that contains additional fields to be passed to the form, for example stuff that is in the session. 2. on_success - callback called if form is valid and object created/updated, if this is not set the default behaviour is to send a redirect 3. on_failure - callback called if form is invalid, the default is to redisplay the form. Note that once newforms are finally done these functions are likely to be redundant, as generic views will be updated to use the newforms API, so use with caution.
*I suppose I'm kind of stubborn, but I prefer to use underscores to replace spaces and other characters. Of course, that shouldn't hold you back from using the build-in slugify filter :)* ** Forcing the slug to use ASCII equivalents: ** Transforming titles like "Äës" to slugs like "aes" was kind of a trial and error job. It now works for me. I hope `_string_to_slug(s):` proves a rather stable solution. Yet the worst-case scenario is that such characters are lost, I guess that is acceptable. Other ways of dealing with this problem can be found at [Latin1 to ASCII at Activestate](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/251871) or in the comments below. **How to use:** The slug fields in your model must have prepopulate_from set, the fields specified in it are used to build the slug. To prevent duplicates, a number is added to the slug if the slug already exists for the current field in another, previous, object. I guess there should be a cleaner way to distinguish between creating a new db entry or updating an existing one, sadly, the db back-end is kind of a black-box to me. At least this works ;) I choose not to alter the slug on an update to keep urls more bookmarkable. You could even extend this further by only updating the slug field if it hasn't been assigned a value.
You can download these files from [here](http://huangyilib.googlecode.com/svn/trunk/mashi_django) Django template, mako, genshi, they are the best three templates in python, aren't they? How to use these files ? [Using Mako in Django](http://fuzzythinker.blogspot.com/2007/04/using-mako-in-django.html) -- by John Leung
After reading the comment to my [snippet #49](http://www.djangosnippets.org/snippets/49/) it occurs to me that Python properties may not be obvious to everyone, and may be underutilized in Django models. Here is a simple example demonstrating a property to format a Person's name.
This class simplies the Feed class of django. The differences are: 1. Don't need define title and description template file 2. default feed generator class is Atom1Feed 3. According feed_url, EasyFeed can auto get the domain field, and you can also specify the domain parameter.(feed_url should be a full domain path, so you can use [Get the full request path](http://www.djangosnippets.org/snippets/41/) to get the full path of the feed url.) 4. There is a helper function render_feed() to return a response value. example --------- Feed class: class BookCommentsFeed(EasyFeed): def __init__(self, feed_url, book_id): super(BookCommentsFeed, self).__init__(feed_url) self.book_id = book_id self.book = Book.objects.get(id=int(book_id)) def link(self): return '/book/%s' % self.book_id def items(self): return self.book.comment_set.all().order_by('-createtime')[:15] def title(self): return 'Comments of: ' + self.book.title def description(self): return self.book.description def item_link(self, item): return '/book/%s/%s' % (self.book_id, item.chapter.num) def item_description(self, item): return item.content def item_title(self, item): return '#%d Comment for: ' % item.id + item.chapter.title def item_pubdate(self, item): return item.createtime def item_author_name(self, item): return item.username And the view code is: from feeds import * from utils.easyfeed import render_feed from utils.common import get_full_path def bookcomments(request, book_id): return render_feed(BookCommentsFeed(get_full_path(request), book_id))
Using newforms you can create forms from existing models easily and automatically with either `form_for_model(class)` or `form_for_instance(instance)`. Usually the automagically generated form fields are sufficient; however, sometimes you need to restrict selections in a choice field. You can also set the default selection when instantiating the form. In this example, if `acct` is not contained in the 'account' field choices, the selection defaults to the first entry. This example is probably not good practice when using `form_for_instance` because the existing value 'selection' of the choice field is lost and must be reset manually (see above).
Returns a sharpened copy of an image. Resizing down or thumbnailing your images with PIL.Image tends to make them blurry. I apply this snippet to such images in order to regain some sharpness.
`foo = dynamic_import ( 'rawr.i.am.a.lion' )` Will import `lion` from `rawr.i.am.a` and return it. (This isn't really Django specific) Props to Crast for the original.
By popular demand an example of search in models that spans more realtions. Keep a list of Q, filter the None away, feed the rest to .filter() Credit goes to Carlo C8E Miron for the idea... cheers buddy! ;)
A more simple version of [https://djangosnippets.org/snippets/1688/](https://djangosnippets.org/snippets/1688/), inheriting from `SelectDateWidget`, overriding only the necessarily. **Usage example:** **In models.py:** from django.db import models from django.utils.translation import gettext_lazy as _ class MyModel(models.Model): start = models.DateField( _("Start date"), ) end = models.DateField( _("End date"), ) class Meta: verbose_name = _("My model") **In forms.py:** from django import forms from .models import MyModel from .widgets import MonthYearWidget class MyModelForm(forms.ModelForm): class Meta: model = MyModel exclude = [] widgets = { "start": MonthYearWidget(), "end": MonthYearWidget(last_day=True), } **kwargs:** - *last_day :* if set to `True`, returns the last day of the month in the generated date.
Provide the ability to disable a select option with Django2. This can be done during the widget initialization (i.e when the widget field is created) or during form initialization. This will disable the select option based on a context or by specifying the values that should be disabled.
FileField delete file on delete or update