How to return 404 page intentionally in django

When you set debug to False, you don’t have a custom handler, and the status code of the response is 404, the 404.html (if present) in your base template directory is used. To return a response with a 404 status, you can simply return an instance of django.http.HttpResponseNotFound. The reason you got a 500 is … Read more

celery – Tasks that need to run in priority

Celery does not support task priority. (v3.0) http://docs.celeryproject.org/en/master/faq.html#does-celery-support-task-priorities You may solve this problem by routing tasks. http://docs.celeryproject.org/en/latest/userguide/routing.html Prepare default and priority_high Queue. from kombu import Queue CELERY_DEFAULT_QUEUE = ‘default’ CELERY_QUEUES = ( Queue(‘default’), Queue(‘priority_high’), ) Run two daemon. user@x:/$ celery worker -Q priority_high user@y:/$ celery worker -Q default,priority_high And route task. your_task.apply_async(args=[‘…’], queue=”priority_high”)

How to detect that you’re in a test environment (check / determine if tests are being run)

Put this in your settings.py: import sys TESTING = len(sys.argv) > 1 and sys.argv[1] == ‘test’ This tests whether the second commandline argument (after ./manage.py) was test. Then you can access this variable from other modules, like so: from django.conf import settings if settings.TESTING: … There are good reasons to do this: suppose you’re accessing … Read more

How to limit fields in django-admin depending on user?

I think there is a more easy way to do that: Guest we have the same problem of Blog-Post blog/models.py: Class Blog(models.Model): … #fields like autor, title, stuff.. … class Post(models.Model): … #fields like blog, title, stuff.. … approved = models.BooleanField(default=False) approved_by = models.ForeignKey(User) class Meta: permissions = ( (“can_approve_post”, “Can approve post”), ) And … Read more

Django’s ModelForm – where is the list of Meta options?

Had this question myself today. For completeness, here is the documentation that currently exists: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelforms-overriding-default-fields And an excerpt from django/forms/models.py: class ModelFormOptions: def __init__(self, options=None): self.model = getattr(options, ‘model’, None) self.fields = getattr(options, ‘fields’, None) self.exclude = getattr(options, ‘exclude’, None) self.widgets = getattr(options, ‘widgets’, None) self.localized_fields = getattr(options, ‘localized_fields’, None) self.labels = getattr(options, ‘labels’, None) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)