[Solved]-Django: Applying permissions in the URL dispatcher?

10👍 ✅ It’s possible to import the login required function and apply it to the generic view: from django.contrib.auth.decorators import login_required from django.views.generic.simple import direct_to_template urlpatterns = patterns(”, (r’^foo/$’, login_required(direct_to_template), {‘template’: ‘foo_index.html’}), ) as mention here. 👤Hoff Django ModelForm fails validation with no errors 18👍 A decorator is a fancy word for a function that … Read more

[Solved]-Django; is it possible to have a default value for queryset

11👍 ✅ If you just want to get a single instance, use get, not filter: employee = Employee.objects.get(age = 99) If that doesn’t exist, you’ll get a Employee.DoesNotExist exception, which you’ll need to catch. If there’s more than one 99 year-old employee, you’ll get a Employee.MultipleObjectsReturned exception, which you may want to catch. There’s always … Read more

[Solved]-Get daily counts of objects from Django

25👍 ✅ Alex pointed to the right answer in the comment: Count number of records by date in Django Credit goes to ara818 Guidoism.objects.extra({‘created’:”date(created)”}).values(‘created’).annotate(created_count=Count(‘id’)) from django.db.models import Count Guidoism.objects \ # get specific dates (not hours for example) and store in “created” .extra({‘created’:”date(created)”}) # get a values list of only “created” defined earlier .values(‘created’) # … Read more

[Solved]-Python: if more than one of three things is true, return false

12👍 ✅ One thing I’ve done in similar situations is this: coupon_types = (self.months, self.dollars, self.lifetime,) true_count = sum(1 for ct in coupon_types if ct) if true_count > 1: raise ValueError(“Coupon can be valid for only one of: months, lifetime, or dollars”) It’s now much easier to add new coupon types to check for in … Read more

[Solved]-How to prevent XSS attacks when I need to render HTML from a WYSIWYG editor?

8👍 ✅ You need to parse the HTML on the server and remove any tags and attributes that don’t meet a strict whitelist. You should parse it (or at least re-render it) as strict XML to prevent attackers from exploiting differences between fuzzy parsers. The whitelist must not include <script>, <style>, <link>, or <meta>, and … Read more

[Solved]-Django – How to prevent database foreign key constraint creation

21👍 ✅ django development version has a db_constraint field for ForeignKey model field – docs. 👤iurii How can i customize the html output of a widget in Django? 4👍 If you set managed=False (Django docs) in your model’s Meta class, Django will not create the table when you run syncdb. class AssessmentLocation(models.Model): name = models.CharField(max_length=150) … Read more

[Solved]-How can i customize the html output of a widget in Django?

25👍 ✅ Firstly, create a widgets.py file in an app. For my example, I’ll be making you an AdminImageWidget class that extends AdminFileWidget. Essentially, I want a image upload field that shows the currently uploaded image in an <img src=”” /> tag instead of just outputting the file’s path. Put the following class in your … Read more

[Solved]-Django render template within a template

21👍 ✅ How about include? Like so: {% include “name_snippet.html” with person=”Jane” greeting=”Hello” %} 👤neoascetic How do I delete a cached template fragment in Django? 4👍 We usually create a custom inclusion tag for this. 👤Ignacio Vazquez-Abrams Unexpected error: replace() takes 2 positional arguments but 3 were given Nested loop in Django template Where is … Read more

[Solved]-How do I delete a cached template fragment in Django?

15👍 For Django 1.6+ and from the Django Documentation you could just generate the key of the partial you’re looking for and delete it: from django.core.cache import cache from django.core.cache.utils import make_template_fragment_key # cache key for {% cache 500 sidebar username %} templatetag key = make_template_fragment_key(‘sidebar’, [username]) cache.delete(key) # invalidates cached template fragment You just … Read more