[Solved]-How to force the use of SSL for some URL of my Django Application?

11👍 ✅ If by WSGI you actually mean Apache/mod_wsgi, then although mounted WSGI applications normally get run in their own sub interpreters, the 80/443 split is a special case and even though in different VirtualHost so long as mount point for WSGIScriptAlias, and the ServerName are the same, they will be merged. <VirtualHost *:80> ServerName … Read more

[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