Posts

Showing posts with the label Django

Cron Parser And Validation In Python

Answer : The Croniter package seems like it may get what you need. Example from the docs: >>> from croniter import croniter >>> from datetime import datetime >>> base = datetime(2010, 1, 25, 4, 46) >>> iter = croniter('*/5 * * * *', base) # every 5 minites >>> print iter.get_next(datetime) # 2010-01-25 04:50:00 >>> print iter.get_next(datetime) # 2010-01-25 04:55:00 >>> print iter.get_next(datetime) # 2010-01-25 05:00:00 >>> >>> iter = croniter('2 4 * * mon,fri', base) # 04:02 on every Monday and Friday >>> print iter.get_next(datetime) # 2010-01-26 04:02:00 >>> print iter.get_next(datetime) # 2010-01-30 04:02:00 >>> print iter.get_next(datetime) # 2010-02-02 04:02:00 Per the code, it also appears to do validation on the entered format. Likely that you came across this already, but just in case :) Since the accepted answer is quite old, the same l...

Aggregation Of An Annotation In GROUP BY In Django

Answer : Update: Since Django 2.1, everything works out of the box. No workarounds needed and the produced query is correct. This is maybe a bit too late, but I have found the solution (tested with Django 1.11.1). The problem is, call to .values('publisher') , which is required to provide grouping, removes all annotations, that are not included in .values() fields param. And we can't include dbl_price to fields param, because it will add another GROUP BY statement. The solution in to make all aggregation, which requires annotated fields firstly, then call .values() and include that aggregations to fields param(this won't add GROUP BY , because they are aggregations). Then we should call .annotate() with ANY expression - this will make django add GROUP BY statement to SQL query using the only non-aggregation field in query - publisher . Title.objects .annotate(dbl_price=2*F('price')) .annotate(sum_of_prices=Sum('dbl_price')) ....

Add Request.GET Variable Using Django.shortcuts.redirect

Answer : Since redirect just returns an HttpResponseRedirect object, you could just alter that: response = redirect('url-name', x) response['Location'] += '?your=querystring' return response Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) I don't know of any way to do this without modifying the urls.py . I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering... You might want to write a thin wrapper to make this easier. Say, custom_redirect def custom_redirect(url_name, *args, **kwargs): from django.core.urlresolvers import reverse import urllib url = reverse(url_name, args = args) params = urllib.urlencode(kwargs) return HttpResponseRedirect(url + "?%s" % params) This can then be called from your views. For e.g. return custom_redirect('url-name', x, q = 'something') # Should ...

Create Django Super User In A Docker Container Without Inputting Password

Answer : Get the container ID and run the command. docker exec -it container_id python manage.py createsuperuser I recommend adding a new management command that will automatically create a superuser if no Users exist. See small example I created at https://github.com/dkarchmer/aws-eb-docker-django. In particular, see how I have a python manage.py initadmin which runs: class Command(BaseCommand): def handle(self, *args, **options): if Account.objects.count() == 0: for user in settings.ADMINS: username = user[0].replace(' ', '') email = user[1] password = 'admin' print('Creating account for %s (%s)' % (username, email)) admin = Account.objects.create_superuser(email=email, username=username, password=password) admin.is_active = True admin.is_admin = True admin.save() else: print(...

Cannot Get Django-debug-toolbar To Appear

Answer : I had the same problem but managed to fix it following dvl's comment on this page. Here is a summary of the fix: In settings.py if DEBUG: MIDDLEWARE += ( 'debug_toolbar.middleware.DebugToolbarMiddleware', ) INSTALLED_APPS += ( 'debug_toolbar', ) INTERNAL_IPS = ('127.0.0.1', ) DEBUG_TOOLBAR_CONFIG = { 'INTERCEPT_REDIRECTS': False, } In the project urls.py, add this url pattern to the end: from django.conf import settings if settings.DEBUG: import debug_toolbar urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), ] Some information for news users as me, when dev on virtual or remote machine Add this ligne in a views.py file print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR']) When the views is call, you can see the client IP in the shell You have to add this IP the settings.py file INTERNAL_IPS...

CSS Not Loading Wrong MIME Type Django

Answer : Adding following snippet into settings.py file may fix your problem: import mimetypes mimetypes.add_type("text/css", ".css", True) open your Chrome by F12 Developer Tool and check what you actually received. In my case, the CSS file actually redirected to another page. so MIME is text/html not text/css (My English is not very good.) I ran into this issue during development (production was using Nginx and serving from /static_cdn folder without any issues). The solution came from the Django docs: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)